Skip to contentSkip to conversation

drivers: hwmon: Add On-Chip Controller driver

PATCH Superseded [email protected] 6 participants 24 messages Jan 16, 2017 – Jan 29, 2017

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover drivers: hwmon: Add On-Chip Controller driver

From: "Edward A. James" <[email protected]>

This patchset adds a hwmon driver to support the OCC (On-Chip Controller) on the IBM POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The OCC is an embedded processor that provides real time power and thermal monitoring.

The driver provides an interface on a BMC to poll OCC sensor data, set user power caps, and perform some basic OCC error handling. It interfaces with userspace through hwmon.

The driver is currently functional only for the OCC on POWER8 chips. Communicating with the POWER9 OCC requries FSI support.

Edward A. James (6):
hwmon: Add core On-Chip Controller support for POWER CPUs
hwmon: occ: Add sysfs interface
hwmon: occ: Add I2C transport implementation for SCOM operations
hwmon: occ: Add callbacks for parsing P8 OCC datastructures
hwmon: occ: Add hwmon implementation for the P8 OCC
hwmon: occ: Add callbacks for parsing P9 OCC datastructures

 Documentation/devicetree/bindings/hwmon/occ.txt |  13 +
 Documentation/hwmon/occ                         | 114 ++++++
 MAINTAINERS                                     |   7 +
 drivers/hwmon/Kconfig                           |   2 +
 drivers/hwmon/Makefile                          |   1 +
 drivers/hwmon/occ/Kconfig                       |  29 ++
 drivers/hwmon/occ/Makefile                      |   2 +
 drivers/hwmon/occ/occ.c                         | 522 ++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h                         |  81 ++++
 drivers/hwmon/occ/occ_p8.c                      | 247 +++++++++++
 drivers/hwmon/occ/occ_p8.h                      |  30 ++
 drivers/hwmon/occ/occ_p9.c                      | 308 ++++++++++++++
 drivers/hwmon/occ/occ_p9.h                      |  30 ++
 drivers/hwmon/occ/occ_scom_i2c.c                |  72 ++++
 drivers/hwmon/occ/occ_scom_i2c.h                |  26 ++
 drivers/hwmon/occ/occ_sysfs.c                   | 271 ++++++++++++
 drivers/hwmon/occ/occ_sysfs.h                   |  44 ++
 drivers/hwmon/occ/p8_occ_i2c.c                  | 104 +++++
 drivers/hwmon/occ/scom.h                        |  47 +++
 19 files changed, 1950 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/occ.txt
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/occ_p8.c
 create mode 100644 drivers/hwmon/occ/occ_p8.h
 create mode 100644 drivers/hwmon/occ/occ_p9.c
 create mode 100644 drivers/hwmon/occ/occ_p9.h
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
 create mode 100644 drivers/hwmon/occ/scom.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/6 hwmon: Add core On-Chip Controller support for POWER CPUs

From: "Edward A. James" <eajames-r/[email protected]>

Add core support for polling the OCC for it's sensor data and parsing that data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 522 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  81 +++++++
 drivers/hwmon/occ/scom.h   |  47 ++++
 9 files changed, 716 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +40 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9 processor
+(see the BMC - Host Communications section).
+
+Author: Eddie James <eajames-r/[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on the IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The
+OCC is an embedded processor that provides real time power and thermal
+monitoring.
+
+This driver provides an interface on a BMC to poll OCC sensor data, set user
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor data: power,
+temperature, frequency, and "caps," which indicate limits and thresholds used
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8 over I2C bus.
+However, to access the OCC register space, any data transfer must use a SCOM
+operation. SCOM is a procedure to initiate a data transfer, typically of 8
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements these
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9 over FSI bus
+and SBE engine. Once again, SCOM operations are required. This driver will
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +7 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:	git git://linuxtv.org/media_tree.git
 S:	Maintained
 F:	drivers/media/i2c/ov7670.c
 
+ON-CHIP CONTROLLER HWMON DRIVER
+M:	Eddie James <eajames-r/[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/hwmon/occ
+F:	drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
 M:	Kyungmin Park <[email protected]>
 L:	[email protected]
drivers/hwmon/Kconfig +2 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
 	  This driver can also be built as a module. If so, the module
 	  will be called nsa320-hwmon.
 
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)	+= wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)	+= xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)	+= occ/
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +15 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
+
+menuconfig SENSORS_PPC_OCC
+	bool "PPC On-Chip Controller"
+	help
+	  If you say yes here you get support to monitor Power CPU
+	  sensors via the On-Chip Controller (OCC).
+
+	  Generally this is used by management controllers such as a BMC
+	  on an OpenPower system.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +522 -0

522 lines not on this page · raw message

drivers/hwmon/occ/occ.h new file +81 -0

81 lines not on this page · raw message

drivers/hwmon/occ/scom.h new file +47 -0

47 lines not on this page · raw message

Signature · 6 lines
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#2 lore raw

Re: [PATCH linux v3 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

Quoted 266 lines · On 01/16/2017 01:13 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add core support for polling the OCC for it's sensor data and parsing that data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 522 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  81 +++++++
 drivers/hwmon/occ/scom.h   |  47 ++++
 9 files changed, 716 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +40 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9 processor
+(see the BMC - Host Communications section).
+
+Author: Eddie James <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on the IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The
+OCC is an embedded processor that provides real time power and thermal
+monitoring.
+
+This driver provides an interface on a BMC to poll OCC sensor data, set user
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor data: power,
+temperature, frequency, and "caps," which indicate limits and thresholds used
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8 over I2C bus.
+However, to access the OCC register space, any data transfer must use a SCOM
+operation. SCOM is a procedure to initiate a data transfer, typically of 8
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements these
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9 over FSI bus
+and SBE engine. Once again, SCOM operations are required. This driver will
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +7 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:	git git://linuxtv.org/media_tree.git
 S:	Maintained
 F:	drivers/media/i2c/ov7670.c
 
+ON-CHIP CONTROLLER HWMON DRIVER
+M:	Eddie James <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/hwmon/occ
+F:	drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
 M:	Kyungmin Park <[email protected]>
 L:	[email protected]
drivers/hwmon/Kconfig +2 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
 	  This driver can also be built as a module. If so, the module
 	  will be called nsa320-hwmon.
 
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)	+= wm831x-hwmon.o
 obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)	+= xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)	+= occ/
 obj-$(CONFIG_PMBUS)		+= pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +15 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
+
+menuconfig SENSORS_PPC_OCC
+	bool "PPC On-Chip Controller"
+	help
+	  If you say yes here you get support to monitor Power CPU
+	  sensors via the On-Chip Controller (OCC).
+
+	  Generally this is used by management controllers such as a BMC
+	  on an OpenPower system.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +118 -0
new file mode 100644
index 0000000..3089762
@@ -0,0 +1,522 @@
+/*
+ * occ.c - OCC hwmon driver
+ *
+ * This file contains the methods and data structures for the OCC hwmon driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+
+#define OCC_DATA_MAX		4096
+#define OCC_BMC_TIMEOUT_MS	20000
+
+/* To generate attn to OCC */
+#define ATTN_DATA		0x0006B035
+
+/* For BMC to read/write SRAM */
+#define OCB_ADDRESS		0x0006B070
+#define OCB_DATA		0x0006B075
+#define OCB_STATUS_CONTROL_AND	0x0006B072
+#define OCB_STATUS_CONTROL_OR	0x0006B073
+
+/* To init OCB */
+#define OCB_AND_INIT0		0xFBFFFFFF
+#define OCB_AND_INIT1		0xFFFFFFFF
+#define OCB_OR_INIT0		0x08000000
+#define OCB_OR_INIT1		0x00000000
+
+/* To generate attention on OCC */
+#define ATTN0			0x01010000
+#define ATTN1			0x00000000
+
+/* OCC return status */
+#define RESP_RETURN_CMD_IN_PRG	0xFF
+#define RESP_RETURN_SUCCESS	0
+#define RESP_RETURN_CMD_INVAL	0x11
+#define RESP_RETURN_CMD_LEN	0x12
+#define RESP_RETURN_DATA_INVAL	0x13
+#define RESP_RETURN_CHKSUM	0x14
+#define RESP_RETURN_OCC_ERR	0x15
+#define RESP_RETURN_STATE	0x16
+
+/* time interval to retry on "command in progress" return status */
+#define CMD_IN_PRG_INT_MS	100
+#define CMD_IN_PRG_RETRIES	(OCC_BMC_TIMEOUT_MS / CMD_IN_PRG_INT_MS)
+
+/* OCC command definitions */
+#define OCC_POLL		0
+#define OCC_SET_USER_POWR_CAP	0x22
+
+/* OCC poll command data */
+#define OCC_POLL_STAT_SENSOR	0x10
+
+/* OCC response data offsets */
+#define RESP_RETURN_STATUS	2
+#define RESP_DATA_LENGTH	3
+#define RESP_HEADER_OFFSET	5
+#define SENSOR_STR_OFFSET	37
+#define SENSOR_BLOCK_NUM_OFFSET	43
+#define SENSOR_BLOCK_OFFSET	45
+
+/* occ_poll_header
+ * structure to match the raw occ poll response data
+ */
+struct occ_poll_header {
+	u8 status;
+	u8 ext_status;
+	u8 occs_present;
+	u8 config;
+	u8 occ_state;
+	u8 mode;
+	u8 ips_status;
+	u8 error_log_id;
+	u32 error_log_addr_start;
+	u16 error_log_length;
+	u8 reserved2;
+	u8 reserved3;
+	u8 occ_code_level[16];
+	u8 sensor_eye_catcher[6];
+	u8 sensor_block_num;
+	u8 sensor_data_version;
+} __attribute__((packed, aligned(4)));
+
+struct occ_response {
+	struct occ_poll_header header;
+	struct occ_blocks data;
+};
+
+struct occ {
+	struct device *dev;
+	void *bus;
+	struct occ_bus_ops bus_ops;
+	struct occ_ops ops;
+	struct occ_config config;
+	unsigned long update_interval;
+	unsigned long last_updated;
+	struct mutex update_lock;
+	struct occ_response response;

drivers/hwmon/occ/occ.c line 128

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+	bool valid;
+};
+
+static void deinit_occ_resp_buf(struct occ_response *resp)
+{
+	int i;
+
+	if (!resp)
+		return;
+

This creates the impression that resp can ever be NULL, which is not the case.

Quoted 55 lines
drivers/hwmon/occ/occ.c new file +55 -0
+	if (!resp->data.blocks)
+		return;
+
+	for (i = 0; i < resp->header.sensor_block_num; ++i)
+		kfree(resp->data.blocks[i].sensors);
+
+	kfree(resp->data.blocks);
+/sensor_type_s
+	memset(resp, 0, sizeof(struct occ_response));
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+		resp->data.sensor_block_id[i] = -1;
+}
+
+static void *occ_get_sensor_by_type(struct occ_response *resp,
+				    enum sensor_type t)
+{
+	if (!resp->data.blocks)
+		return NULL;
+
+	if (resp->data.sensor_block_id[t] == -1)
+		return NULL;
+
+	return resp->data.blocks[resp->data.sensor_block_id[t]].sensors;
+}
+
+static int occ_check_sensor(struct occ *driver, u8 sensor_length,
+			    u8 sensor_num, enum sensor_type t, int block)
+{
+	void *sensor;
+	int type_block_id;
+	struct occ_response *resp = &driver->response;
+
+	sensor = occ_get_sensor_by_type(resp, t);
+
+	/* empty sensor block, release older sensor data */
+	if (sensor_num == 0 || sensor_length == 0) {
+		kfree(sensor);
+		dev_err(driver->dev, "no sensor blocks available\n");
+		return -ENODATA;
+	}
+
+	type_block_id = resp->data.sensor_block_id[t];
+	if (!sensor || sensor_num !=
+	    resp->data.blocks[type_block_id].header.sensor_num) {
+		kfree(sensor);
+		resp->data.blocks[block].sensors =
+			driver->ops.alloc_sensor(t, sensor_num);
+		if (!resp->data.blocks[block].sensors)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+

drivers/hwmon/occ/occ.c line 193

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+static int parse_occ_response(struct occ *driver, u8 *data,
+			      struct occ_response *resp)
+{
+	int b;
+	int s;
+	int rc;
+	int offset = SENSOR_BLOCK_OFFSET;
+	int sensor_type;
+	u8 sensor_block_num;
+	char sensor_type_string[5] = { 0 };

The only purpose of this variable seems to be to zero-terminate it to be able to print it. Why not use length-limited print functions instead ?

drivers/hwmon/occ/occ.c line 199

Quoted 6 lines
drivers/hwmon/occ/occ.c new file +6 -0
+	struct sensor_data_block_header *block;
+	struct device *dev = driver->dev;
+
+	/* check if the data is valid */
+	if (strncmp(&data[SENSOR_STR_OFFSET], "SENSOR", 6) != 0) {
+		dev_err(dev, "no SENSOR string in response\n");

Are those messages really necessary ? That can create a lot of logging noise if the occ responds with bad data.

drivers/hwmon/occ/occ.c line 204

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+		rc = -ENODATA;
+		goto err;
+	}
+
+	sensor_block_num = data[SENSOR_BLOCK_NUM_OFFSET];

There is a lot of trust in assuming that this data was actually received. At the same time, there is a lot of error checking. Why check if the information in this field is valid if you don't check if it was received in the first place ?

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+	if (sensor_block_num == 0) {
+		dev_err(dev, "no sensor blocks available\n");
+		rc = -ENODATA;
+		goto err;

drivers/hwmon/occ/occ.c line 218

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+	}
+
+	/* if number of sensor block has changed, re-malloc */
+	if (sensor_block_num != resp->header.sensor_block_num) {
+		deinit_occ_resp_buf(resp);
+		resp->data.blocks = kcalloc(sensor_block_num,
+					    sizeof(struct sensor_data_block),
+					    GFP_KERNEL);
+		if (!resp->data.blocks)
+			return -ENOMEM;

Why not just allocate the maximum number of sensors once ? That might waste a bit of memory, but it would make the code much less error prone. We know that there will never be more sensors than the number fitting into 4k of memory, so the "waste" would not really be that much.

Quoted 108 lines
drivers/hwmon/occ/occ.c new file +108 -0
+	}
+
+	memcpy(&resp->header, &data[RESP_HEADER_OFFSET],
+	       sizeof(struct occ_poll_header));
+	resp->header.error_log_addr_start =
+		be32_to_cpu(resp->header.error_log_addr_start);
+	resp->header.error_log_length =
+		be16_to_cpu(resp->header.error_log_length);
+
+	dev_dbg(dev, "Reading %d sensor blocks\n",
+		resp->header.sensor_block_num);
+	for (b = 0; b < sensor_block_num; b++) {
+		block = (struct sensor_data_block_header *)&data[offset];
+		/* copy to a null terminated string */
+		strncpy(sensor_type_string, block->sensor_type, 4);
+		offset += 8;
+
+		dev_dbg(dev, "sensor block[%d]: type: %s, sensor_num: %d\n", b,
+			sensor_type_string, block->sensor_num);
+
+		if (strncmp(block->sensor_type, "FREQ", 4) == 0)
+			sensor_type = FREQ;
+		else if (strncmp(block->sensor_type, "TEMP", 4) == 0)
+			sensor_type = TEMP;
+		else if (strncmp(block->sensor_type, "POWR", 4) == 0)
+			sensor_type = POWER;
+		else if (strncmp(block->sensor_type, "CAPS", 4) == 0)
+			sensor_type = CAPS;
+		else {
+			dev_err(dev, "sensor type not supported %s\n",
+				sensor_type_string);
+			continue;
+		}
+
+		rc = occ_check_sensor(driver, block->sensor_length,
+				      block->sensor_num, sensor_type, b);
+		if (rc == -ENOMEM)
+			goto err;
+		else if (rc)
+			continue;
+
+		resp->data.sensor_block_id[sensor_type] = b;
+		for (s = 0; s < block->sensor_num; s++) {
+			driver->ops.parse_sensor(data,
+						 resp->data.blocks[b].sensors,
+						 sensor_type, offset, s);
+			offset += block->sensor_length;
+		}
+
+		/* copy block data over to response pointer */
+		resp->data.blocks[b].header = *block;
+	}
+
+	return 0;
+err:
+	deinit_occ_resp_buf(resp);
+	return rc;
+}
+
+static u8 occ_send_cmd(struct occ *driver, u8 seq, u8 type, u16 length,
+		       const u8 *data, u8 *resp)
+{
+	u32 cmd1, cmd2;
+	u16 checksum = 0;
+	u16 length_le = cpu_to_le16(length);
+	bool retry = 0;
+	int i, rc, tries = 0;
+
+	cmd1 = (seq << 24) | (type << 16) | length_le;
+	memcpy(&cmd2, data, length);
+	cmd2 <<= ((4 - length) * 8);
+
+	/* checksum: sum of every bytes of cmd1, cmd2 */
+	for (i = 0; i < 4; i++) {
+		checksum += (cmd1 >> (i * 8)) & 0xFF;
+		checksum += (cmd2 >> (i * 8)) & 0xFF;
+	}
+
+	cmd2 |= checksum << ((2 - length) * 8);
+
+	/* Init OCB */
+	rc = driver->bus_ops.putscom(driver->bus, OCB_STATUS_CONTROL_OR,
+				     OCB_OR_INIT0, OCB_OR_INIT1);
+	if (rc)
+		goto err;
+
+	rc = driver->bus_ops.putscom(driver->bus, OCB_STATUS_CONTROL_AND,
+				     OCB_AND_INIT0, OCB_AND_INIT1);
+	if (rc)
+		goto err;
+
+	/* Send command, 2nd half of the 64-bit addr is unused (write 0) */
+	rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+				     driver->config.command_addr, 0);
+	if (rc)
+		goto err;
+
+	rc = driver->bus_ops.putscom(driver->bus, OCB_DATA, cmd1, cmd2);
+	if (rc)
+		goto err;
+
+	/* Trigger attention */
+	rc = driver->bus_ops.putscom(driver->bus, ATTN_DATA, ATTN0, ATTN1);
+	if (rc)
+		goto err;
+
+	/* Get response data */
+	rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,

drivers/hwmon/occ/occ.c line 336

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+				     driver->config.response_addr, 0);
+	if (rc)
+		goto err;
+
+	do {
+		if (retry) {
+			set_current_state(TASK_INTERRUPTIBLE);
+			schedule_timeout(msecs_to_jiffies(CMD_IN_PRG_INT_MS));
+		}
+

Isn't there a better way to do this ? What exactly is interrupted ? The getscom function ? If so, shouldn't the timeout be handled there ?

drivers/hwmon/occ/occ.c line 344

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+		rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+					     (u64 *)resp);
+		if (rc)
+			goto err;
+
+		/* retry if we get "command in progress" return status */
+		retry = (resp[RESP_RETURN_STATUS] == RESP_RETURN_CMD_IN_PRG) &&
+			(tries++ < CMD_IN_PRG_RETRIES);

There are some unnecessary () in this expression.

Quoted 16 lines
drivers/hwmon/occ/occ.c new file +16 -0
+	} while (retry);
+
+	switch (resp[RESP_RETURN_STATUS]) {
+	case RESP_RETURN_CMD_IN_PRG:
+		rc = -EALREADY;
+		break;
+	case RESP_RETURN_SUCCESS:
+		rc = 0;
+		break;
+	case RESP_RETURN_CMD_INVAL:
+	case RESP_RETURN_CMD_LEN:
+	case RESP_RETURN_DATA_INVAL:
+	case RESP_RETURN_CHKSUM:
+		rc = -EINVAL;
+		break;
+	case RESP_RETURN_OCC_ERR:

drivers/hwmon/occ/occ.c line 370

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+		rc = -EREMOTE;
+		break;
+	default:
+		rc = -EFAULT;
+	}
+
+	return rc;
+
+err:
+	dev_err(driver->dev, "scom op failed rc:%d\n", rc);

Some of the errors result in an error message, some don't. What is the guiding principle ? Even if there is an error message, it doesn't report what function caused it, which makes it as good as no error message. Can this be dropped ?

drivers/hwmon/occ/occ.c line 383

Quoted 13 lines
drivers/hwmon/occ/occ.c new file +13 -0
+	return rc;
+}
+
+static int occ_get_all(struct occ *driver)
+{
+	int i = 0, rc;
+	u8 *occ_data;
+	u16 num_bytes;
+	const u8 poll_cmd_data = OCC_POLL_STAT_SENSOR;
+	struct device *dev = driver->dev;
+	struct occ_response *resp = &driver->response;
+
+	occ_data = devm_kzalloc(dev, OCC_DATA_MAX, GFP_KERNEL);

I am a bot lost here. What is the purpose of using a devm function ?

Either case, you might want to allocate the memory once and just keep it allocated. Allocating a 4k buffer and freeing it each time data is read from the occ seems to be wasteful and not really worth it.

drivers/hwmon/occ/occ.c line 394

Quoted 11 lines
drivers/hwmon/occ/occ.c new file +11 -0
+	if (!occ_data)
+		return -ENOMEM;
+
+	rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data, occ_data);
+	if (rc) {
+		dev_err(dev, "OCC poll failed: %d\n", rc);
+		goto out;
+	}
+
+	num_bytes = get_unaligned((u16 *)&occ_data[RESP_DATA_LENGTH]);
+	num_bytes = be16_to_cpu(num_bytes);

What data is in big endian, and what data is in little endian ?

Looks like everything is big endian except for the length in the send command. Is that correct ? If so, an explanation would be useful, because the logic isn't entirely clear.

drivers/hwmon/occ/occ.c line 403

Quoted 9 lines
drivers/hwmon/occ/occ.c new file +9 -0
+	dev_dbg(dev, "OCC data length: %d\n", num_bytes);
+
+	if (num_bytes > OCC_DATA_MAX) {
+		dev_err(dev, "OCC data length must be < 4KB\n");
+		rc = -EINVAL;
+		goto out;
+	}
+
+	if (num_bytes <= 0) {

This can never be < 0.

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+		dev_err(dev, "OCC data length is zero\n");
+		rc = -EINVAL;
+		goto out;
+	}
+
+	/* read remaining data */
+	for (i = 8; i < num_bytes + 8; i += 8) {
+		rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,

drivers/hwmon/occ/occ.c line 421

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+					     (u64 *)&occ_data[i]);
+		if (rc) {
+			dev_err(dev, "scom op failed rc:%d\n", rc);
+			goto out;
+		}
+	}
+
+	/* don't need more sanity checks; buffer is alloc'd for max response
+	 * size so we just check for valid data in parse_occ_response
+	 */

Yes, but as mentioned above you don't check in that function if the data was actually received.

Quoted 6 lines
drivers/hwmon/occ/occ.c new file +6 -0
+	rc = parse_occ_response(driver, occ_data, resp);
+
+out:
+	devm_kfree(dev, occ_data);
+	return rc;
+}

drivers/hwmon/occ/occ.c line 437

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+
+int occ_update_device(struct occ *driver)
+{
+	int rc = 0;
+
+	mutex_lock(&driver->update_lock);
+
+	if (time_after(jiffies, driver->last_updated + driver->update_interval)
+	    || !driver->valid) {
+		driver->valid = 1;

valid is bool.

drivers/hwmon/occ/occ.c line 441

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+
+		rc = occ_get_all(driver);
+		if (rc)
+			driver->valid = 0;

valid is bool.

Quoted 9 lines
drivers/hwmon/occ/occ.c new file +9 -0
+
+		driver->last_updated = jiffies;
+	}
+
+	mutex_unlock(&driver->update_lock);
+
+	return rc;
+}
+EXPORT_SYMBOL(occ_update_device);

drivers/hwmon/occ/occ.c line 460

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+
+void *occ_get_sensor(struct occ *driver, int sensor_type)
+{
+	int rc;
+
+	/* occ_update_device locks the update lock */
+	rc = occ_update_device(driver);
+	if (rc) {
+		dev_err(driver->dev, "cannot get occ sensor data: %d\n",
+			rc);

Have you counted the number of error messages reported if anything goes wrong. This driver is quite noisy. I won't force you to drop the messages, but it seems to me that if anything goes wrong, the kernel log will be clogged with messages just from this driver.

Quoted 43 lines
drivers/hwmon/occ/occ.c new file +43 -0
+		return NULL;
+	}
+
+	return occ_get_sensor_by_type(&driver->response, sensor_type);
+}
+EXPORT_SYMBOL(occ_get_sensor);
+
+int occ_get_sensor_value(struct occ *occ, int sensor_type, int sensor_num,
+			 u32 hwmon, long *val)
+{
+	return occ->ops.get_sensor(occ, sensor_type, sensor_num, hwmon, val);
+}
+EXPORT_SYMBOL(occ_get_sensor_value);
+
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks **blocks)
+{
+	*blocks = &occ->response.data;
+}
+EXPORT_SYMBOL(occ_get_response_blocks);
+
+void occ_set_update_interval(struct occ *occ, unsigned long interval)
+{
+	occ->update_interval = msecs_to_jiffies(interval);
+}
+EXPORT_SYMBOL(occ_set_update_interval);
+
+int occ_set_user_powercap(struct occ *occ, u16 cap)
+{
+	u8 resp[8];
+
+	cap = cpu_to_be16(cap);
+
+	return occ_send_cmd(occ, 0, OCC_SET_USER_POWR_CAP, 2, (const u8 *)&cap,
+			    resp);
+}
+EXPORT_SYMBOL(occ_set_user_powercap);
+
+struct occ *occ_start(struct device *dev, void *bus,
+		      struct occ_bus_ops *bus_ops, const struct occ_ops *ops,
+		      const struct occ_config *config)
+{
+	struct occ *driver = devm_kzalloc(dev, sizeof(struct occ), GFP_KERNEL);
+

drivers/hwmon/occ/occ.c line 513

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+	if (!driver)
+		return ERR_PTR(-ENOMEM);
+
+	driver->dev = dev;
+	driver->bus = bus;
+	driver->bus_ops = *bus_ops;
+	driver->ops = *ops;
+	driver->config = *config;
+
+	driver->update_interval = HZ;

The update interval is only supposed to be configurable if it can be written into the hardware. Otherwise it is really up to user space to decide how often it wants to poll the data, not up to the kernel.

I don't see a function to send the update interval to the occ. As such, it should not be configurable but a constant.

Quoted 130 lines
drivers/hwmon/occ/occ.c new file +9 -0
+	mutex_init(&driver->update_lock);
+
+	return driver;
+}
+EXPORT_SYMBOL(occ_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC hwmon core driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ.h new file +81 -0
new file mode 100644
index 0000000..c86b06a
@@ -0,0 +1,81 @@
+/*
+ * occ.h - hwmon OCC driver
+ *
+ * This file contains data structures and function prototypes for common access
+ * between different bus protocols and host systems.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_H__
+#define __OCC_H__
+
+#include "scom.h"
+
+struct device;
+struct occ;
+
+/* sensor_data_block_header
+ * structure to match the raw occ sensor block header
+ */
+struct sensor_data_block_header {
+	u8 sensor_type[4];
+	u8 reserved0;
+	u8 sensor_format;
+	u8 sensor_length;
+	u8 sensor_num;
+} __attribute__((packed, aligned(4)));
+
+struct sensor_data_block {
+	struct sensor_data_block_header header;
+	void *sensors;
+};
+
+enum sensor_type {
+	FREQ = 0,
+	TEMP,
+	POWER,
+	CAPS,
+	MAX_OCC_SENSOR_TYPE
+};
+
+struct occ_ops {
+	void (*parse_sensor)(u8 *data, void *sensor, int sensor_type, int off,
+			     int snum);
+	void *(*alloc_sensor)(int sensor_type, int num_sensors);
+	int (*get_sensor)(struct occ *driver, int sensor_type, int sensor_num,
+			  u32 hwmon, long *val);
+};
+
+struct occ_config {
+	u32 command_addr;
+	u32 response_addr;
+};
+
+struct occ_blocks {
+	int sensor_block_id[MAX_OCC_SENSOR_TYPE];
+	struct sensor_data_block *blocks;
+};
+
+struct occ *occ_start(struct device *dev, void *bus,
+		      struct occ_bus_ops *bus_ops, const struct occ_ops *ops,
+		      const struct occ_config *config);
+void *occ_get_sensor(struct occ *occ, int sensor_type);
+int occ_get_sensor_value(struct occ *occ, int sensor_type, int sensor_num,
+			 u32 hwmon, long *val);
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks **blocks);
+int occ_update_device(struct occ *driver);
+void occ_set_update_interval(struct occ *occ, unsigned long interval);
+int occ_set_user_powercap(struct occ *occ, u16 cap);
+
+#endif /* __OCC_H__ */
drivers/hwmon/occ/scom.h new file +28 -0
new file mode 100644
index 0000000..c1da645
@@ -0,0 +1,47 @@
+/*
+ * scom.h - hwmon OCC driver
+ *
+ * This file contains data structures for scom operations to the OCC
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __SCOM_H__
+#define __SCOM_H__
+
+/*
+ * occ_bus_ops - represent the low-level transfer methods to communicate with
+ * the OCC.
+ *
+ * getscom - OCC scom read
+ * @bus: handle to slave device
+ * @address: address

drivers/hwmon/occ/scom.h line 38

Quoted 10 lines
drivers/hwmon/occ/scom.h new file +10 -0
+ * @data: where to store data read from slave; buffer size must be at least
+ * eight bytes.
+ *
+ * Returns 0 on success or a negative errno on error
+ *
+ * putscom - OCC scom write
+ * @bus: handle to slave device
+ * @address: address
+ * @data0: first data byte to write
+ * @data1: second data byte to write

32 bit bytes ? It might also be worthwhile to mention the expected byte order.

Quoted 10 lines
drivers/hwmon/occ/scom.h new file +9 -0
+ *
+ * Returns 0 on success or a negative errno on error
+ */
+struct occ_bus_ops {
+	int (*getscom)(void *bus, u32 address, u64 *data);
+	int (*putscom)(void *bus, u32 address, u32 data0, u32 data1);
+};
+
+#endif /* __SCOM_H__ */
#3 lore raw

Re: [PATCH linux v3 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

I still just can't get gmail to reply to this. Thanks for the review.

Quoted 5 lines · Guenter Roeck
Quoted 4 lines · On 01/16/2017 01:13 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add core support for polling the OCC for it's sensor data and parsing

that

Quoted 51 lines
Quoted 51 lines

data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 522
+++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  81 +++++++
 drivers/hwmon/occ/scom.h   |  47 ++++
 9 files changed, 716 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +8 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9

processor

Documentation/hwmon/occ new file +8 -0
+(see the BMC - Host Communications section).
+
+Author: Eddie James <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on the

IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The

Documentation/hwmon/occ new file +2 -0
+OCC is an embedded processor that provides real time power and thermal
+monitoring.
Quoted 8 lines
Quoted 8 lines
Documentation/hwmon/occ new file +2 -0
+
+This driver provides an interface on a BMC to poll OCC sensor data, set

user

Documentation/hwmon/occ new file +3 -0
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor data:

power,
+temperature, frequency, and "caps," which indicate limits and

thresholds

Quoted 7 lines

Documentation/hwmon/occ

Quoted 7 lines

used

Documentation/hwmon/occ new file +6 -0
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8 over

I2C

Quoted 4 lines
Quoted 4 lines

bus.
+However, to access the OCC register space, any data transfer must use a SCOM
+operation. SCOM is a procedure to initiate a data transfer, typically

of

Quoted 6 lines

Documentation/hwmon/occ

Quoted 6 lines

8

Documentation/hwmon/occ new file +5 -0
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements these
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9 over

FSI

Quoted 30 lines
Quoted 30 lines

bus
+and SBE engine. Once again, SCOM operations are required. This driver will

Documentation/hwmon/occ new file +1 -0
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +7 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:       git git://linuxtv.org/media_tree.git
 S:     Maintained
 F:     drivers/media/i2c/ov7670.c
 
+ON-CHIP CONTROLLER HWMON DRIVER
+M:     Eddie James <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     Documentation/hwmon/occ
+F:     drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
 M:     Kyungmin Park <[email protected]>
 L:     [email protected]
drivers/hwmon/Kconfig +0 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
          This driver can also be built as a module. If so, the module
          will be called nsa320-hwmon.
 
Quoted 10 lines

drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_WM831X) +=

Quoted 10 lines
drivers/hwmon/Kconfig +2 -0
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
        tristate "Philips PCF8591 ADC/DAC"
        depends on I2C
drivers/hwmon/Makefile +0 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)        +=

wm831x-hwmon.o

Quoted 166 lines
Quoted 166 lines
drivers/hwmon/Makefile +1 -0
 obj-$(CONFIG_SENSORS_WM8350)   += wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)    += xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)  += occ/
 obj-$(CONFIG_PMBUS)            += pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +15 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
+
+menuconfig SENSORS_PPC_OCC
+       bool "PPC On-Chip Controller"
+       help
+         If you say yes here you get support to monitor Power CPU
+         sensors via the On-Chip Controller (OCC).
+
+         Generally this is used by management controllers such as a BMC
+         on an OpenPower system.
+
+         This driver can also be built as a module. If so, the module
+         will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +4 -0
new file mode 100644
index 0000000..3089762
@@ -0,0 +1,522 @@
+/*
+ * occ.c - OCC hwmon driver
+ *
+ * This file contains the methods and data structures for the OCC hwmon

driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ.c new file +107 -0
+
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+
+#define OCC_DATA_MAX           4096
+#define OCC_BMC_TIMEOUT_MS     20000
+
+/* To generate attn to OCC */
+#define ATTN_DATA              0x0006B035
+
+/* For BMC to read/write SRAM */
+#define OCB_ADDRESS            0x0006B070
+#define OCB_DATA               0x0006B075
+#define OCB_STATUS_CONTROL_AND 0x0006B072
+#define OCB_STATUS_CONTROL_OR  0x0006B073
+
+/* To init OCB */
+#define OCB_AND_INIT0          0xFBFFFFFF
+#define OCB_AND_INIT1          0xFFFFFFFF
+#define OCB_OR_INIT0           0x08000000
+#define OCB_OR_INIT1           0x00000000
+
+/* To generate attention on OCC */
+#define ATTN0                  0x01010000
+#define ATTN1                  0x00000000
+
+/* OCC return status */
+#define RESP_RETURN_CMD_IN_PRG 0xFF
+#define RESP_RETURN_SUCCESS    0
+#define RESP_RETURN_CMD_INVAL  0x11
+#define RESP_RETURN_CMD_LEN    0x12
+#define RESP_RETURN_DATA_INVAL 0x13
+#define RESP_RETURN_CHKSUM     0x14
+#define RESP_RETURN_OCC_ERR    0x15
+#define RESP_RETURN_STATE      0x16
+
+/* time interval to retry on "command in progress" return status */
+#define CMD_IN_PRG_INT_MS      100
+#define CMD_IN_PRG_RETRIES     (OCC_BMC_TIMEOUT_MS / CMD_IN_PRG_INT_MS)
+
+/* OCC command definitions */
+#define OCC_POLL               0
+#define OCC_SET_USER_POWR_CAP  0x22
+
+/* OCC poll command data */
+#define OCC_POLL_STAT_SENSOR   0x10
+
+/* OCC response data offsets */
+#define RESP_RETURN_STATUS     2
+#define RESP_DATA_LENGTH       3
+#define RESP_HEADER_OFFSET     5
+#define SENSOR_STR_OFFSET      37
+#define SENSOR_BLOCK_NUM_OFFSET        43
+#define SENSOR_BLOCK_OFFSET    45
+
+/* occ_poll_header
+ * structure to match the raw occ poll response data
+ */
+struct occ_poll_header {
+       u8 status;
+       u8 ext_status;
+       u8 occs_present;
+       u8 config;
+       u8 occ_state;
+       u8 mode;
+       u8 ips_status;
+       u8 error_log_id;
+       u32 error_log_addr_start;
+       u16 error_log_length;
+       u8 reserved2;
+       u8 reserved3;
+       u8 occ_code_level[16];
+       u8 sensor_eye_catcher[6];
+       u8 sensor_block_num;
+       u8 sensor_data_version;
+} __attribute__((packed, aligned(4)));
+
+struct occ_response {
+       struct occ_poll_header header;
+       struct occ_blocks data;
+};
+
+struct occ {
+       struct device *dev;
+       void *bus;
+       struct occ_bus_ops bus_ops;
+       struct occ_ops ops;
+       struct occ_config config;
+       unsigned long update_interval;
+       unsigned long last_updated;
+       struct mutex update_lock;
+       struct occ_response response;
+       bool valid;
+};
+
+static void deinit_occ_resp_buf(struct occ_response *resp)
+{
+       int i;
Quoted 8 lines

drivers/hwmon/occ/occ.c line 128

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+
+       if (!resp)
+               return;
+

This creates the impression that resp can ever be NULL, which is not the case.

Agreed. This might go away anyway if I follow your suggestions below about memory management.

Quoted 22 lines
Quoted 22 lines
drivers/hwmon/occ/occ.c new file +20 -0
+       if (!resp->data.blocks)
+               return;
+
+       for (i = 0; i < resp->header.sensor_block_num; ++i)
+               kfree(resp->data.blocks[i].sensors);
+
+       kfree(resp->data.blocks);
+/sensor_type_s
 
+       memset(resp, 0, sizeof(struct occ_response));
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+               resp->data.sensor_block_id[i] = -1;
+}
+
+static void *occ_get_sensor_by_type(struct occ_response *resp,
+                                   enum sensor_type t)
+{
+       if (!resp->data.blocks)
+               return NULL;
+
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+       if (resp->data.sensor_block_id[t] == -1)
+               return NULL;
+
+       return resp->data.blocks[resp->data.sensor_block_id[t]].sensors;
+}
+
+static int occ_check_sensor(struct occ *driver, u8 sensor_length,
+                           u8 sensor_num, enum sensor_type t, int

block)

Quoted 34 lines
Quoted 34 lines
drivers/hwmon/occ/occ.c new file +34 -0
+{
+       void *sensor;
+       int type_block_id;
+       struct occ_response *resp = &driver->response;
+
+       sensor = occ_get_sensor_by_type(resp, t);
+
+       /* empty sensor block, release older sensor data */
+       if (sensor_num == 0 || sensor_length == 0) {
+               kfree(sensor);
+               dev_err(driver->dev, "no sensor blocks available\n");
+               return -ENODATA;
+       }
+
+       type_block_id = resp->data.sensor_block_id[t];
+       if (!sensor || sensor_num !=
+           resp->data.blocks[type_block_id].header.sensor_num) {
+               kfree(sensor);
+               resp->data.blocks[block].sensors =
+                       driver->ops.alloc_sensor(t, sensor_num);
+               if (!resp->data.blocks[block].sensors)
+                       return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static int parse_occ_response(struct occ *driver, u8 *data,
+                             struct occ_response *resp)
+{
+       int b;
+       int s;
+       int rc;
+       int offset = SENSOR_BLOCK_OFFSET;
Quoted 8 lines

drivers/hwmon/occ/occ.c line 193

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       int sensor_type;
+       u8 sensor_block_num;
+       char sensor_type_string[5] = { 0 };

The only purpose of this variable seems to be to zero-terminate it to be able
to print it. Why not use length-limited print functions instead ?

Is there such a function to length limit to dev_err or dev_dbg? Otherwise I have to snprintf or strncpy into a string to pass anyway. I can drop the dev_dbg but I would like to print out the sensor string if it doesn't match any of the expected ones.

Quoted 10 lines

drivers/hwmon/occ/occ.c line 199

Quoted 7 lines
drivers/hwmon/occ/occ.c new file +6 -0
+       struct sensor_data_block_header *block;
+       struct device *dev = driver->dev;
+
+       /* check if the data is valid */
+       if (strncmp(&data[SENSOR_STR_OFFSET], "SENSOR", 6) != 0) {
+               dev_err(dev, "no SENSOR string in response\n");

Are those messages really necessary ? That can create a lot of logging

noise

Quoted 1 line

if the occ responds with bad data.

I will audit these, but I think it's limited to one error message logged in any one function in case of error. Is that acceptable? Otherwise it may be hard to tell where the error occurred.

Quoted 8 lines

drivers/hwmon/occ/occ.c line 204

Quoted 6 lines
drivers/hwmon/occ/occ.c new file +5 -0
+               rc = -ENODATA;
+               goto err;
+       }
+
+       sensor_block_num = data[SENSOR_BLOCK_NUM_OFFSET];
Quoted 4 lines

There is a lot of trust in assuming that this data was actually received. At the same time, there is a lot of error checking. Why check if the information in this field is valid if you don't check if it was received in the first place ?

If we get to parse_occ_response, we know we got some data back from the OCC (see occ_get_all). Here we are really checking the response is a valid one, which I think makes sense in the parsing function.

Quoted 18 lines
Quoted 13 lines
drivers/hwmon/occ/occ.c new file +11 -0
+       if (sensor_block_num == 0) {
+               dev_err(dev, "no sensor blocks available\n");
+               rc = -ENODATA;
+               goto err;
+       }
+
+       /* if number of sensor block has changed, re-malloc */
+       if (sensor_block_num != resp->header.sensor_block_num) {
+               deinit_occ_resp_buf(resp);
+               resp->data.blocks = kcalloc(sensor_block_num,
+                                           sizeof(struct

sensor_data_block),

drivers/hwmon/occ/occ.c line 218

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+                                           GFP_KERNEL);
+               if (!resp->data.blocks)
+                       return -ENOMEM;
Quoted 1 line

Why not just allocate the maximum number of sensors once ? That might

waste

Quoted 1 line

a bit of memory, but it would make the code much less error prone. We

know

Quoted 2 lines

that
there will never be more sensors than the number fitting into 4k of

memory,

Quoted 1 line

so the "waste" would not really be that much.

Fair point. I'll do that.

Quoted 15 lines
Quoted 5 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       }
+
+       memcpy(&resp->header, &data[RESP_HEADER_OFFSET],

drivers/hwmon/occ/occ.c

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+              sizeof(struct occ_poll_header));
+       resp->header.error_log_addr_start =
+               be32_to_cpu(resp->header.error_log_addr_start);
+       resp->header.error_log_length =
+               be16_to_cpu(resp->header.error_log_length);
+
+       dev_dbg(dev, "Reading %d sensor blocks\n",
+               resp->header.sensor_block_num);
+       for (b = 0; b < sensor_block_num; b++) {
+               block = (struct sensor_data_block_header *)&data

[offset];

Quoted 16 lines
Quoted 16 lines
drivers/hwmon/occ/occ.c new file +5 -0
+               /* copy to a null terminated string */
+               strncpy(sensor_type_string, block->sensor_type, 4);
+               offset += 8;
+
+               dev_dbg(dev, "sensor block[%d]: type: %s, sensor_num:

%d\n", b,

drivers/hwmon/occ/occ.c new file +10 -0
+                       sensor_type_string, block->sensor_num);
+
+               if (strncmp(block->sensor_type, "FREQ", 4) == 0)
+                       sensor_type = FREQ;
+               else if (strncmp(block->sensor_type, "TEMP", 4) == 0)
+                       sensor_type = TEMP;
+               else if (strncmp(block->sensor_type, "POWR", 4) == 0)
+                       sensor_type = POWER;
+               else if (strncmp(block->sensor_type, "CAPS", 4) == 0)
+                       sensor_type = CAPS;
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+               else {
+                       dev_err(dev, "sensor type not supported %s\n",
+                               sensor_type_string);
+                       continue;
+               }
+
+               rc = occ_check_sensor(driver, block->sensor_length,
+                                     block->sensor_num, sensor_type,

b);

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +9 -0
+               if (rc == -ENOMEM)
+                       goto err;
+               else if (rc)
+                       continue;
+
+               resp->data.sensor_block_id[sensor_type] = b;
+               for (s = 0; s < block->sensor_num; s++) {
+                       driver->ops.parse_sensor(data,
+

resp->data.blocks[b].sensors,
+ sensor_type, offset,

s);

Quoted 33 lines
Quoted 33 lines
drivers/hwmon/occ/occ.c new file +33 -0
+                       offset += block->sensor_length;
+               }
+
+               /* copy block data over to response pointer */
+               resp->data.blocks[b].header = *block;
+       }
+
+       return 0;
+err:
+       deinit_occ_resp_buf(resp);
+       return rc;
+}
+
+static u8 occ_send_cmd(struct occ *driver, u8 seq, u8 type, u16 length,
+                      const u8 *data, u8 *resp)
+{
+       u32 cmd1, cmd2;
+       u16 checksum = 0;
+       u16 length_le = cpu_to_le16(length);
+       bool retry = 0;
+       int i, rc, tries = 0;
+
+       cmd1 = (seq << 24) | (type << 16) | length_le;
+       memcpy(&cmd2, data, length);
+       cmd2 <<= ((4 - length) * 8);
+
+       /* checksum: sum of every bytes of cmd1, cmd2 */
+       for (i = 0; i < 4; i++) {
+               checksum += (cmd1 >> (i * 8)) & 0xFF;
+               checksum += (cmd2 >> (i * 8)) & 0xFF;
+       }
+
+       cmd2 |= checksum << ((2 - length) * 8);
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+
+       /* Init OCB */
+       rc = driver->bus_ops.putscom(driver->bus, OCB_STATUS_CONTROL_OR,
+                                    OCB_OR_INIT0, OCB_OR_INIT1);
+       if (rc)
+               goto err;
+
+       rc = driver->bus_ops.putscom(driver->bus,

OCB_STATUS_CONTROL_AND,

Quoted 29 lines
Quoted 29 lines
drivers/hwmon/occ/occ.c new file +5 -0
+                                    OCB_AND_INIT0, OCB_AND_INIT1);
+       if (rc)
+               goto err;
+
+       /* Send command, 2nd half of the 64-bit addr is unused (write 0)

*/

drivers/hwmon/occ/occ.c new file +11 -0
+       rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+                                    driver->config.command_addr, 0);
+       if (rc)
+               goto err;
+
+       rc = driver->bus_ops.putscom(driver->bus, OCB_DATA, cmd1, cmd2);
+       if (rc)
+               goto err;
+
+       /* Trigger attention */
+       rc = driver->bus_ops.putscom(driver->bus, ATTN_DATA, ATTN0,

ATTN1);

drivers/hwmon/occ/occ.c new file +11 -0
+       if (rc)
+               goto err;
+
+       /* Get response data */
+       rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+                                    driver->config.response_addr, 0);
+       if (rc)
+               goto err;
+
+       do {
+               if (retry) {
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +2 -0
+                       set_current_state(TASK_INTERRUPTIBLE);
+

schedule_timeout(msecs_to_jiffies(CMD_IN_PRG_INT_MS));

drivers/hwmon/occ/occ.c new file +2 -0
+               }
+

Isn't there a better way to do this ? What exactly is interrupted ? The getscom function ? If so, shouldn't the timeout be handled there ?

There shouldn't be any interrupt. I set TASK_INTERRUPTIBLE because it doesn't really matter if it's interrupted or not. The purpose of this loop is to retry if we get the "command in progress" status from the OCC. It's higher level than the getscom function, so I think the loop is appropriate here.

Quoted 7 lines

drivers/hwmon/occ/occ.c

Quoted 7 lines
drivers/hwmon/occ/occ.c new file +6 -0
+               rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+                                            (u64 *)resp);
+               if (rc)
+                       goto err;
+
+               /* retry if we get "command in progress" return status

*/

Quoted 30 lines
Quoted 3 lines

+ retry = (resp[RESP_RETURN_STATUS] ==
RESP_RETURN_CMD_IN_PRG) &&
+ (tries++ < CMD_IN_PRG_RETRIES);

There are some unnecessary () in this expression.

Quoted 22 lines
drivers/hwmon/occ/occ.c new file +22 -0
+       } while (retry);
+
+       switch (resp[RESP_RETURN_STATUS]) {
+       case RESP_RETURN_CMD_IN_PRG:
+               rc = -EALREADY;
+               break;
+       case RESP_RETURN_SUCCESS:
+               rc = 0;
+               break;
+       case RESP_RETURN_CMD_INVAL:
+       case RESP_RETURN_CMD_LEN:
+       case RESP_RETURN_DATA_INVAL:
+       case RESP_RETURN_CHKSUM:
+               rc = -EINVAL;
+               break;
+       case RESP_RETURN_OCC_ERR:
+               rc = -EREMOTE;
+               break;
+       default:
+               rc = -EFAULT;
+       }
+
Quoted 8 lines

drivers/hwmon/occ/occ.c line 370

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       return rc;
+
+err:
+       dev_err(driver->dev, "scom op failed rc:%d\n", rc);

Some of the errors result in an error message, some don't. What is the guiding principle ? Even if there is an error message, it doesn't

report

Quoted 2 lines

what function caused it, which makes it as good as no error message. Can this be dropped ?

resp[RESP_RETURN_STATUS] is the status reported on the OCC, these aren't necessarily errors, though most are. This is translating an OCC status code to an error for occ_get_all to pick up. It cannot be dropped, we need to know if the occ poll was successful. I'm not sure what you mean by reporting the function? occ_get_all will fail out if we report the error here. If it's the scom op that failed we will have the dev_err message.

Quoted 18 lines
Quoted 4 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       return rc;
+}
+

drivers/hwmon/occ/occ.c line 383

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+static int occ_get_all(struct occ *driver)
+{
+       int i = 0, rc;
+       u8 *occ_data;
+       u16 num_bytes;
+       const u8 poll_cmd_data = OCC_POLL_STAT_SENSOR;
+       struct device *dev = driver->dev;
+       struct occ_response *resp = &driver->response;
+
+       occ_data = devm_kzalloc(dev, OCC_DATA_MAX, GFP_KERNEL);

I am a bot lost here. What is the purpose of using a devm function ?

Quoted 5 lines

Either case, you might want to allocate the memory once and just keep it allocated.
Allocating a 4k buffer and freeing it each time data is read from the occ seems
to be wasteful and not really worth it.

Agreed.

Quoted 17 lines

drivers/hwmon/occ/occ.c line 394

Quoted 13 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       if (!occ_data)
+               return -ENOMEM;
+
+       rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data,

occ_data);

drivers/hwmon/occ/occ.c new file +7 -0
+       if (rc) {
+               dev_err(dev, "OCC poll failed: %d\n", rc);
+               goto out;
+       }
+
+       num_bytes = get_unaligned((u16 *)&occ_data[RESP_DATA_LENGTH]);
+       num_bytes = be16_to_cpu(num_bytes);

What data is in big endian, and what data is in little endian ?

Quoted 3 lines

Looks like everything is big endian except for the length in the send command.
Is that correct ? If so, an explanation would be useful, because the

logic

Quoted 1 line

isn't entirely clear.

I'll add some comments.

Quoted 13 lines

drivers/hwmon/occ/occ.c line 403

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +9 -0
+       dev_dbg(dev, "OCC data length: %d\n", num_bytes);
+
+       if (num_bytes > OCC_DATA_MAX) {
+               dev_err(dev, "OCC data length must be < 4KB\n");
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (num_bytes <= 0) {

This can never be < 0.

Good point.

Quoted 15 lines
Quoted 15 lines
drivers/hwmon/occ/occ.c new file +14 -0
+               dev_err(dev, "OCC data length is zero\n");
+               rc = -EINVAL;
+               goto out;
+       }
+
+       /* read remaining data */
+       for (i = 8; i < num_bytes + 8; i += 8) {
+               rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+                                            (u64 *)&occ_data[i]);
+               if (rc) {
+                       dev_err(dev, "scom op failed rc:%d\n", rc);
+                       goto out;
+               }
+       }
Quoted 8 lines

drivers/hwmon/occ/occ.c line 421

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +2 -0
+
+       /* don't need more sanity checks; buffer is alloc'd for max

response

drivers/hwmon/occ/occ.c new file +2 -0
+        * size so we just check for valid data in parse_occ_response
+        */

Yes, but as mentioned above you don't check in that function if the data was actually received.

If we don't receive data, the data length will be 0 or the low level getscom will fail. So we know we have data here.

Quoted 22 lines
Quoted 16 lines
drivers/hwmon/occ/occ.c new file +14 -0
+       rc = parse_occ_response(driver, occ_data, resp);
+
+out:
+       devm_kfree(dev, occ_data);
+       return rc;
+}
+
+int occ_update_device(struct occ *driver)
+{
+       int rc = 0;
+
+       mutex_lock(&driver->update_lock);
+
+       if (time_after(jiffies, driver->last_updated +

driver->update_interval)

drivers/hwmon/occ/occ.c line 437

Quoted 2 lines
drivers/hwmon/occ/occ.c new file +2 -0
+           || !driver->valid) {
+               driver->valid = 1;

valid is bool.

Quoted 7 lines

drivers/hwmon/occ/occ.c line 441

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+
+               rc = occ_get_all(driver);
+               if (rc)
+                       driver->valid = 0;

valid is bool.

Good point.

Quoted 17 lines
Quoted 17 lines
drivers/hwmon/occ/occ.c new file +16 -0
+
+               driver->last_updated = jiffies;
+       }
+
+       mutex_unlock(&driver->update_lock);
+
+       return rc;
+}
+EXPORT_SYMBOL(occ_update_device);
+
+void *occ_get_sensor(struct occ *driver, int sensor_type)
+{
+       int rc;
+
+       /* occ_update_device locks the update lock */
+       rc = occ_update_device(driver);
Quoted 8 lines

drivers/hwmon/occ/occ.c line 460

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       if (rc) {
+               dev_err(driver->dev, "cannot get occ sensor data: %d\n",
+                       rc);

Have you counted the number of error messages reported if anything goes wrong.
This driver is quite noisy. I won't force you to drop the messages, but

it

Quoted 4 lines

seems
to me that if anything goes wrong, the kernel log will be clogged with messages
just from this driver.

I'll audit these.

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +8 -0
+               return NULL;
+       }
+
+       return occ_get_sensor_by_type(&driver->response, sensor_type);
+}
+EXPORT_SYMBOL(occ_get_sensor);
+
+int occ_get_sensor_value(struct occ *occ, int sensor_type, int

sensor_num,

Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +3 -0
+                        u32 hwmon, long *val)
+{
+       return occ->ops.get_sensor(occ, sensor_type, sensor_num, hwmon,

val);

drivers/hwmon/occ/occ.c new file +4 -0
+}
+EXPORT_SYMBOL(occ_get_sensor_value);
+
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks

**blocks)

Quoted 36 lines
Quoted 36 lines
drivers/hwmon/occ/occ.c new file +18 -0
+{
+       *blocks = &occ->response.data;
+}
+EXPORT_SYMBOL(occ_get_response_blocks);
+
+void occ_set_update_interval(struct occ *occ, unsigned long interval)
+{
+       occ->update_interval = msecs_to_jiffies(interval);
+}
+EXPORT_SYMBOL(occ_set_update_interval);
+
+int occ_set_user_powercap(struct occ *occ, u16 cap)
+{
+       u8 resp[8];
+
+       cap = cpu_to_be16(cap);
+
+       return occ_send_cmd(occ, 0, OCC_SET_USER_POWR_CAP, 2, (const u8

*)&cap,

drivers/hwmon/occ/occ.c new file +6 -0
+                           resp);
+}
+EXPORT_SYMBOL(occ_set_user_powercap);
+
+struct occ *occ_start(struct device *dev, void *bus,
+                     struct occ_bus_ops *bus_ops, const struct occ_ops

*ops,

drivers/hwmon/occ/occ.c new file +3 -0
+                     const struct occ_config *config)
+{
+       struct occ *driver = devm_kzalloc(dev, sizeof(struct occ),

GFP_KERNEL);

drivers/hwmon/occ/occ.c new file +6 -0
+
+       if (!driver)
+               return ERR_PTR(-ENOMEM);
+
+       driver->dev = dev;
+       driver->bus = bus;
Quoted 8 lines

drivers/hwmon/occ/occ.c line 513

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+       driver->bus_ops = *bus_ops;
+       driver->ops = *ops;
+       driver->config = *config;
+
+       driver->update_interval = HZ;

The update interval is only supposed to be configurable if it can be

written

Quoted 5 lines

into the hardware. Otherwise it is really up to user space to decide how often
it wants to poll the data, not up to the kernel.

I don't see a function to send the update interval to the occ. As such,

it

Quoted 2 lines

should
not be configurable but a constant.

I don't quite follow. Right now, the driver maximum polling rate is configurable by userspace (via hwmon sysfs attribute). If you mean that user space should control that rate just by how often users read the attributes, I'm not sure that will work as there are a large number of attributes that will all try and trigger an occ poll when read. Besides, different users don't want to communicate about when they last polled. If the rate is constant, if may be too slow or too fast depending on the system and needs of the users. Why can't the polling rate be configurable? To user space, the update interval works the same if it's driver or hardware.

Quoted 13 lines
Quoted 13 lines
drivers/hwmon/occ/occ.c new file +9 -0
+       mutex_init(&driver->update_lock);
+
+       return driver;
+}
+EXPORT_SYMBOL(occ_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC hwmon core driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ.h new file +0 -0
new file mode 100644
Quoted 8 lines

drivers/hwmon/occ/occ.h line 4

Quoted 8 lines

index 0000000..c86b06a

drivers/hwmon/occ/occ.h new file +4 -0
@@ -0,0 +1,81 @@
+/*
+ * occ.h - hwmon OCC driver
+ *
+ * This file contains data structures and function prototypes for

common

Quoted 42 lines
Quoted 42 lines

access
+ * between different bus protocols and host systems.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ.h new file +27 -0
+
+#ifndef __OCC_H__
+#define __OCC_H__
+
+#include "scom.h"
+
+struct device;
+struct occ;
+
+/* sensor_data_block_header
+ * structure to match the raw occ sensor block header
+ */
+struct sensor_data_block_header {
+       u8 sensor_type[4];
+       u8 reserved0;
+       u8 sensor_format;
+       u8 sensor_length;
+       u8 sensor_num;
+} __attribute__((packed, aligned(4)));
+
+struct sensor_data_block {
+       struct sensor_data_block_header header;
+       void *sensors;
+};
+
+enum sensor_type {
+       FREQ = 0,
Quoted 8 lines

drivers/hwmon/occ/occ.h

Quoted 8 lines
drivers/hwmon/occ/occ.h new file +8 -0
+       TEMP,
+       POWER,
+       CAPS,
+       MAX_OCC_SENSOR_TYPE
+};
+
+struct occ_ops {
+       void (*parse_sensor)(u8 *data, void *sensor, int sensor_type,

int

Quoted 54 lines
Quoted 54 lines

off,

drivers/hwmon/occ/occ.h new file +3 -0
+                            int snum);
+       void *(*alloc_sensor)(int sensor_type, int num_sensors);
+       int (*get_sensor)(struct occ *driver, int sensor_type, int

sensor_num,

drivers/hwmon/occ/occ.h new file +15 -0
+                         u32 hwmon, long *val);
+};
+
+struct occ_config {
+       u32 command_addr;
+       u32 response_addr;
+};
+
+struct occ_blocks {
+       int sensor_block_id[MAX_OCC_SENSOR_TYPE];
+       struct sensor_data_block *blocks;
+};
+
+struct occ *occ_start(struct device *dev, void *bus,
+                     struct occ_bus_ops *bus_ops, const struct occ_ops

*ops,

drivers/hwmon/occ/occ.h new file +3 -0
+                     const struct occ_config *config);
+void *occ_get_sensor(struct occ *occ, int sensor_type);
+int occ_get_sensor_value(struct occ *occ, int sensor_type, int

sensor_num,

drivers/hwmon/occ/occ.h new file +2 -0
+                        u32 hwmon, long *val);
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks

**blocks);

drivers/hwmon/occ/occ.h new file +5 -0
+int occ_update_device(struct occ *driver);
+void occ_set_update_interval(struct occ *occ, unsigned long interval);
+int occ_set_user_powercap(struct occ *occ, u16 cap);
+
+#endif /* __OCC_H__ */
drivers/hwmon/occ/scom.h new file +15 -0
new file mode 100644
index 0000000..c1da645
@@ -0,0 +1,47 @@
+/*
+ * scom.h - hwmon OCC driver
+ *
+ * This file contains data structures for scom operations to the OCC
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Quoted 8 lines

drivers/hwmon/occ/scom.h

Quoted 8 lines
drivers/hwmon/occ/scom.h new file +8 -0
+ * GNU General Public License for more details.
+ */
+
+#ifndef __SCOM_H__
+#define __SCOM_H__
+
+/*
+ * occ_bus_ops - represent the low-level transfer methods to

communicate

Quoted 19 lines
Quoted 9 lines

with
+ * the OCC.
+ *
+ * getscom - OCC scom read
+ * @bus: handle to slave device
+ * @address: address
+ * @data: where to store data read from slave; buffer size must be at least
+ * eight bytes.

Quoted 8 lines

+ *
+ * Returns 0 on success or a negative errno on error
+ *
+ * putscom - OCC scom write
+ * @bus: handle to slave device
+ * @address: address
+ * @data0: first data byte to write
+ * @data1: second data byte to write

Quoted 2 lines

32 bit bytes ? It might also be worthwhile to mention the expected byte order.

Good point.

Quoted 13 lines
Quoted 12 lines

+ *
+ * Returns 0 on success or a negative errno on error
+ */

drivers/hwmon/occ/scom.h new file +6 -0
+struct occ_bus_ops {
+       int (*getscom)(void *bus, u32 address, u64 *data);
+       int (*putscom)(void *bus, u32 address, u32 data0, u32 data1);
+};
+
+#endif /* __SCOM_H__ */
maintainer
#4 lore raw

Re: [PATCH linux v3 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

Quoted 382 lines · Edward James

I still just can't get gmail to reply to this. Thanks for the review.

Quoted 5 lines · Guenter Roeck
Quoted 4 lines · On 01/16/2017 01:13 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add core support for polling the OCC for it's sensor data and parsing

that

Quoted 51 lines
Quoted 51 lines

data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 522
+++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  81 +++++++
 drivers/hwmon/occ/scom.h   |  47 ++++
 9 files changed, 716 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +8 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9

processor

Documentation/hwmon/occ new file +8 -0
+(see the BMC - Host Communications section).
+
+Author: Eddie James <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on the

IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The

Documentation/hwmon/occ new file +2 -0
+OCC is an embedded processor that provides real time power and thermal
+monitoring.
Quoted 8 lines
Quoted 8 lines
Documentation/hwmon/occ new file +2 -0
+
+This driver provides an interface on a BMC to poll OCC sensor data, set

user

Documentation/hwmon/occ new file +3 -0
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor data:

power,
+temperature, frequency, and "caps," which indicate limits and

thresholds

Quoted 7 lines

Documentation/hwmon/occ

Quoted 7 lines

used

Documentation/hwmon/occ new file +6 -0
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8 over

I2C

Quoted 4 lines
Quoted 4 lines

bus.
+However, to access the OCC register space, any data transfer must use a SCOM
+operation. SCOM is a procedure to initiate a data transfer, typically

of

Quoted 6 lines

Documentation/hwmon/occ

Quoted 6 lines

8

Documentation/hwmon/occ new file +5 -0
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements these
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9 over

FSI

Quoted 30 lines
Quoted 30 lines

bus
+and SBE engine. Once again, SCOM operations are required. This driver will

Documentation/hwmon/occ new file +1 -0
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +7 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:       git git://linuxtv.org/media_tree.git
 S:     Maintained
 F:     drivers/media/i2c/ov7670.c
 
+ON-CHIP CONTROLLER HWMON DRIVER
+M:     Eddie James <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     Documentation/hwmon/occ
+F:     drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
 M:     Kyungmin Park <[email protected]>
 L:     [email protected]
drivers/hwmon/Kconfig +0 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
          This driver can also be built as a module. If so, the module
          will be called nsa320-hwmon.
 
Quoted 10 lines

drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_WM831X) +=

Quoted 10 lines
drivers/hwmon/Kconfig +2 -0
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
        tristate "Philips PCF8591 ADC/DAC"
        depends on I2C
drivers/hwmon/Makefile +0 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)        +=

wm831x-hwmon.o

Quoted 166 lines
Quoted 166 lines
drivers/hwmon/Makefile +1 -0
 obj-$(CONFIG_SENSORS_WM8350)   += wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)    += xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)  += occ/
 obj-$(CONFIG_PMBUS)            += pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +15 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
+
+menuconfig SENSORS_PPC_OCC
+       bool "PPC On-Chip Controller"
+       help
+         If you say yes here you get support to monitor Power CPU
+         sensors via the On-Chip Controller (OCC).
+
+         Generally this is used by management controllers such as a BMC
+         on an OpenPower system.
+
+         This driver can also be built as a module. If so, the module
+         will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +4 -0
new file mode 100644
index 0000000..3089762
@@ -0,0 +1,522 @@
+/*
+ * occ.c - OCC hwmon driver
+ *
+ * This file contains the methods and data structures for the OCC hwmon

driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ.c new file +107 -0
+
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+
+#define OCC_DATA_MAX           4096
+#define OCC_BMC_TIMEOUT_MS     20000
+
+/* To generate attn to OCC */
+#define ATTN_DATA              0x0006B035
+
+/* For BMC to read/write SRAM */
+#define OCB_ADDRESS            0x0006B070
+#define OCB_DATA               0x0006B075
+#define OCB_STATUS_CONTROL_AND 0x0006B072
+#define OCB_STATUS_CONTROL_OR  0x0006B073
+
+/* To init OCB */
+#define OCB_AND_INIT0          0xFBFFFFFF
+#define OCB_AND_INIT1          0xFFFFFFFF
+#define OCB_OR_INIT0           0x08000000
+#define OCB_OR_INIT1           0x00000000
+
+/* To generate attention on OCC */
+#define ATTN0                  0x01010000
+#define ATTN1                  0x00000000
+
+/* OCC return status */
+#define RESP_RETURN_CMD_IN_PRG 0xFF
+#define RESP_RETURN_SUCCESS    0
+#define RESP_RETURN_CMD_INVAL  0x11
+#define RESP_RETURN_CMD_LEN    0x12
+#define RESP_RETURN_DATA_INVAL 0x13
+#define RESP_RETURN_CHKSUM     0x14
+#define RESP_RETURN_OCC_ERR    0x15
+#define RESP_RETURN_STATE      0x16
+
+/* time interval to retry on "command in progress" return status */
+#define CMD_IN_PRG_INT_MS      100
+#define CMD_IN_PRG_RETRIES     (OCC_BMC_TIMEOUT_MS / CMD_IN_PRG_INT_MS)
+
+/* OCC command definitions */
+#define OCC_POLL               0
+#define OCC_SET_USER_POWR_CAP  0x22
+
+/* OCC poll command data */
+#define OCC_POLL_STAT_SENSOR   0x10
+
+/* OCC response data offsets */
+#define RESP_RETURN_STATUS     2
+#define RESP_DATA_LENGTH       3
+#define RESP_HEADER_OFFSET     5
+#define SENSOR_STR_OFFSET      37
+#define SENSOR_BLOCK_NUM_OFFSET        43
+#define SENSOR_BLOCK_OFFSET    45
+
+/* occ_poll_header
+ * structure to match the raw occ poll response data
+ */
+struct occ_poll_header {
+       u8 status;
+       u8 ext_status;
+       u8 occs_present;
+       u8 config;
+       u8 occ_state;
+       u8 mode;
+       u8 ips_status;
+       u8 error_log_id;
+       u32 error_log_addr_start;
+       u16 error_log_length;
+       u8 reserved2;
+       u8 reserved3;
+       u8 occ_code_level[16];
+       u8 sensor_eye_catcher[6];
+       u8 sensor_block_num;
+       u8 sensor_data_version;
+} __attribute__((packed, aligned(4)));
+
+struct occ_response {
+       struct occ_poll_header header;
+       struct occ_blocks data;
+};
+
+struct occ {
+       struct device *dev;
+       void *bus;
+       struct occ_bus_ops bus_ops;
+       struct occ_ops ops;
+       struct occ_config config;
+       unsigned long update_interval;
+       unsigned long last_updated;
+       struct mutex update_lock;
+       struct occ_response response;
+       bool valid;
+};
+
+static void deinit_occ_resp_buf(struct occ_response *resp)
+{
+       int i;
Quoted 8 lines

drivers/hwmon/occ/occ.c line 128

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+
+       if (!resp)
+               return;
+

This creates the impression that resp can ever be NULL, which is not the case.

Agreed. This might go away anyway if I follow your suggestions below about memory management.

Quoted 22 lines
Quoted 22 lines
drivers/hwmon/occ/occ.c new file +20 -0
+       if (!resp->data.blocks)
+               return;
+
+       for (i = 0; i < resp->header.sensor_block_num; ++i)
+               kfree(resp->data.blocks[i].sensors);
+
+       kfree(resp->data.blocks);
+/sensor_type_s
 
+       memset(resp, 0, sizeof(struct occ_response));
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+               resp->data.sensor_block_id[i] = -1;
+}
+
+static void *occ_get_sensor_by_type(struct occ_response *resp,
+                                   enum sensor_type t)
+{
+       if (!resp->data.blocks)
+               return NULL;
+
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+       if (resp->data.sensor_block_id[t] == -1)
+               return NULL;
+
+       return resp->data.blocks[resp->data.sensor_block_id[t]].sensors;
+}
+
+static int occ_check_sensor(struct occ *driver, u8 sensor_length,
+                           u8 sensor_num, enum sensor_type t, int

block)

Quoted 34 lines
Quoted 34 lines
drivers/hwmon/occ/occ.c new file +34 -0
+{
+       void *sensor;
+       int type_block_id;
+       struct occ_response *resp = &driver->response;
+
+       sensor = occ_get_sensor_by_type(resp, t);
+
+       /* empty sensor block, release older sensor data */
+       if (sensor_num == 0 || sensor_length == 0) {
+               kfree(sensor);
+               dev_err(driver->dev, "no sensor blocks available\n");
+               return -ENODATA;
+       }
+
+       type_block_id = resp->data.sensor_block_id[t];
+       if (!sensor || sensor_num !=
+           resp->data.blocks[type_block_id].header.sensor_num) {
+               kfree(sensor);
+               resp->data.blocks[block].sensors =
+                       driver->ops.alloc_sensor(t, sensor_num);
+               if (!resp->data.blocks[block].sensors)
+                       return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static int parse_occ_response(struct occ *driver, u8 *data,
+                             struct occ_response *resp)
+{
+       int b;
+       int s;
+       int rc;
+       int offset = SENSOR_BLOCK_OFFSET;
Quoted 8 lines

drivers/hwmon/occ/occ.c line 193

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       int sensor_type;
+       u8 sensor_block_num;
+       char sensor_type_string[5] = { 0 };

The only purpose of this variable seems to be to zero-terminate it to be able
to print it. Why not use length-limited print functions instead ?

Quoted 5 lines

Is there such a function to length limit to dev_err or dev_dbg? Otherwise I have to snprintf or strncpy into a string to pass anyway. I can drop the dev_dbg but I would like to print out the sensor string if it doesn't match any of the expected ones.

%*s or %-*s ?

Quoted 25 lines
Quoted 10 lines

drivers/hwmon/occ/occ.c line 199

Quoted 7 lines
drivers/hwmon/occ/occ.c new file +6 -0
+       struct sensor_data_block_header *block;
+       struct device *dev = driver->dev;
+
+       /* check if the data is valid */
+       if (strncmp(&data[SENSOR_STR_OFFSET], "SENSOR", 6) != 0) {
+               dev_err(dev, "no SENSOR string in response\n");

Are those messages really necessary ? That can create a lot of logging

noise

Quoted 1 line

if the occ responds with bad data.

I will audit these, but I think it's limited to one error message logged in any one function in case of error. Is that acceptable? Otherwise it may be hard to tell where the error occurred.

Quoted 8 lines
Quoted 6 lines
drivers/hwmon/occ/occ.c new file +5 -0
+               rc = -ENODATA;
+               goto err;
+       }
+
+       sensor_block_num = data[SENSOR_BLOCK_NUM_OFFSET];
Quoted 9 lines
Quoted 4 lines

There is a lot of trust in assuming that this data was actually received. At the same time, there is a lot of error checking. Why check if the information in this field is valid if you don't check if it was received in the first place ?

If we get to parse_occ_response, we know we got some data back from the OCC (see occ_get_all). Here we are really checking the response is a valid one, which I think makes sense in the parsing function.

Yes, but you don't check the length of the data vs. the length indicated in the header.

Quoted 162 lines
Quoted 18 lines
Quoted 13 lines
drivers/hwmon/occ/occ.c new file +11 -0
+       if (sensor_block_num == 0) {
+               dev_err(dev, "no sensor blocks available\n");
+               rc = -ENODATA;
+               goto err;
+       }
+
+       /* if number of sensor block has changed, re-malloc */
+       if (sensor_block_num != resp->header.sensor_block_num) {
+               deinit_occ_resp_buf(resp);
+               resp->data.blocks = kcalloc(sensor_block_num,
+                                           sizeof(struct

sensor_data_block),

drivers/hwmon/occ/occ.c line 218

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+                                           GFP_KERNEL);
+               if (!resp->data.blocks)
+                       return -ENOMEM;
Quoted 1 line

Why not just allocate the maximum number of sensors once ? That might

waste

Quoted 1 line

a bit of memory, but it would make the code much less error prone. We

know

Quoted 2 lines

that
there will never be more sensors than the number fitting into 4k of

memory,

Quoted 1 line

so the "waste" would not really be that much.

Fair point. I'll do that.

Quoted 15 lines
Quoted 5 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       }
+
+       memcpy(&resp->header, &data[RESP_HEADER_OFFSET],

drivers/hwmon/occ/occ.c

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+              sizeof(struct occ_poll_header));
+       resp->header.error_log_addr_start =
+               be32_to_cpu(resp->header.error_log_addr_start);
+       resp->header.error_log_length =
+               be16_to_cpu(resp->header.error_log_length);
+
+       dev_dbg(dev, "Reading %d sensor blocks\n",
+               resp->header.sensor_block_num);
+       for (b = 0; b < sensor_block_num; b++) {
+               block = (struct sensor_data_block_header *)&data

[offset];

Quoted 16 lines
Quoted 16 lines
drivers/hwmon/occ/occ.c new file +5 -0
+               /* copy to a null terminated string */
+               strncpy(sensor_type_string, block->sensor_type, 4);
+               offset += 8;
+
+               dev_dbg(dev, "sensor block[%d]: type: %s, sensor_num:

%d\n", b,

drivers/hwmon/occ/occ.c new file +10 -0
+                       sensor_type_string, block->sensor_num);
+
+               if (strncmp(block->sensor_type, "FREQ", 4) == 0)
+                       sensor_type = FREQ;
+               else if (strncmp(block->sensor_type, "TEMP", 4) == 0)
+                       sensor_type = TEMP;
+               else if (strncmp(block->sensor_type, "POWR", 4) == 0)
+                       sensor_type = POWER;
+               else if (strncmp(block->sensor_type, "CAPS", 4) == 0)
+                       sensor_type = CAPS;
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+               else {
+                       dev_err(dev, "sensor type not supported %s\n",
+                               sensor_type_string);
+                       continue;
+               }
+
+               rc = occ_check_sensor(driver, block->sensor_length,
+                                     block->sensor_num, sensor_type,

b);

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +9 -0
+               if (rc == -ENOMEM)
+                       goto err;
+               else if (rc)
+                       continue;
+
+               resp->data.sensor_block_id[sensor_type] = b;
+               for (s = 0; s < block->sensor_num; s++) {
+                       driver->ops.parse_sensor(data,
+

resp->data.blocks[b].sensors,
+ sensor_type, offset,

s);

Quoted 33 lines
Quoted 33 lines
drivers/hwmon/occ/occ.c new file +33 -0
+                       offset += block->sensor_length;
+               }
+
+               /* copy block data over to response pointer */
+               resp->data.blocks[b].header = *block;
+       }
+
+       return 0;
+err:
+       deinit_occ_resp_buf(resp);
+       return rc;
+}
+
+static u8 occ_send_cmd(struct occ *driver, u8 seq, u8 type, u16 length,
+                      const u8 *data, u8 *resp)
+{
+       u32 cmd1, cmd2;
+       u16 checksum = 0;
+       u16 length_le = cpu_to_le16(length);
+       bool retry = 0;
+       int i, rc, tries = 0;
+
+       cmd1 = (seq << 24) | (type << 16) | length_le;
+       memcpy(&cmd2, data, length);
+       cmd2 <<= ((4 - length) * 8);
+
+       /* checksum: sum of every bytes of cmd1, cmd2 */
+       for (i = 0; i < 4; i++) {
+               checksum += (cmd1 >> (i * 8)) & 0xFF;
+               checksum += (cmd2 >> (i * 8)) & 0xFF;
+       }
+
+       cmd2 |= checksum << ((2 - length) * 8);
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+
+       /* Init OCB */
+       rc = driver->bus_ops.putscom(driver->bus, OCB_STATUS_CONTROL_OR,
+                                    OCB_OR_INIT0, OCB_OR_INIT1);
+       if (rc)
+               goto err;
+
+       rc = driver->bus_ops.putscom(driver->bus,

OCB_STATUS_CONTROL_AND,

Quoted 29 lines
Quoted 29 lines
drivers/hwmon/occ/occ.c new file +5 -0
+                                    OCB_AND_INIT0, OCB_AND_INIT1);
+       if (rc)
+               goto err;
+
+       /* Send command, 2nd half of the 64-bit addr is unused (write 0)

*/

drivers/hwmon/occ/occ.c new file +11 -0
+       rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+                                    driver->config.command_addr, 0);
+       if (rc)
+               goto err;
+
+       rc = driver->bus_ops.putscom(driver->bus, OCB_DATA, cmd1, cmd2);
+       if (rc)
+               goto err;
+
+       /* Trigger attention */
+       rc = driver->bus_ops.putscom(driver->bus, ATTN_DATA, ATTN0,

ATTN1);

drivers/hwmon/occ/occ.c new file +11 -0
+       if (rc)
+               goto err;
+
+       /* Get response data */
+       rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+                                    driver->config.response_addr, 0);
+       if (rc)
+               goto err;
+
+       do {
+               if (retry) {
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +2 -0
+                       set_current_state(TASK_INTERRUPTIBLE);
+

schedule_timeout(msecs_to_jiffies(CMD_IN_PRG_INT_MS));

drivers/hwmon/occ/occ.c new file +2 -0
+               }
+

Isn't there a better way to do this ? What exactly is interrupted ? The getscom function ? If so, shouldn't the timeout be handled there ?

Quoted 6 lines

There shouldn't be any interrupt. I set TASK_INTERRUPTIBLE because it doesn't really matter if it's interrupted or not. The purpose of this loop is to retry if we get the "command in progress" status from the OCC. It's higher level than the getscom function, so I think the loop is appropriate here.

This is quite unusual. Why is this needed here but not elsewhere in the kernel ? Normally the called function would time out and return an appropriate error.

Quoted 47 lines
Quoted 7 lines

drivers/hwmon/occ/occ.c

Quoted 7 lines
drivers/hwmon/occ/occ.c new file +6 -0
+               rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+                                            (u64 *)resp);
+               if (rc)
+                       goto err;
+
+               /* retry if we get "command in progress" return status

*/

Quoted 30 lines
Quoted 3 lines

+ retry = (resp[RESP_RETURN_STATUS] ==
RESP_RETURN_CMD_IN_PRG) &&
+ (tries++ < CMD_IN_PRG_RETRIES);

There are some unnecessary () in this expression.

Quoted 22 lines
drivers/hwmon/occ/occ.c new file +22 -0
+       } while (retry);
+
+       switch (resp[RESP_RETURN_STATUS]) {
+       case RESP_RETURN_CMD_IN_PRG:
+               rc = -EALREADY;
+               break;
+       case RESP_RETURN_SUCCESS:
+               rc = 0;
+               break;
+       case RESP_RETURN_CMD_INVAL:
+       case RESP_RETURN_CMD_LEN:
+       case RESP_RETURN_DATA_INVAL:
+       case RESP_RETURN_CHKSUM:
+               rc = -EINVAL;
+               break;
+       case RESP_RETURN_OCC_ERR:
+               rc = -EREMOTE;
+               break;
+       default:
+               rc = -EFAULT;
+       }
+
Quoted 8 lines

drivers/hwmon/occ/occ.c line 370

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       return rc;
+
+err:
+       dev_err(driver->dev, "scom op failed rc:%d\n", rc);

Some of the errors result in an error message, some don't. What is the guiding principle ? Even if there is an error message, it doesn't

report

Quoted 8 lines
Quoted 2 lines

what function caused it, which makes it as good as no error message. Can this be dropped ?

resp[RESP_RETURN_STATUS] is the status reported on the OCC, these aren't necessarily errors, though most are. This is translating an OCC status code to an error for occ_get_all to pick up. It cannot be dropped, we need to know if the occ poll was successful. I'm not sure what you mean by reporting the function? occ_get_all will fail out if

There are several function returns which all end up here. You don't know which one actually failed.

Quoted 3 lines

we report the error here. If it's the scom op that failed we will have the dev_err message.

But which scom operation ? You mean it is important to report that some operation failed, but not which one ? Ok, I'll accept that, it just seems odd.

Quoted 91 lines
Quoted 18 lines
Quoted 4 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       return rc;
+}
+

drivers/hwmon/occ/occ.c line 383

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+static int occ_get_all(struct occ *driver)
+{
+       int i = 0, rc;
+       u8 *occ_data;
+       u16 num_bytes;
+       const u8 poll_cmd_data = OCC_POLL_STAT_SENSOR;
+       struct device *dev = driver->dev;
+       struct occ_response *resp = &driver->response;
+
+       occ_data = devm_kzalloc(dev, OCC_DATA_MAX, GFP_KERNEL);

I am a bot lost here. What is the purpose of using a devm function ?

Quoted 5 lines

Either case, you might want to allocate the memory once and just keep it allocated.
Allocating a 4k buffer and freeing it each time data is read from the occ seems
to be wasteful and not really worth it.

Agreed.

Quoted 17 lines

drivers/hwmon/occ/occ.c line 394

Quoted 13 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       if (!occ_data)
+               return -ENOMEM;
+
+       rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data,

occ_data);

drivers/hwmon/occ/occ.c new file +7 -0
+       if (rc) {
+               dev_err(dev, "OCC poll failed: %d\n", rc);
+               goto out;
+       }
+
+       num_bytes = get_unaligned((u16 *)&occ_data[RESP_DATA_LENGTH]);
+       num_bytes = be16_to_cpu(num_bytes);

What data is in big endian, and what data is in little endian ?

Quoted 3 lines

Looks like everything is big endian except for the length in the send command.
Is that correct ? If so, an explanation would be useful, because the

logic

Quoted 1 line

isn't entirely clear.

I'll add some comments.

Quoted 13 lines

drivers/hwmon/occ/occ.c line 403

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +9 -0
+       dev_dbg(dev, "OCC data length: %d\n", num_bytes);
+
+       if (num_bytes > OCC_DATA_MAX) {
+               dev_err(dev, "OCC data length must be < 4KB\n");
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (num_bytes <= 0) {

This can never be < 0.

Good point.

Quoted 15 lines
Quoted 15 lines
drivers/hwmon/occ/occ.c new file +14 -0
+               dev_err(dev, "OCC data length is zero\n");
+               rc = -EINVAL;
+               goto out;
+       }
+
+       /* read remaining data */
+       for (i = 8; i < num_bytes + 8; i += 8) {
+               rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+                                            (u64 *)&occ_data[i]);
+               if (rc) {
+                       dev_err(dev, "scom op failed rc:%d\n", rc);
+                       goto out;
+               }
+       }
Quoted 8 lines

drivers/hwmon/occ/occ.c line 421

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +2 -0
+
+       /* don't need more sanity checks; buffer is alloc'd for max

response

drivers/hwmon/occ/occ.c new file +2 -0
+        * size so we just check for valid data in parse_occ_response
+        */

Yes, but as mentioned above you don't check in that function if the data was actually received.

Quoted 3 lines

If we don't receive data, the data length will be 0 or the low level getscom will fail. So we know we have data here.

Ok. However, since you don't pass num_bytes to the parse function, there is no verification that num_bytes actually matches what is in the packet. The rest of the header could indicate that there is 4k of data, but num_bytes reports, say, the minimum size. In that case, parse_occ_response() would end up parsing a stale buffer.

Quoted 139 lines
Quoted 22 lines
Quoted 16 lines
drivers/hwmon/occ/occ.c new file +14 -0
+       rc = parse_occ_response(driver, occ_data, resp);
+
+out:
+       devm_kfree(dev, occ_data);
+       return rc;
+}
+
+int occ_update_device(struct occ *driver)
+{
+       int rc = 0;
+
+       mutex_lock(&driver->update_lock);
+
+       if (time_after(jiffies, driver->last_updated +

driver->update_interval)

drivers/hwmon/occ/occ.c line 437

Quoted 2 lines
drivers/hwmon/occ/occ.c new file +2 -0
+           || !driver->valid) {
+               driver->valid = 1;

valid is bool.

Quoted 7 lines

drivers/hwmon/occ/occ.c line 441

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+
+               rc = occ_get_all(driver);
+               if (rc)
+                       driver->valid = 0;

valid is bool.

Good point.

Quoted 17 lines
Quoted 17 lines
drivers/hwmon/occ/occ.c new file +16 -0
+
+               driver->last_updated = jiffies;
+       }
+
+       mutex_unlock(&driver->update_lock);
+
+       return rc;
+}
+EXPORT_SYMBOL(occ_update_device);
+
+void *occ_get_sensor(struct occ *driver, int sensor_type)
+{
+       int rc;
+
+       /* occ_update_device locks the update lock */
+       rc = occ_update_device(driver);
Quoted 8 lines

drivers/hwmon/occ/occ.c line 460

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       if (rc) {
+               dev_err(driver->dev, "cannot get occ sensor data: %d\n",
+                       rc);

Have you counted the number of error messages reported if anything goes wrong.
This driver is quite noisy. I won't force you to drop the messages, but

it

Quoted 4 lines

seems
to me that if anything goes wrong, the kernel log will be clogged with messages
just from this driver.

I'll audit these.

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +8 -0
+               return NULL;
+       }
+
+       return occ_get_sensor_by_type(&driver->response, sensor_type);
+}
+EXPORT_SYMBOL(occ_get_sensor);
+
+int occ_get_sensor_value(struct occ *occ, int sensor_type, int

sensor_num,

Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +3 -0
+                        u32 hwmon, long *val)
+{
+       return occ->ops.get_sensor(occ, sensor_type, sensor_num, hwmon,

val);

drivers/hwmon/occ/occ.c new file +4 -0
+}
+EXPORT_SYMBOL(occ_get_sensor_value);
+
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks

**blocks)

Quoted 36 lines
Quoted 36 lines
drivers/hwmon/occ/occ.c new file +18 -0
+{
+       *blocks = &occ->response.data;
+}
+EXPORT_SYMBOL(occ_get_response_blocks);
+
+void occ_set_update_interval(struct occ *occ, unsigned long interval)
+{
+       occ->update_interval = msecs_to_jiffies(interval);
+}
+EXPORT_SYMBOL(occ_set_update_interval);
+
+int occ_set_user_powercap(struct occ *occ, u16 cap)
+{
+       u8 resp[8];
+
+       cap = cpu_to_be16(cap);
+
+       return occ_send_cmd(occ, 0, OCC_SET_USER_POWR_CAP, 2, (const u8

*)&cap,

drivers/hwmon/occ/occ.c new file +6 -0
+                           resp);
+}
+EXPORT_SYMBOL(occ_set_user_powercap);
+
+struct occ *occ_start(struct device *dev, void *bus,
+                     struct occ_bus_ops *bus_ops, const struct occ_ops

*ops,

drivers/hwmon/occ/occ.c new file +3 -0
+                     const struct occ_config *config)
+{
+       struct occ *driver = devm_kzalloc(dev, sizeof(struct occ),

GFP_KERNEL);

drivers/hwmon/occ/occ.c new file +6 -0
+
+       if (!driver)
+               return ERR_PTR(-ENOMEM);
+
+       driver->dev = dev;
+       driver->bus = bus;
Quoted 8 lines

drivers/hwmon/occ/occ.c line 513

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+       driver->bus_ops = *bus_ops;
+       driver->ops = *ops;
+       driver->config = *config;
+
+       driver->update_interval = HZ;

The update interval is only supposed to be configurable if it can be

written

Quoted 5 lines

into the hardware. Otherwise it is really up to user space to decide how often
it wants to poll the data, not up to the kernel.

I don't see a function to send the update interval to the occ. As such,

it

Quoted 2 lines

should
not be configurable but a constant.

Quoted 11 lines

I don't quite follow. Right now, the driver maximum polling rate is configurable by userspace (via hwmon sysfs attribute). If you mean that user space should control that rate just by how often users read the attributes, I'm not sure that will work as there are a large number of attributes that will all try and trigger an occ poll when read. Besides, different users don't want to communicate about when they last polled. If the rate is constant, if may be too slow or too fast depending on the system and needs of the users. Why can't the polling rate be configurable? To user space, the update interval works the same if it's driver or hardware.

The update_interval attribute is only supposed to exist if writing it results in writing a value into a chip register. It is supposed to set the _chip_ update interval, not the kernel update interval. If you want to limit the rate of chip accesses, you can do it by using a constant in the driver, just like other drivers do it.

This is similar to limit registers. If the chip does not support limit registers, we don't make up "soft" limits either, but don't support the respective attributes.

Thanks,
Guenter

#5 lore raw

Re: [PATCH linux v3 1/6] hwmon: Add core On-Chip Controller support for POWER CPUs

Guenter Roeck <[email protected]> wrote on 01/23/2017 02:21:04 PM:

Quoted 5 lines · Edward James
Quoted 3 lines

I still just can't get gmail to reply to this. Thanks for the review.

On Sat, Jan 21, 2017 at 11:49 AM, Guenter Roeck <[email protected]>

Quoted 5 lines · wrote:
Quoted 5 lines
Quoted 5 lines · On 01/16/2017 01:13 PM, [email protected] wrote:
Quoted 4 lines

From: "Edward A. James" <[email protected]>

Add core support for polling the OCC for it's sensor data and

parsing

Quoted 39 lines
Quoted 39 lines

that

Quoted 38 lines
Quoted 38 lines

data into sensor-specific information.

---
 Documentation/hwmon/occ    |  40 ++++
 MAINTAINERS                |   7 +
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 522
+++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  81 +++++++
 drivers/hwmon/occ/scom.h   |  47 ++++
 9 files changed, 716 insertions(+)
 create mode 100644 Documentation/hwmon/occ
 create mode 100644 drivers/hwmon/occ/Kconfig
 create mode 100644 drivers/hwmon/occ/Makefile
 create mode 100644 drivers/hwmon/occ/occ.c
 create mode 100644 drivers/hwmon/occ/occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Documentation/hwmon/occ new file +8 -0
new file mode 100644
index 0000000..79d1642
@@ -0,0 +1,40 @@
+Kernel driver occ
+=================
+
+Supported chips:
+ * ASPEED AST2400
+ * ASPEED AST2500
+
+Please note that the chip must be connected to a POWER8 or POWER9

processor

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Documentation/hwmon/occ

Quoted 8 lines
Documentation/hwmon/occ new file +8 -0
+(see the BMC - Host Communications section).
+
+Author: Eddie James <[email protected]>
+
+Description
+-----------
+
+This driver implements support for the OCC (On-Chip Controller) on

the

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

IBM
+POWER8 and POWER9 processors, from a BMC (Baseboard Management Controller). The
+OCC is an embedded processor that provides real time power and

thermal

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Documentation/hwmon/occ

Quoted 3 lines
Documentation/hwmon/occ new file +3 -0
+monitoring.
+
+This driver provides an interface on a BMC to poll OCC sensor data,

set

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Documentation/hwmon/occ

Quoted 4 lines

user

Documentation/hwmon/occ new file +3 -0
+power caps, and perform some basic OCC error handling.
+
+Currently, all versions of the OCC support four types of sensor

data:

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines

power,
+temperature, frequency, and "caps," which indicate limits and

thresholds

Quoted 7 lines

Documentation/hwmon/occ

Quoted 7 lines

used

Documentation/hwmon/occ new file +6 -0
+internally on the OCC.
+
+BMC - Host Communications
+-------------------------
+
+For the POWER8 application, the BMC can communicate with the P8

over

Quoted 3 lines
Quoted 3 lines

I2C

Quoted 2 lines
Quoted 2 lines

bus.
+However, to access the OCC register space, any data transfer must

use a

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

SCOM
+operation. SCOM is a procedure to initiate a data transfer,

typically

Quoted 4 lines
Quoted 4 lines

of

Quoted 3 lines

Documentation/hwmon/occ

Quoted 3 lines

8

Documentation/hwmon/occ new file +2 -0
+bytes. SCOMs consist of writing a 32-bit command register and then
+reading/writing two 32-bit data registers. This driver implements

these

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Documentation/hwmon/occ

Quoted 3 lines
Documentation/hwmon/occ new file +3 -0
+SCOM operations over I2C bus in order to communicate with the OCC.
+
+For the POWER9 application, the BMC can communicate with the P9

over

Quoted 3 lines
Quoted 3 lines

FSI

Quoted 2 lines
Quoted 2 lines

bus
+and SBE engine. Once again, SCOM operations are required. This

driver

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

will

Documentation/hwmon/occ new file +1 -0
+implement SCOM ops over FSI/SBE. This will require the FSI driver.
MAINTAINERS +0 -0
index 5f0420a..f5d4195 100644
@@ -9112,6 +9112,13 @@ T:       git git://linuxtv.org/media_tree.git
 S:     Maintained
 F:     drivers/media/i2c/ov7670.c
 
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
MAINTAINERS +7 -0
+ON-CHIP CONTROLLER HWMON DRIVER
+M:     Eddie James <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     Documentation/hwmon/occ
+F:     drivers/hwmon/occ/
+
 ONENAND FLASH DRIVER
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/Kconfig config SENSORS_NSA320 · line 1240

Quoted 8 lines
MAINTAINERS +0 -0
 M:     Kyungmin Park <[email protected]>
 L:     [email protected]
drivers/hwmon/Kconfig +0 -0
index 190d270..e80ca81 100644
@@ -1240,6 +1240,8 @@ config SENSORS_NSA320
          This driver can also be built as a module. If so, the

module

Quoted 29 lines
Quoted 29 lines
Quoted 12 lines

drivers/hwmon/Makefile obj-$(CONFIG_SENSORS_WM831X) +=

Quoted 12 lines
drivers/hwmon/Kconfig +2 -0
          will be called nsa320-hwmon.
 
+source drivers/hwmon/occ/Kconfig
+
 config SENSORS_PCF8591
        tristate "Philips PCF8591 ADC/DAC"
        depends on I2C
drivers/hwmon/Makefile +0 -0
index d2cb7e8..c7ec5d4 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_SENSORS_WM831X)        +=

wm831x-hwmon.o

Quoted 16 lines
Quoted 16 lines
drivers/hwmon/Makefile +1 -0
 obj-$(CONFIG_SENSORS_WM8350)   += wm8350-hwmon.o
 obj-$(CONFIG_SENSORS_XGENE)    += xgene-hwmon.o
 
+obj-$(CONFIG_SENSORS_PPC_OCC)  += occ/
 obj-$(CONFIG_PMBUS)            += pmbus/
 
 ccflags-$(CONFIG_HWMON_DEBUG_CHIP) := -DDEBUG
drivers/hwmon/occ/Kconfig new file +3 -0
new file mode 100644
index 0000000..cdb64a7
@@ -0,0 +1,15 @@
+#
+# On Chip Controller configuration
+#
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/Kconfig

Quoted 8 lines
drivers/hwmon/occ/Kconfig new file +8 -0
+
+menuconfig SENSORS_PPC_OCC
+       bool "PPC On-Chip Controller"
+       help
+         If you say yes here you get support to monitor Power CPU
+         sensors via the On-Chip Controller (OCC).
+
+         Generally this is used by management controllers such as a

BMC

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/hwmon/occ/Kconfig

Quoted 3 lines
drivers/hwmon/occ/Kconfig new file +3 -0
+         on an OpenPower system.
+
+         This driver can also be built as a module. If so, the

module

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/hwmon/occ/Kconfig new file +1 -0
+         will be called occ.
drivers/hwmon/occ/Makefile new file +1 -0
new file mode 100644
index 0000000..93cb52f
@@ -0,0 +1 @@
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
drivers/hwmon/occ/occ.c new file +0 -0
new file mode 100644
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c line 4

Quoted 8 lines

index 0000000..3089762

drivers/hwmon/occ/occ.c new file +4 -0
@@ -0,0 +1,522 @@
+/*
+ * occ.c - OCC hwmon driver
+ *
+ * This file contains the methods and data structures for the OCC

hwmon

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * it under the terms of the GNU General Public License as

published by

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * the Free Software Foundation; either version 2 of the License,

or

Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines

+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ.c new file +40 -0
+
+#include <asm/unaligned.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+
+#define OCC_DATA_MAX           4096
+#define OCC_BMC_TIMEOUT_MS     20000
+
+/* To generate attn to OCC */
+#define ATTN_DATA              0x0006B035
+
+/* For BMC to read/write SRAM */
+#define OCB_ADDRESS            0x0006B070
+#define OCB_DATA               0x0006B075
+#define OCB_STATUS_CONTROL_AND 0x0006B072
+#define OCB_STATUS_CONTROL_OR  0x0006B073
+
+/* To init OCB */
+#define OCB_AND_INIT0          0xFBFFFFFF
+#define OCB_AND_INIT1          0xFFFFFFFF
+#define OCB_OR_INIT0           0x08000000
+#define OCB_OR_INIT1           0x00000000
+
+/* To generate attention on OCC */
+#define ATTN0                  0x01010000
+#define ATTN1                  0x00000000
+
+/* OCC return status */
+#define RESP_RETURN_CMD_IN_PRG 0xFF
+#define RESP_RETURN_SUCCESS    0
+#define RESP_RETURN_CMD_INVAL  0x11
+#define RESP_RETURN_CMD_LEN    0x12
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+#define RESP_RETURN_DATA_INVAL 0x13
+#define RESP_RETURN_CHKSUM     0x14
+#define RESP_RETURN_OCC_ERR    0x15
+#define RESP_RETURN_STATE      0x16
+
+/* time interval to retry on "command in progress" return status */
+#define CMD_IN_PRG_INT_MS      100
+#define CMD_IN_PRG_RETRIES     (OCC_BMC_TIMEOUT_MS /

CMD_IN_PRG_INT_MS)

Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
drivers/hwmon/occ/occ.c new file +58 -0
+
+/* OCC command definitions */
+#define OCC_POLL               0
+#define OCC_SET_USER_POWR_CAP  0x22
+
+/* OCC poll command data */
+#define OCC_POLL_STAT_SENSOR   0x10
+
+/* OCC response data offsets */
+#define RESP_RETURN_STATUS     2
+#define RESP_DATA_LENGTH       3
+#define RESP_HEADER_OFFSET     5
+#define SENSOR_STR_OFFSET      37
+#define SENSOR_BLOCK_NUM_OFFSET        43
+#define SENSOR_BLOCK_OFFSET    45
+
+/* occ_poll_header
+ * structure to match the raw occ poll response data
+ */
+struct occ_poll_header {
+       u8 status;
+       u8 ext_status;
+       u8 occs_present;
+       u8 config;
+       u8 occ_state;
+       u8 mode;
+       u8 ips_status;
+       u8 error_log_id;
+       u32 error_log_addr_start;
+       u16 error_log_length;
+       u8 reserved2;
+       u8 reserved3;
+       u8 occ_code_level[16];
+       u8 sensor_eye_catcher[6];
+       u8 sensor_block_num;
+       u8 sensor_data_version;
+} __attribute__((packed, aligned(4)));
+
+struct occ_response {
+       struct occ_poll_header header;
+       struct occ_blocks data;
+};
+
+struct occ {
+       struct device *dev;
+       void *bus;
+       struct occ_bus_ops bus_ops;
+       struct occ_ops ops;
+       struct occ_config config;
+       unsigned long update_interval;
+       unsigned long last_updated;
+       struct mutex update_lock;
+       struct occ_response response;
+       bool valid;
+};
+
+static void deinit_occ_resp_buf(struct occ_response *resp)
+{
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c line 128

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+       int i;
+
+       if (!resp)
+               return;
+

This creates the impression that resp can ever be NULL, which is not

the

Quoted 15 lines
Quoted 15 lines
Quoted 1 line

case.

Agreed. This might go away anyway if I follow your suggestions below about memory management.

Quoted 10 lines
Quoted 10 lines
drivers/hwmon/occ/occ.c new file +8 -0
+       if (!resp->data.blocks)
+               return;
+
+       for (i = 0; i < resp->header.sensor_block_num; ++i)
+               kfree(resp->data.blocks[i].sensors);
+
+       kfree(resp->data.blocks);
+/sensor_type_s
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+       memset(resp, 0, sizeof(struct occ_response));
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+               resp->data.sensor_block_id[i] = -1;
+}
+
+static void *occ_get_sensor_by_type(struct occ_response *resp,
+                                   enum sensor_type t)
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+{
+       if (!resp->data.blocks)
+               return NULL;
+
+       if (resp->data.sensor_block_id[t] == -1)
+               return NULL;
+
+       return resp->data.blocks[resp->data.sensor_block_id

[t]].sensors;

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

drivers/hwmon/occ/occ.c

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+}
+
+static int occ_check_sensor(struct occ *driver, u8 sensor_length,
+                           u8 sensor_num, enum sensor_type t, int

block)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/hwmon/occ/occ.c

Quoted 11 lines
drivers/hwmon/occ/occ.c new file +11 -0
+{
+       void *sensor;
+       int type_block_id;
+       struct occ_response *resp = &driver->response;
+
+       sensor = occ_get_sensor_by_type(resp, t);
+
+       /* empty sensor block, release older sensor data */
+       if (sensor_num == 0 || sensor_length == 0) {
+               kfree(sensor);
+               dev_err(driver->dev, "no sensor blocks available

\n");

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
drivers/hwmon/occ/occ.c new file +21 -0
+               return -ENODATA;
+       }
+
+       type_block_id = resp->data.sensor_block_id[t];
+       if (!sensor || sensor_num !=
+           resp->data.blocks[type_block_id].header.sensor_num) {
+               kfree(sensor);
+               resp->data.blocks[block].sensors =
+                       driver->ops.alloc_sensor(t, sensor_num);
+               if (!resp->data.blocks[block].sensors)
+                       return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static int parse_occ_response(struct occ *driver, u8 *data,
+                             struct occ_response *resp)
+{
+       int b;
+       int s;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c line 193

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+       int rc;
+       int offset = SENSOR_BLOCK_OFFSET;
+       int sensor_type;
+       u8 sensor_block_num;
+       char sensor_type_string[5] = { 0 };

The only purpose of this variable seems to be to zero-terminate it to

be

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

able
to print it. Why not use length-limited print functions instead ?

Is there such a function to length limit to dev_err or dev_dbg? Otherwise I have to snprintf or strncpy into a string to pass anyway. I can drop the dev_dbg but I would like to print out the sensor string if it doesn't match any of the expected ones.

%*s or %-*s ?

OK, that will work, thanks.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/hwmon/occ/occ.c line 199

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +6 -0
+       struct sensor_data_block_header *block;
+       struct device *dev = driver->dev;
+
+       /* check if the data is valid */
+       if (strncmp(&data[SENSOR_STR_OFFSET], "SENSOR", 6) != 0) {
+               dev_err(dev, "no SENSOR string in response\n");

Are those messages really necessary ? That can create a lot of

logging

Quoted 8 lines
Quoted 8 lines

noise

Quoted 1 line

if the occ responds with bad data.

I will audit these, but I think it's limited to one error message logged in any one function in case of error. Is that acceptable? Otherwise it may be hard to tell where the error occurred.

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c line 204

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+               rc = -ENODATA;
+               goto err;
+       }
+
+       sensor_block_num = data[SENSOR_BLOCK_NUM_OFFSET];

There is a lot of trust in assuming that this data was actually

received.

Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

At the same time, there is a lot of error checking. Why check if the information in this field is valid if you don't check if it was received in the first place ?

If we get to parse_occ_response, we know we got some data back from the OCC (see occ_get_all). Here we are really checking the response is a valid one, which I think makes sense in the parsing function.

Yes, but you don't check the length of the data vs. the length indicated in the header.

Well I don't know how you'd know the length of the actual data. That's why we
do the sanity checks when we parse. We read the amount of data that the OCC reports. If the scom ops fail to read all of it, we'll fail out. If it ends up
invalid in some way, our sanity checks will catch it.

Quoted 46 lines
Quoted 46 lines
Quoted 19 lines
Quoted 14 lines
drivers/hwmon/occ/occ.c new file +11 -0
+       if (sensor_block_num == 0) {
+               dev_err(dev, "no sensor blocks available\n");
+               rc = -ENODATA;
+               goto err;
+       }
+
+       /* if number of sensor block has changed, re-malloc */
+       if (sensor_block_num != resp->header.sensor_block_num) {
+               deinit_occ_resp_buf(resp);
+               resp->data.blocks = kcalloc(sensor_block_num,
+                                           sizeof(struct

sensor_data_block),

drivers/hwmon/occ/occ.c line 218

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+                                           GFP_KERNEL);
+               if (!resp->data.blocks)
+                       return -ENOMEM;
Quoted 1 line

Why not just allocate the maximum number of sensors once ? That might

waste

Quoted 1 line

a bit of memory, but it would make the code much less error prone. We

know

Quoted 2 lines

that
there will never be more sensors than the number fitting into 4k of

memory,

Quoted 1 line

so the "waste" would not really be that much.

Fair point. I'll do that.

Quoted 15 lines
Quoted 5 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       }
+
+       memcpy(&resp->header, &data[RESP_HEADER_OFFSET],

drivers/hwmon/occ/occ.c

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+              sizeof(struct occ_poll_header));
+       resp->header.error_log_addr_start =
+               be32_to_cpu(resp->header.error_log_addr_start);
+       resp->header.error_log_length =
+               be16_to_cpu(resp->header.error_log_length);
+
+       dev_dbg(dev, "Reading %d sensor blocks\n",
+               resp->header.sensor_block_num);
+       for (b = 0; b < sensor_block_num; b++) {
+               block = (struct sensor_data_block_header *)&data

[offset];

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/hwmon/occ/occ.c

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+               /* copy to a null terminated string */
+               strncpy(sensor_type_string, block->sensor_type, 4);
+               offset += 8;
+
+               dev_dbg(dev, "sensor block[%d]: type: %s,

sensor_num:

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/hwmon/occ/occ.c

Quoted 6 lines

%d\n", b,

drivers/hwmon/occ/occ.c new file +5 -0
+                       sensor_type_string, block->sensor_num);
+
+               if (strncmp(block->sensor_type, "FREQ", 4) == 0)
+                       sensor_type = FREQ;
+               else if (strncmp(block->sensor_type, "TEMP", 4) ==

0)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/hwmon/occ/occ.c

Quoted 2 lines
drivers/hwmon/occ/occ.c new file +2 -0
+                       sensor_type = TEMP;
+               else if (strncmp(block->sensor_type, "POWR", 4) ==

0)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/hwmon/occ/occ.c

Quoted 2 lines
drivers/hwmon/occ/occ.c new file +2 -0
+                       sensor_type = POWER;
+               else if (strncmp(block->sensor_type, "CAPS", 4) ==

0)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/hwmon/occ/occ.c

Quoted 3 lines
drivers/hwmon/occ/occ.c new file +3 -0
+                       sensor_type = CAPS;
+               else {
+                       dev_err(dev, "sensor type not supported %s

\n",

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/hwmon/occ/occ.c

Quoted 6 lines
drivers/hwmon/occ/occ.c new file +6 -0
+                               sensor_type_string);
+                       continue;
+               }
+
+               rc = occ_check_sensor(driver, block->sensor_length,
+                                     block->sensor_num,

sensor_type,

Quoted 12 lines
Quoted 12 lines

b);

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +9 -0
+               if (rc == -ENOMEM)
+                       goto err;
+               else if (rc)
+                       continue;
+
+               resp->data.sensor_block_id[sensor_type] = b;
+               for (s = 0; s < block->sensor_num; s++) {
+                       driver->ops.parse_sensor(data,
+

resp->data.blocks[b].sensors,
+ sensor_type,

offset,

Quoted 15 lines
Quoted 15 lines

s);

Quoted 6 lines
Quoted 6 lines
drivers/hwmon/occ/occ.c new file +6 -0
+                       offset += block->sensor_length;
+               }
+
+               /* copy block data over to response pointer */
+               resp->data.blocks[b].header = *block;
+       }
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+
+       return 0;
+err:
+       deinit_occ_resp_buf(resp);
+       return rc;
+}
+
+static u8 occ_send_cmd(struct occ *driver, u8 seq, u8 type, u16

length,

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/hwmon/occ/occ.c new file +14 -0
+                      const u8 *data, u8 *resp)
+{
+       u32 cmd1, cmd2;
+       u16 checksum = 0;
+       u16 length_le = cpu_to_le16(length);
+       bool retry = 0;
+       int i, rc, tries = 0;
+
+       cmd1 = (seq << 24) | (type << 16) | length_le;
+       memcpy(&cmd2, data, length);
+       cmd2 <<= ((4 - length) * 8);
+
+       /* checksum: sum of every bytes of cmd1, cmd2 */
+       for (i = 0; i < 4; i++) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+               checksum += (cmd1 >> (i * 8)) & 0xFF;
+               checksum += (cmd2 >> (i * 8)) & 0xFF;
+       }
+
+       cmd2 |= checksum << ((2 - length) * 8);
+
+       /* Init OCB */
+       rc = driver->bus_ops.putscom(driver->bus,

OCB_STATUS_CONTROL_OR,

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

drivers/hwmon/occ/occ.c

Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+                                    OCB_OR_INIT0, OCB_OR_INIT1);
+       if (rc)
+               goto err;
+
+       rc = driver->bus_ops.putscom(driver->bus,

OCB_STATUS_CONTROL_AND,

Quoted 5 lines
Quoted 5 lines
drivers/hwmon/occ/occ.c new file +5 -0
+                                    OCB_AND_INIT0, OCB_AND_INIT1);
+       if (rc)
+               goto err;
+
+       /* Send command, 2nd half of the 64-bit addr is unused

(write 0)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/hwmon/occ/occ.c

Quoted 3 lines

*/

drivers/hwmon/occ/occ.c new file +2 -0
+       rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+                                    driver->config.command_addr,

0);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/hwmon/occ/occ.c

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       if (rc)
+               goto err;
+
+       rc = driver->bus_ops.putscom(driver->bus, OCB_DATA, cmd1,

cmd2);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       if (rc)
+               goto err;
+
+       /* Trigger attention */
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines

+ rc = driver->bus_ops.putscom(driver->bus, ATTN_DATA, ATTN0,
ATTN1);

drivers/hwmon/occ/occ.c new file +6 -0
+       if (rc)
+               goto err;
+
+       /* Get response data */
+       rc = driver->bus_ops.putscom(driver->bus, OCB_ADDRESS,
+                                    driver->config.response_addr,

0);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       if (rc)
+               goto err;
+
+       do {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 6 lines
drivers/hwmon/occ/occ.c new file +3 -0
+               if (retry) {
+                       set_current_state(TASK_INTERRUPTIBLE);
+

schedule_timeout(msecs_to_jiffies(CMD_IN_PRG_INT_MS));

drivers/hwmon/occ/occ.c new file +2 -0
+               }
+

Isn't there a better way to do this ? What exactly is interrupted ?

The

Quoted 11 lines
Quoted 8 lines
Quoted 1 line

getscom function ? If so, shouldn't the timeout be handled there ?

There shouldn't be any interrupt. I set TASK_INTERRUPTIBLE because it doesn't really matter if it's interrupted or not. The purpose of this loop is to retry if we get the "command in progress" status from the OCC. It's higher level than the getscom function, so I think the loop is appropriate here.

This is quite unusual. Why is this needed here but not elsewhere in the kernel ? Normally the called function would time out and return an appropriate error.

This is the response to the entire occ command, not an individual scom operation. It's just the nature of the OCC. It can and will return the "busy" response sometimes. The scom op will succeed, and we have no indication
that we need to try again unless we check here. If we don't retry, we have to
fail out, as the data certainly isn't valid.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +6 -0
+               rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+                                            (u64 *)resp);
+               if (rc)
+                       goto err;
+
+               /* retry if we get "command in progress" return

status

Quoted 52 lines
Quoted 40 lines

*/

Quoted 30 lines
Quoted 3 lines

+ retry = (resp[RESP_RETURN_STATUS] ==
RESP_RETURN_CMD_IN_PRG) &&
+ (tries++ < CMD_IN_PRG_RETRIES);

There are some unnecessary () in this expression.

Quoted 22 lines
drivers/hwmon/occ/occ.c new file +22 -0
+       } while (retry);
+
+       switch (resp[RESP_RETURN_STATUS]) {
+       case RESP_RETURN_CMD_IN_PRG:
+               rc = -EALREADY;
+               break;
+       case RESP_RETURN_SUCCESS:
+               rc = 0;
+               break;
+       case RESP_RETURN_CMD_INVAL:
+       case RESP_RETURN_CMD_LEN:
+       case RESP_RETURN_DATA_INVAL:
+       case RESP_RETURN_CHKSUM:
+               rc = -EINVAL;
+               break;
+       case RESP_RETURN_OCC_ERR:
+               rc = -EREMOTE;
+               break;
+       default:
+               rc = -EFAULT;
+       }
+
Quoted 8 lines

drivers/hwmon/occ/occ.c line 370

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       return rc;
+
+err:
+       dev_err(driver->dev, "scom op failed rc:%d\n", rc);

Some of the errors result in an error message, some don't. What is the guiding principle ? Even if there is an error message, it doesn't

report

Quoted 8 lines
Quoted 2 lines

what function caused it, which makes it as good as no error message. Can this be dropped ?

resp[RESP_RETURN_STATUS] is the status reported on the OCC, these aren't necessarily errors, though most are. This is translating an OCC status code to an error for occ_get_all to pick up. It cannot be dropped, we need to know if the occ poll was successful. I'm not sure what you mean by reporting the function? occ_get_all will fail out if

There are several function returns which all end up here. You don't know which one actually failed.

Quoted 6 lines
Quoted 3 lines

we report the error here. If it's the scom op that failed we will have the dev_err message.

But which scom operation ? You mean it is important to report that some operation failed, but not which one ? Ok, I'll accept that, it just seems odd.

Ah, I guess I forgot I removed the dev_err calls from the actual scom op. I'll
try and improve the error handling in this function then.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines
drivers/hwmon/occ/occ.c new file +3 -0
+       return rc;
+}
+

drivers/hwmon/occ/occ.c line 383

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+static int occ_get_all(struct occ *driver)
+{
+       int i = 0, rc;
+       u8 *occ_data;
+       u16 num_bytes;
+       const u8 poll_cmd_data = OCC_POLL_STAT_SENSOR;
+       struct device *dev = driver->dev;
+       struct occ_response *resp = &driver->response;
+
+       occ_data = devm_kzalloc(dev, OCC_DATA_MAX, GFP_KERNEL);

I am a bot lost here. What is the purpose of using a devm function ?

Quoted 1 line
Quoted 1 line
Quoted 1 line

Either case, you might want to allocate the memory once and just keep

it

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

allocated.
Allocating a 4k buffer and freeing it each time data is read from the

occ

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

seems
to be wasteful and not really worth it.

Agreed.

Quoted 1 line
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/hwmon/occ/occ.c

Quoted 11 lines
drivers/hwmon/occ/occ.c new file +4 -0
+       if (!occ_data)
+               return -ENOMEM;
+
+       rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data,

occ_data);

drivers/hwmon/occ/occ.c new file +6 -0
+       if (rc) {
+               dev_err(dev, "OCC poll failed: %d\n", rc);
+               goto out;
+       }
+
+       num_bytes = get_unaligned((u16 *)&occ_data

[RESP_DATA_LENGTH]);

Quoted 40 lines
Quoted 40 lines
Quoted 8 lines
Quoted 1 line

+ num_bytes = be16_to_cpu(num_bytes);

What data is in big endian, and what data is in little endian ?

Looks like everything is big endian except for the length in the send command.
Is that correct ? If so, an explanation would be useful, because the

logic

Quoted 1 line

isn't entirely clear.

I'll add some comments.

Quoted 13 lines

drivers/hwmon/occ/occ.c line 403

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +9 -0
+       dev_dbg(dev, "OCC data length: %d\n", num_bytes);
+
+       if (num_bytes > OCC_DATA_MAX) {
+               dev_err(dev, "OCC data length must be < 4KB\n");
+               rc = -EINVAL;
+               goto out;
+       }
+
+       if (num_bytes <= 0) {

This can never be < 0.

Good point.

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +10 -0
+               dev_err(dev, "OCC data length is zero\n");
+               rc = -EINVAL;
+               goto out;
+       }
+
+       /* read remaining data */
+       for (i = 8; i < num_bytes + 8; i += 8) {
+               rc = driver->bus_ops.getscom(driver->bus, OCB_DATA,
+                                            (u64 *)&occ_data[i]);
+               if (rc) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/occ/occ.c new file +6 -0
+                       dev_err(dev, "scom op failed rc:%d\n", rc);
+                       goto out;
+               }
+       }
+
+       /* don't need more sanity checks; buffer is alloc'd for max

response
+ * size so we just check for valid data in

parse_occ_response

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

+ */

Yes, but as mentioned above you don't check in that function if the

data

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

was actually received.

If we don't receive data, the data length will be 0 or the low level getscom will fail. So we know we have data here.

Ok. However, since you don't pass num_bytes to the parse function, there is no verification that num_bytes actually matches what is in the packet.
The rest of the header could indicate that there is 4k of data, but

num_bytes

Quoted 1 line

reports, say, the minimum size. In that case, parse_occ_response() would

end

Quoted 1 line

up parsing a stale buffer.

Ah, I see your concern about parsing a stale buffer. As is, the driver re-allocs
the buffer every time, so you won't see stale data. As soon as your run out of
real sensor data to parse, the sanity checking will fail. But it makes sense to
ensure we haven't gone past the amount we've read in anyway, I'll do that.

Quoted 44 lines
Quoted 44 lines
Quoted 23 lines
Quoted 17 lines
drivers/hwmon/occ/occ.c new file +14 -0
+       rc = parse_occ_response(driver, occ_data, resp);
+
+out:
+       devm_kfree(dev, occ_data);
+       return rc;
+}
+
+int occ_update_device(struct occ *driver)
+{
+       int rc = 0;
+
+       mutex_lock(&driver->update_lock);
+
+       if (time_after(jiffies, driver->last_updated +

driver->update_interval)

drivers/hwmon/occ/occ.c line 437

Quoted 2 lines
drivers/hwmon/occ/occ.c new file +2 -0
+           || !driver->valid) {
+               driver->valid = 1;

valid is bool.

Quoted 7 lines

drivers/hwmon/occ/occ.c line 441

Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+
+               rc = occ_get_all(driver);
+               if (rc)
+                       driver->valid = 0;

valid is bool.

Good point.

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ.c new file +10 -0
+
+               driver->last_updated = jiffies;
+       }
+
+       mutex_unlock(&driver->update_lock);
+
+       return rc;
+}
+EXPORT_SYMBOL(occ_update_device);
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+void *occ_get_sensor(struct occ *driver, int sensor_type)
+{
+       int rc;
+
+       /* occ_update_device locks the update lock */
+       rc = occ_update_device(driver);
+       if (rc) {
+               dev_err(driver->dev, "cannot get occ sensor data: %d

\n",

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+ rc);

Have you counted the number of error messages reported if anything

goes

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

wrong.
This driver is quite noisy. I won't force you to drop the messages,

but

Quoted 3 lines
Quoted 3 lines

it

Quoted 2 lines

seems
to me that if anything goes wrong, the kernel log will be clogged

with

Quoted 11 lines
Quoted 11 lines
Quoted 2 lines

messages
just from this driver.

I'll audit these.

Quoted 6 lines
Quoted 6 lines
drivers/hwmon/occ/occ.c new file +4 -0
+               return NULL;
+       }
+
+       return occ_get_sensor_by_type(&driver->response,

sensor_type);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +4 -0
+}
+EXPORT_SYMBOL(occ_get_sensor);
+
+int occ_get_sensor_value(struct occ *occ, int sensor_type, int

sensor_num,

drivers/hwmon/occ/occ.c new file +3 -0
+                        u32 hwmon, long *val)
+{
+       return occ->ops.get_sensor(occ, sensor_type, sensor_num,

hwmon,

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

drivers/hwmon/occ/occ.c

Quoted 5 lines

val);

drivers/hwmon/occ/occ.c new file +4 -0
+}
+EXPORT_SYMBOL(occ_get_sensor_value);
+
+void occ_get_response_blocks(struct occ *occ, struct occ_blocks

**blocks)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/hwmon/occ/occ.c

Quoted 6 lines
drivers/hwmon/occ/occ.c new file +6 -0
+{
+       *blocks = &occ->response.data;
+}
+EXPORT_SYMBOL(occ_get_response_blocks);
+
+void occ_set_update_interval(struct occ *occ, unsigned long

interval)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/hwmon/occ/occ.c new file +4 -0
+{
+       occ->update_interval = msecs_to_jiffies(interval);
+}
+EXPORT_SYMBOL(occ_set_update_interval);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ.c

Quoted 8 lines
drivers/hwmon/occ/occ.c new file +8 -0
+
+int occ_set_user_powercap(struct occ *occ, u16 cap)
+{
+       u8 resp[8];
+
+       cap = cpu_to_be16(cap);
+
+       return occ_send_cmd(occ, 0, OCC_SET_USER_POWR_CAP, 2, (const

u8

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/hwmon/occ/occ.c

Quoted 7 lines

*)&cap,

drivers/hwmon/occ/occ.c new file +6 -0
+                           resp);
+}
+EXPORT_SYMBOL(occ_set_user_powercap);
+
+struct occ *occ_start(struct device *dev, void *bus,
+                     struct occ_bus_ops *bus_ops, const struct

occ_ops

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 6 lines

*ops,

drivers/hwmon/occ/occ.c new file +3 -0
+                     const struct occ_config *config)
+{
+       struct occ *driver = devm_kzalloc(dev, sizeof(struct occ),

GFP_KERNEL);

drivers/hwmon/occ/occ.c new file +1 -0
+

drivers/hwmon/occ/occ.c line 513

Quoted 10 lines
drivers/hwmon/occ/occ.c new file +10 -0
+       if (!driver)
+               return ERR_PTR(-ENOMEM);
+
+       driver->dev = dev;
+       driver->bus = bus;
+       driver->bus_ops = *bus_ops;
+       driver->ops = *ops;
+       driver->config = *config;
+
+       driver->update_interval = HZ;

The update interval is only supposed to be configurable if it can be

Quoted 2 lines
Quoted 2 lines

written

Quoted 1 line

into the hardware. Otherwise it is really up to user space to decide

how

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

often
it wants to poll the data, not up to the kernel.

I don't see a function to send the update interval to the occ. As

such,

Quoted 22 lines
Quoted 4 lines

it

Quoted 2 lines

should
not be configurable but a constant.

Quoted 11 lines

I don't quite follow. Right now, the driver maximum polling rate is configurable by userspace (via hwmon sysfs attribute). If you mean that user space should control that rate just by how often users read the attributes, I'm not sure that will work as there are a large number of attributes that will all try and trigger an occ poll when read. Besides, different users don't want to communicate about when they last polled. If the rate is constant, if may be too slow or too fast depending on the system and needs of the users. Why can't the polling rate be configurable? To user space, the update interval works the same if it's driver or hardware.

The update_interval attribute is only supposed to exist if writing it results in writing a value into a chip register. It is supposed to set the _chip_ update interval, not the kernel update interval. If you want to limit the rate of chip accesses, you can do it by using a constant in the driver, just like other drivers do it.

Quoted 3 lines

This is similar to limit registers. If the chip does not support limit registers, we don't make up "soft" limits either, but don't support the respective attributes.

OK, we'll just use a constant then.

Quoted 4 lines

Thanks,
Guenter

#6 lore raw

PATCH v3 2/6 hwmon: occ: Add sysfs interface

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 271 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  44 +++++++
 4 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +62 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports a number of sysfs files for each type of sensor. The
+sensor-specific files vary depending on the processor type, though many of the
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be used.
+Therefore, the frequency sensor on the OCC uses the "input" type sensor defined
+by the hwmon interface, rather than defining a new type of custom sensor.
+
+Below are detailed the names and meaning of each sensor file for both types of
+processors. All sensors are read-only unless otherwise specified. <x> indicates
+the hwmon index. sensor id indicates the unique internal OCC identifer. Please
+see the POWER OCC specification for details on all these sensor values.
+
+frequency:
+	all processors:
+		in<x>_input - frequency value
+		in<x>_label - sensor id
+temperature:
+	POWER8:
+		temp<x>_input - temperature value
+		temp<x>_label - sensor id
+	POWER9 (in addition to above):
+		temp<x>_type - FRU type
+
+power:
+	POWER8:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average - accumulator
+		power<x>_average_interval - update tag (number of samples in
+			accumulator)
+	POWER9:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average_min - accumulator[0]
+		power<x>_average_max - accumulator[1] (64 bits total)
+		power<x>_average_interval - update tag
+		power<x>_reset_history - (function_id | (apss_channel << 8)
+
+caps:
+	POWER8:
+		power<x>_cap - current powercap
+		power<x>_cap_max - max powercap
+		power<x>_cap_min - min powercap
+		power<x>_max - normal powercap
+		power<x>_alarm - user powercap, r/w
+	POWER9:
+		power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be multiple
+OCCs present on the system, these two files are only present for the "master"
+OCC.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
 BMC - Host Communications
 -------------------------
 
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
drivers/hwmon/occ/occ_sysfs.c new file +271 -0

271 lines not on this page · raw message

drivers/hwmon/occ/occ_sysfs.h new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#7 lore raw

Re: [PATCH linux v3 2/6] hwmon: occ: Add sysfs interface

Quoted 131 lines · On 01/16/2017 01:13 PM, [email protected]…

From: "Edward A. James" <eajames-r/[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 271 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  44 +++++++
 4 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +62 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports a number of sysfs files for each type of sensor. The
+sensor-specific files vary depending on the processor type, though many of the
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be used.
+Therefore, the frequency sensor on the OCC uses the "input" type sensor defined
+by the hwmon interface, rather than defining a new type of custom sensor.
+
+Below are detailed the names and meaning of each sensor file for both types of
+processors. All sensors are read-only unless otherwise specified. <x> indicates
+the hwmon index. sensor id indicates the unique internal OCC identifer. Please
+see the POWER OCC specification for details on all these sensor values.
+
+frequency:
+	all processors:
+		in<x>_input - frequency value
+		in<x>_label - sensor id
+temperature:
+	POWER8:
+		temp<x>_input - temperature value
+		temp<x>_label - sensor id
+	POWER9 (in addition to above):
+		temp<x>_type - FRU type
+
+power:
+	POWER8:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average - accumulator
+		power<x>_average_interval - update tag (number of samples in
+			accumulator)
+	POWER9:
+		power<x>_input - power value
+		power<x>_label - sensor id
+		power<x>_average_min - accumulator[0]
+		power<x>_average_max - accumulator[1] (64 bits total)
+		power<x>_average_interval - update tag
+		power<x>_reset_history - (function_id | (apss_channel << 8)
+
+caps:
+	POWER8:
+		power<x>_cap - current powercap
+		power<x>_cap_max - max powercap
+		power<x>_cap_min - min powercap
+		power<x>_max - normal powercap
+		power<x>_alarm - user powercap, r/w
+	POWER9:
+		power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be multiple
+OCCs present on the system, these two files are only present for the "master"
+OCC.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
 BMC - Host Communications
 -------------------------
 
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
drivers/hwmon/occ/occ_sysfs.c new file +29 -0
new file mode 100644
index 0000000..2f20c40
@@ -0,0 +1,271 @@
+/*
+ * occ_sysfs.c - OCC sysfs interface
+ *
+ * This file contains the methods and data structures for implementing the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>

drivers/hwmon/occ/occ_sysfs.c line 39

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_sysfs.h"
+
+#define RESP_RETURN_CMD_INVAL	0x13
+
+static int occ_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			  u32 attr, int channel, long *val)
+{
+	int rc = 0;

Unnecessary initialization.

Quoted 27 lines
drivers/hwmon/occ/occ_sysfs.c new file +27 -0
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	struct occ *occ = driver->occ;
+
+	switch (type) {
+	case hwmon_in:
+		rc = occ_get_sensor_value(occ, FREQ, channel, attr, val);
+		break;
+	case hwmon_temp:
+		rc = occ_get_sensor_value(occ, TEMP, channel, attr, val);
+		break;
+	case hwmon_power:
+		rc = occ_get_sensor_value(occ, POWER, channel, attr, val);
+		break;
+	default:
+		rc = -EOPNOTSUPP;
+	}
+
+	return rc;
+}
+
+static int occ_hwmon_read_string(struct device *dev,
+				 enum hwmon_sensor_types type, u32 attr,
+				 int channel, char **str)
+{
+	int rc;
+	unsigned long val = 0;
+

drivers/hwmon/occ/occ_sysfs.c line 76

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+	if (!((type == hwmon_in && attr == hwmon_in_label) ||
+	    (type == hwmon_temp && attr == hwmon_temp_label) ||
+	    (type == hwmon_power && attr == hwmon_power_label)))
+		return -EOPNOTSUPP;
+
+	rc = occ_hwmon_read(dev, type, attr, channel, &val);
+	if (rc < 0)
+		return rc;
+
+	rc = snprintf(*str, PAGE_SIZE - 1, "%ld", val);

val is unsigned long.

I am quite confused what this is for, though. The function is used for string attributes, or in other words for labels. What is the benefit of having a label that returns the same data as the value attribute ? Is this maybe a misunderstanding ?

Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.c new file +4 -0
+	if (rc > 0)
+		rc = 0;
+
+	return rc;

drivers/hwmon/occ/occ_sysfs.c line 90

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+}
+
+static int occ_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
+			   u32 attr, int channel, long val)
+{
+	int rc = 0;
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+		occ_set_update_interval(driver->occ, val);

As mentioned in the other patch, please drop. This is not the intended use case for this attribute.

Quoted 41 lines
drivers/hwmon/occ/occ_sysfs.c new file +41 -0
+		return 0;
+	} else if (type == hwmon_power && attr == hwmon_power_alarm) {
+		rc = occ_set_user_powercap(driver->occ, val);
+		if (rc) {
+			if (rc == RESP_RETURN_CMD_INVAL) {
+				dev_err(dev,
+					"set invalid powercap value: %ld\n",
+					val);
+				return -EINVAL;
+			}
+
+			dev_err(dev, "set user powercap failed: 0x:%x\n", rc);
+			return rc;
+		}
+
+		driver->user_powercap = val;
+
+		return rc;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static umode_t occ_is_visible(const void *data, enum hwmon_sensor_types type,
+			      u32 attr, int channel)
+{
+	const struct occ_sysfs *driver = data;
+
+	switch (type) {
+	case hwmon_chip:
+		if (attr == hwmon_chip_update_interval)
+			return S_IRUGO | S_IWUSR;
+		break;
+	case hwmon_in:
+		if (BIT(attr) & driver->sensor_hwmon_configs[0])
+			return S_IRUGO;
+		break;
+	case hwmon_temp:
+		if (BIT(attr) & driver->sensor_hwmon_configs[1])
+			return S_IRUGO;
+		break;

drivers/hwmon/occ/occ_sysfs.c line 141

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+	case hwmon_power:
+		/* user power limit */
+		if (attr == hwmon_power_alarm)
+			return S_IRUGO | S_IWUSR;
+		else if ((BIT(attr) & driver->sensor_hwmon_configs[2]) ||
+			 (BIT(attr) & driver->sensor_hwmon_configs[3]))
+			return S_IRUGO;
+		break;
+	default:
+		return 0;

Might as well use break; here for consistency.

Quoted 42 lines
drivers/hwmon/occ/occ_sysfs.c new file +42 -0
+	}
+
+	return 0;
+}
+
+static const struct hwmon_ops occ_hwmon_ops = {
+	.is_visible = occ_is_visible,
+	.read = occ_hwmon_read,
+	.read_string = occ_hwmon_read_string,
+	.write = occ_hwmon_write,
+};
+
+static const u32 occ_chip_config[] = {
+	HWMON_C_UPDATE_INTERVAL,
+	0
+};
+
+static const struct hwmon_channel_info occ_chip = {
+	.type = hwmon_chip,
+	.config = occ_chip_config
+};
+
+static const enum hwmon_sensor_types occ_sensor_types[MAX_OCC_SENSOR_TYPE] = {
+	hwmon_in,
+	hwmon_temp,
+	hwmon_power,
+	hwmon_power
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  const u32 *sensor_hwmon_configs,
+				  const char *name)
+{
+	bool master_occ = false;
+	int rc, i, j, sensor_num, index = 0, id;
+	char *brk;
+	struct occ_blocks *resp = NULL;
+	u32 *sensor_config;
+	struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+					       GFP_KERNEL);
+	if (!hwmon)
+		return ERR_PTR(-ENOMEM);

drivers/hwmon/occ/occ_sysfs.c line 193

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+	/* need space for null-termination and occ chip */
+	hwmon->occ_sensors =
+		devm_kzalloc(dev, sizeof(struct hwmon_channel_info *) *
+			     (MAX_OCC_SENSOR_TYPE + 2), GFP_KERNEL);
+	if (!hwmon->occ_sensors)
+		return ERR_PTR(-ENOMEM);
+
+	hwmon->occ = occ;
+	hwmon->sensor_hwmon_configs = (u32 *)sensor_hwmon_configs;

Either this is a const or it isn't. If it isn't, it should not be passed in as const. If it is, it can be declared const in struct occ_sysfs.

drivers/hwmon/occ/occ_sysfs.c line 196

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+	hwmon->occ_info.ops = &occ_hwmon_ops;
+	hwmon->occ_info.info =
+		(const struct hwmon_channel_info **)hwmon->occ_sensors;

Why is this typecast needed ?

Quoted 33 lines
drivers/hwmon/occ/occ_sysfs.c new file +33 -0
+
+	occ_get_response_blocks(occ, &resp);
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+		resp->sensor_block_id[i] = -1;
+
+	/* read sensor data from occ */
+	rc = occ_update_device(occ);
+	if (rc) {
+		dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+		return ERR_PTR(rc);
+	}
+	if (!resp->blocks)
+		return ERR_PTR(-ENOMEM);
+
+	master_occ = resp->sensor_block_id[CAPS] >= 0;
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+		id = resp->sensor_block_id[i];
+		if (id < 0)
+			continue;
+
+		sensor_num = resp->blocks[id].header.sensor_num;
+		/* need null-termination */
+		sensor_config = devm_kzalloc(dev,
+					     sizeof(u32) * (sensor_num + 1),
+					     GFP_KERNEL);
+		if (!sensor_config)
+			return ERR_PTR(-ENOMEM);
+
+		for (j = 0; j < sensor_num; j++)
+			sensor_config[j] = sensor_hwmon_configs[i];
+

drivers/hwmon/occ/occ_sysfs.c line 239

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+		hwmon->occ_sensors[index] =
+			devm_kzalloc(dev, sizeof(struct hwmon_channel_info),
+				     GFP_KERNEL);
+		if (!hwmon->occ_sensors[index])
+			return ERR_PTR(-ENOMEM);
+
+		hwmon->occ_sensors[index]->type = occ_sensor_types[i];
+		hwmon->occ_sensors[index]->config = sensor_config;
+		index++;
+	}

I had the impression from patch 1 that the number of sensors can change at runtime. If so, this doesn't really work well; the sysfs attributes won't be updated in this case.

Can it really happen that the sensor information can change ?

drivers/hwmon/occ/occ_sysfs.c line 244

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+
+	/* only need one of these for any number of occs */
+	if (master_occ)
+		hwmon->occ_sensors[index] =
+			(struct hwmon_channel_info *)&occ_chip;

Why this typecast ?

Quoted 62 lines
drivers/hwmon/occ/occ_sysfs.c new file +27 -0
+
+	/* search for bad chars */
+	strncpy(hwmon->hwmon_name, name, OCC_HWMON_NAME_LENGTH);
+	brk = strpbrk(hwmon->hwmon_name, "-* \t\n");
+	while (brk) {
+		*brk = '_';
+		brk = strpbrk(brk,  "-* \t\n");
+	}
+
+	hwmon->dev = devm_hwmon_device_register_with_info(dev,
+							  hwmon->hwmon_name,
+							  hwmon,
+							  &hwmon->occ_info,
+							  NULL);
+	if (IS_ERR(hwmon->dev)) {
+		dev_err(dev, "cannot register hwmon device %s: %ld\n",
+			hwmon->hwmon_name, PTR_ERR(hwmon->dev));
+		return ERR_CAST(hwmon->dev);
+	}
+
+	return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+MODULE_AUTHOR("Eddie James <eajames-r/[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_sysfs.h new file +29 -0
new file mode 100644
index 0000000..7de92e7
@@ -0,0 +1,44 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes for the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+#include <linux/hwmon.h>
+
+struct occ;
+struct device;
+
+#define OCC_HWMON_NAME_LENGTH	32
+

drivers/hwmon/occ/occ_sysfs.h line 39

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.h new file +10 -0
+struct occ_sysfs {
+	struct device *dev;
+	struct occ *occ;
+
+	char hwmon_name[OCC_HWMON_NAME_LENGTH + 1];
+	u32 *sensor_hwmon_configs;
+	struct hwmon_channel_info **occ_sensors;
+	struct hwmon_chip_info occ_info;
+	u16 user_powercap;
+};

Is this information used outside the sysfs code ? If not, it should be defined in the source file and not be available outside of it.

drivers/hwmon/occ/occ_sysfs.h line 44

Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.h new file +5 -0
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  const u32 *sensor_hwmon_configs,
+				  const char *name);
+#endif /* __OCC_SYSFS_H__ */

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

Re: [PATCH linux v3 2/6] hwmon: occ: Add sysfs interface

Quoted 19 lines · Guenter Roeck
Quoted 18 lines · On 01/16/2017 01:13 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 271
++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  44 +++++++
 4 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Quoted 7 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types · line 26

Quoted 7 lines
Documentation/hwmon/occ +0 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four types
 of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and

thresholds

Quoted 15 lines
Quoted 12 lines

used
internally on the OCC.

Documentation/hwmon/occ +4 -0
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to

userspace.

Documentation/hwmon/occ +3 -0
+
+The driver exports a number of sysfs files for each type of sensor. The
+sensor-specific files vary depending on the processor type, though many

of the

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be

used.

Quoted 3 lines
Quoted 3 lines

+Therefore, the frequency sensor on the OCC uses the "input" type sensor defined
+by the hwmon interface, rather than defining a new type of custom

sensor.

Quoted 18 lines
Quoted 18 lines
Documentation/hwmon/occ +2 -0
+
+Below are detailed the names and meaning of each sensor file for both

types of
+processors. All sensors are read-only unless otherwise specified. <x> indicates
+the hwmon index. sensor id indicates the unique internal OCC identifer. Please

Documentation/hwmon/occ +11 -0
+see the POWER OCC specification for details on all these sensor values.
+
+frequency:
+       all processors:
+               in<x>_input - frequency value
+               in<x>_label - sensor id
+temperature:
+       POWER8:
+               temp<x>_input - temperature value
+               temp<x>_label - sensor id
+       POWER9 (in addition to above):
Quoted 8 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 8 lines
Documentation/hwmon/occ +8 -0
+               temp<x>_type - FRU type
+
+power:
+       POWER8:
+               power<x>_input - power value
+               power<x>_label - sensor id
+               power<x>_average - accumulator
+               power<x>_average_interval - update tag (number of

samples

Quoted 35 lines
Quoted 35 lines

in

Documentation/hwmon/occ +8 -0
+                       accumulator)
+       POWER9:
+               power<x>_input - power value
+               power<x>_label - sensor id
+               power<x>_average_min - accumulator[0]
+               power<x>_average_max - accumulator[1] (64 bits total)
+               power<x>_average_interval - update tag
+               power<x>_reset_history - (function_id | (apss_channel <<

8)

Documentation/hwmon/occ +13 -0
+
+caps:
+       POWER8:
+               power<x>_cap - current powercap
+               power<x>_cap_max - max powercap
+               power<x>_cap_min - min powercap
+               power<x>_max - normal powercap
+               power<x>_alarm - user powercap, r/w
+       POWER9:
+               power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be

multiple
+OCCs present on the system, these two files are only present for the "master"

Documentation/hwmon/occ +3 -0
+OCC.
+       name - read the name of the driver
+       update_interval - read or write the minimum interval for polling

the

Documentation/hwmon/occ +2 -0
+               OCC.
+
 BMC - Host Communications
 -------------------------
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.c

Quoted 41 lines
Quoted 41 lines

new file mode 100644
index 0000000..2f20c40

drivers/hwmon/occ/occ_sysfs.c new file +4 -0
@@ -0,0 +1,271 @@
+/*
+ * occ_sysfs.c - OCC sysfs interface
+ *
+ * This file contains the methods and data structures for implementing

the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ_sysfs.c new file +17 -0
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_sysfs.h"
+
+#define RESP_RETURN_CMD_INVAL  0x13
+
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 39

Quoted 5 lines

+static int occ_hwmon_read(struct device *dev, enum hwmon_sensor_types type,

drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+                         u32 attr, int channel, long *val)
+{
+       int rc = 0;

Unnecessary initialization.

Got it.

Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+       struct occ_sysfs *driver = dev_get_drvdata(dev);
+       struct occ *occ = driver->occ;
+
+       switch (type) {
+       case hwmon_in:
+               rc = occ_get_sensor_value(occ, FREQ, channel, attr,

val);

Quoted 3 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               break;
+       case hwmon_temp:
+               rc = occ_get_sensor_value(occ, TEMP, channel, attr,

val);

Quoted 3 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               break;
+       case hwmon_power:
+               rc = occ_get_sensor_value(occ, POWER, channel, attr,

val);

Quoted 29 lines
Quoted 15 lines
drivers/hwmon/occ/occ_sysfs.c new file +15 -0
+               break;
+       default:
+               rc = -EOPNOTSUPP;
+       }
+
+       return rc;
+}
+
+static int occ_hwmon_read_string(struct device *dev,
+                                enum hwmon_sensor_types type, u32 attr,
+                                int channel, char **str)
+{
+       int rc;
+       unsigned long val = 0;
+

drivers/hwmon/occ/occ_sysfs.c line 76

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+       if (!((type == hwmon_in && attr == hwmon_in_label) ||
+           (type == hwmon_temp && attr == hwmon_temp_label) ||
+           (type == hwmon_power && attr == hwmon_power_label)))
+               return -EOPNOTSUPP;
+
+       rc = occ_hwmon_read(dev, type, attr, channel, &val);
+       if (rc < 0)
+               return rc;
+
+       rc = snprintf(*str, PAGE_SIZE - 1, "%ld", val);

val is unsigned long.

Quoted 5 lines

I am quite confused what this is for, though. The function is used for string attributes,
or in other words for labels. What is the benefit of having a label that returns the same
data as the value attribute ? Is this maybe a misunderstanding ?

So this does work, though I admit it's a bit confusing. occ_get_sensor_value returns either the value or the occ sensor id depending on the "type" passed in. I'll rename occ_get_sensor_value and add a comment.

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       if (rc > 0)
+               rc = 0;
+
+       return rc;
+}
+
+static int occ_hwmon_write(struct device *dev, enum hwmon_sensor_types

type,

drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+                          u32 attr, int channel, long val)
+{
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 90

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+       int rc = 0;
+       struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+       if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+               occ_set_update_interval(driver->occ, val);

As mentioned in the other patch, please drop. This is not the intended

use

Quoted 2 lines

case
for this attribute.

OK.

Quoted 15 lines
Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+               return 0;
+       } else if (type == hwmon_power && attr == hwmon_power_alarm) {
+               rc = occ_set_user_powercap(driver->occ, val);
+               if (rc) {
+                       if (rc == RESP_RETURN_CMD_INVAL) {
+                               dev_err(dev,
+                                       "set invalid powercap value:

%ld\n",

drivers/hwmon/occ/occ_sysfs.c

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+                                       val);
+                               return -EINVAL;
+                       }
+
+                       dev_err(dev, "set user powercap failed: 0x:%x

\n",

Quoted 28 lines
Quoted 28 lines

rc);

drivers/hwmon/occ/occ_sysfs.c new file +12 -0
+                       return rc;
+               }
+
+               driver->user_powercap = val;
+
+               return rc;
+       }
+
+       return -EOPNOTSUPP;
+}
+
+static umode_t occ_is_visible(const void *data, enum hwmon_sensor_types

type,

drivers/hwmon/occ/occ_sysfs.c new file +14 -0
+                             u32 attr, int channel)
+{
+       const struct occ_sysfs *driver = data;
+
+       switch (type) {
+       case hwmon_chip:
+               if (attr == hwmon_chip_update_interval)
+                       return S_IRUGO | S_IWUSR;
+               break;
+       case hwmon_in:
+               if (BIT(attr) & driver->sensor_hwmon_configs[0])
+                       return S_IRUGO;
+               break;
+       case hwmon_temp:
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               if (BIT(attr) & driver->sensor_hwmon_configs[1])
+                       return S_IRUGO;
+               break;
+       case hwmon_power:
+               /* user power limit */
+               if (attr == hwmon_power_alarm)
+                       return S_IRUGO | S_IWUSR;
+               else if ((BIT(attr) & driver->sensor_hwmon_configs[2])
||
>> +                        (BIT(attr) & driver->sensor_hwmon_configs[3]))
>> +                       return S_IRUGO;
>> +               break;
>> +       default:
>> +               return 0;
Quoted 54 lines

Might as well use break; here for consistency.

Quoted 49 lines
drivers/hwmon/occ/occ_sysfs.c new file +23 -0
+       }
+
+       return 0;
+}
+
+static const struct hwmon_ops occ_hwmon_ops = {
+       .is_visible = occ_is_visible,
+       .read = occ_hwmon_read,
+       .read_string = occ_hwmon_read_string,
+       .write = occ_hwmon_write,
+};
+
+static const u32 occ_chip_config[] = {
+       HWMON_C_UPDATE_INTERVAL,
+       0
+};
+
+static const struct hwmon_channel_info occ_chip = {
+       .type = hwmon_chip,
+       .config = occ_chip_config
+};
+
+static const enum hwmon_sensor_types

occ_sensor_types[MAX_OCC_SENSOR_TYPE] = {

drivers/hwmon/occ/occ_sysfs.c new file +16 -0
+       hwmon_in,
+       hwmon_temp,
+       hwmon_power,
+       hwmon_power
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                                 const u32 *sensor_hwmon_configs,
+                                 const char *name)
+{
+       bool master_occ = false;
+       int rc, i, j, sensor_num, index = 0, id;
+       char *brk;
+       struct occ_blocks *resp = NULL;
+       u32 *sensor_config;
+       struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct

occ_sysfs),

drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+                                              GFP_KERNEL);
+       if (!hwmon)
+               return ERR_PTR(-ENOMEM);
+
+       /* need space for null-termination and occ chip */
+       hwmon->occ_sensors =
+               devm_kzalloc(dev, sizeof(struct hwmon_channel_info *) *
+                            (MAX_OCC_SENSOR_TYPE + 2), GFP_KERNEL);
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 193

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+       if (!hwmon->occ_sensors)
+               return ERR_PTR(-ENOMEM);
+
+       hwmon->occ = occ;
+       hwmon->sensor_hwmon_configs = (u32 *)sensor_hwmon_configs;

Either this is a const or it isn't. If it isn't, it should not be passed

in

Quoted 2 lines

as const.
If it is, it can be declared const in struct occ_sysfs.

It is const, i'll fix.

Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 196

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+       hwmon->occ_info.ops = &occ_hwmon_ops;
+       hwmon->occ_info.info =
+               (const struct hwmon_channel_info **)hwmon->occ_sensors;

Why is this typecast needed ?

struct hwmon_chip_info {
        const struct hwmon_ops *ops;
        const struct hwmon_channel_info **info;
};

My compiler throws an error without a cast here.

occ_sysfs.c:195:23: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
| hwmon->occ_info.info = hwmon->occ_sensors;

occ_sensors is not const, as it's dynamically zero-allocated and then if we poll and get a sensor, we allocate a new hwmon_channel_info. Not sure what the best solution is here except to cast.

Quoted 42 lines
Quoted 42 lines
drivers/hwmon/occ/occ_sysfs.c new file +26 -0
+
+       occ_get_response_blocks(occ, &resp);
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+               resp->sensor_block_id[i] = -1;
+
+       /* read sensor data from occ */
+       rc = occ_update_device(occ);
+       if (rc) {
+               dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+               return ERR_PTR(rc);
+       }
+       if (!resp->blocks)
+               return ERR_PTR(-ENOMEM);
+
+       master_occ = resp->sensor_block_id[CAPS] >= 0;
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+               id = resp->sensor_block_id[i];
+               if (id < 0)
+                       continue;
+
+               sensor_num = resp->blocks[id].header.sensor_num;
+               /* need null-termination */
+               sensor_config = devm_kzalloc(dev,
+                                            sizeof(u32) * (sensor_num +

1),

drivers/hwmon/occ/occ_sysfs.c new file +9 -0
+                                            GFP_KERNEL);
+               if (!sensor_config)
+                       return ERR_PTR(-ENOMEM);
+
+               for (j = 0; j < sensor_num; j++)
+                       sensor_config[j] = sensor_hwmon_configs[i];
+
+               hwmon->occ_sensors[index] =
+                       devm_kzalloc(dev, sizeof(struct

hwmon_channel_info),

drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+                                    GFP_KERNEL);
+               if (!hwmon->occ_sensors[index])
+                       return ERR_PTR(-ENOMEM);
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 239

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+
+               hwmon->occ_sensors[index]->type = occ_sensor_types[i];
+               hwmon->occ_sensors[index]->config = sensor_config;
+               index++;
+       }

I had the impression from patch 1 that the number of sensors can change

at

Quoted 2 lines

runtime.
If so, this doesn't really work well; the sysfs attributes won't be

updated

Quoted 4 lines

in this
case.

Can it really happen that the sensor information can change ?

Well, the number of sensors won't change while we are running. But we don't know how many sensors we have when we start up. So this first poll will tell us how many we need and allow the driver to initialize the correct number of hwmon attributes. But yes, it shouldn't change while we
are running.

Quoted 9 lines

drivers/hwmon/occ/occ_sysfs.c line 244

Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+
+       /* only need one of these for any number of occs */
+       if (master_occ)
+               hwmon->occ_sensors[index] =
+                       (struct hwmon_channel_info *)&occ_chip;

Why this typecast ?

occ_chip is const but my occ_sensors field isn't.

Quoted 24 lines
Quoted 24 lines
drivers/hwmon/occ/occ_sysfs.c new file +11 -0
+
+       /* search for bad chars */
+       strncpy(hwmon->hwmon_name, name, OCC_HWMON_NAME_LENGTH);
+       brk = strpbrk(hwmon->hwmon_name, "-* \t\n");
+       while (brk) {
+               *brk = '_';
+               brk = strpbrk(brk,  "-* \t\n");
+       }
+
+       hwmon->dev = devm_hwmon_device_register_with_info(dev,
+

hwmon->hwmon_name,

drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+                                                         hwmon,
+

&hwmon->occ_info,

drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+                                                         NULL);
+       if (IS_ERR(hwmon->dev)) {
+               dev_err(dev, "cannot register hwmon device %s: %ld\n",
+                       hwmon->hwmon_name, PTR_ERR(hwmon->dev));
+               return ERR_CAST(hwmon->dev);
+       }
+
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.h

Quoted 9 lines

drivers/hwmon/occ/occ_sysfs.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..7de92e7

drivers/hwmon/occ/occ_sysfs.h new file +4 -0
@@ -0,0 +1,44 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes for

the

Quoted 33 lines
Quoted 33 lines

OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ_sysfs.h new file +18 -0
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+#include <linux/hwmon.h>
+
+struct occ;
+struct device;
+
+#define OCC_HWMON_NAME_LENGTH  32
+
+struct occ_sysfs {
+       struct device *dev;
+       struct occ *occ;
+
+       char hwmon_name[OCC_HWMON_NAME_LENGTH + 1];
+       u32 *sensor_hwmon_configs;
+       struct hwmon_channel_info **occ_sensors;
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.h line 39

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.h new file +3 -0
+       struct hwmon_chip_info occ_info;
+       u16 user_powercap;
+};

Is this information used outside the sysfs code ? If not, it should be defined
in the source file and not be available outside of it.

OK. It is used in p8_occ_i2c.c but none of the members are accessed so we shouldn't need it defined there, just declared, you're right.

Quoted 9 lines
Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.h new file +5 -0
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                                 const u32 *sensor_hwmon_configs,
+                                 const char *name);
+#endif /* __OCC_SYSFS_H__ */
maintainer
#9 lore raw

Re: [PATCH linux v3 2/6] hwmon: occ: Add sysfs interface

Quoted 397 lines · Edward James
Quoted 19 lines · Guenter Roeck
Quoted 18 lines · On 01/16/2017 01:13 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 271
++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  44 +++++++
 4 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Quoted 7 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types · line 26

Quoted 7 lines
Documentation/hwmon/occ +0 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four types
 of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and

thresholds

Quoted 15 lines
Quoted 12 lines

used
internally on the OCC.

Documentation/hwmon/occ +4 -0
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to

userspace.

Documentation/hwmon/occ +3 -0
+
+The driver exports a number of sysfs files for each type of sensor. The
+sensor-specific files vary depending on the processor type, though many

of the

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be

used.

Quoted 3 lines
Quoted 3 lines

+Therefore, the frequency sensor on the OCC uses the "input" type sensor defined
+by the hwmon interface, rather than defining a new type of custom

sensor.

Quoted 18 lines
Quoted 18 lines
Documentation/hwmon/occ +2 -0
+
+Below are detailed the names and meaning of each sensor file for both

types of
+processors. All sensors are read-only unless otherwise specified. <x> indicates
+the hwmon index. sensor id indicates the unique internal OCC identifer. Please

Documentation/hwmon/occ +11 -0
+see the POWER OCC specification for details on all these sensor values.
+
+frequency:
+       all processors:
+               in<x>_input - frequency value
+               in<x>_label - sensor id
+temperature:
+       POWER8:
+               temp<x>_input - temperature value
+               temp<x>_label - sensor id
+       POWER9 (in addition to above):
Quoted 8 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 8 lines
Documentation/hwmon/occ +8 -0
+               temp<x>_type - FRU type
+
+power:
+       POWER8:
+               power<x>_input - power value
+               power<x>_label - sensor id
+               power<x>_average - accumulator
+               power<x>_average_interval - update tag (number of

samples

Quoted 35 lines
Quoted 35 lines

in

Documentation/hwmon/occ +8 -0
+                       accumulator)
+       POWER9:
+               power<x>_input - power value
+               power<x>_label - sensor id
+               power<x>_average_min - accumulator[0]
+               power<x>_average_max - accumulator[1] (64 bits total)
+               power<x>_average_interval - update tag
+               power<x>_reset_history - (function_id | (apss_channel <<

8)

Documentation/hwmon/occ +13 -0
+
+caps:
+       POWER8:
+               power<x>_cap - current powercap
+               power<x>_cap_max - max powercap
+               power<x>_cap_min - min powercap
+               power<x>_max - normal powercap
+               power<x>_alarm - user powercap, r/w
+       POWER9:
+               power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be

multiple
+OCCs present on the system, these two files are only present for the "master"

Documentation/hwmon/occ +3 -0
+OCC.
+       name - read the name of the driver
+       update_interval - read or write the minimum interval for polling

the

Documentation/hwmon/occ +2 -0
+               OCC.
+
 BMC - Host Communications
 -------------------------
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.c

Quoted 41 lines
Quoted 41 lines

new file mode 100644
index 0000000..2f20c40

drivers/hwmon/occ/occ_sysfs.c new file +4 -0
@@ -0,0 +1,271 @@
+/*
+ * occ_sysfs.c - OCC sysfs interface
+ *
+ * This file contains the methods and data structures for implementing

the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ_sysfs.c new file +17 -0
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_sysfs.h"
+
+#define RESP_RETURN_CMD_INVAL  0x13
+
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 39

Quoted 5 lines

+static int occ_hwmon_read(struct device *dev, enum hwmon_sensor_types type,

drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+                         u32 attr, int channel, long *val)
+{
+       int rc = 0;

Unnecessary initialization.

Got it.

Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+       struct occ_sysfs *driver = dev_get_drvdata(dev);
+       struct occ *occ = driver->occ;
+
+       switch (type) {
+       case hwmon_in:
+               rc = occ_get_sensor_value(occ, FREQ, channel, attr,

val);

Quoted 3 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               break;
+       case hwmon_temp:
+               rc = occ_get_sensor_value(occ, TEMP, channel, attr,

val);

Quoted 3 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               break;
+       case hwmon_power:
+               rc = occ_get_sensor_value(occ, POWER, channel, attr,

val);

Quoted 29 lines
Quoted 15 lines
drivers/hwmon/occ/occ_sysfs.c new file +15 -0
+               break;
+       default:
+               rc = -EOPNOTSUPP;
+       }
+
+       return rc;
+}
+
+static int occ_hwmon_read_string(struct device *dev,
+                                enum hwmon_sensor_types type, u32 attr,
+                                int channel, char **str)
+{
+       int rc;
+       unsigned long val = 0;
+

drivers/hwmon/occ/occ_sysfs.c line 76

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+       if (!((type == hwmon_in && attr == hwmon_in_label) ||
+           (type == hwmon_temp && attr == hwmon_temp_label) ||
+           (type == hwmon_power && attr == hwmon_power_label)))
+               return -EOPNOTSUPP;
+
+       rc = occ_hwmon_read(dev, type, attr, channel, &val);
+       if (rc < 0)
+               return rc;
+
+       rc = snprintf(*str, PAGE_SIZE - 1, "%ld", val);

val is unsigned long.

Quoted 5 lines

I am quite confused what this is for, though. The function is used for string attributes,
or in other words for labels. What is the benefit of having a label that returns the same
data as the value attribute ? Is this maybe a misunderstanding ?

So this does work, though I admit it's a bit confusing. occ_get_sensor_value returns either the value or the occ sensor id depending on the "type" passed in. I'll rename occ_get_sensor_value and add a comment.

Quoted 11 lines
Quoted 11 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       if (rc > 0)
+               rc = 0;
+
+       return rc;
+}
+
+static int occ_hwmon_write(struct device *dev, enum hwmon_sensor_types

type,

drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+                          u32 attr, int channel, long val)
+{
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 90

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+       int rc = 0;
+       struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+       if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
+               occ_set_update_interval(driver->occ, val);

As mentioned in the other patch, please drop. This is not the intended

use

Quoted 2 lines

case
for this attribute.

OK.

Quoted 15 lines
Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+               return 0;
+       } else if (type == hwmon_power && attr == hwmon_power_alarm) {
+               rc = occ_set_user_powercap(driver->occ, val);
+               if (rc) {
+                       if (rc == RESP_RETURN_CMD_INVAL) {
+                               dev_err(dev,
+                                       "set invalid powercap value:

%ld\n",

drivers/hwmon/occ/occ_sysfs.c

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+                                       val);
+                               return -EINVAL;
+                       }
+
+                       dev_err(dev, "set user powercap failed: 0x:%x

\n",

Quoted 28 lines
Quoted 28 lines

rc);

drivers/hwmon/occ/occ_sysfs.c new file +12 -0
+                       return rc;
+               }
+
+               driver->user_powercap = val;
+
+               return rc;
+       }
+
+       return -EOPNOTSUPP;
+}
+
+static umode_t occ_is_visible(const void *data, enum hwmon_sensor_types

type,

drivers/hwmon/occ/occ_sysfs.c new file +14 -0
+                             u32 attr, int channel)
+{
+       const struct occ_sysfs *driver = data;
+
+       switch (type) {
+       case hwmon_chip:
+               if (attr == hwmon_chip_update_interval)
+                       return S_IRUGO | S_IWUSR;
+               break;
+       case hwmon_in:
+               if (BIT(attr) & driver->sensor_hwmon_configs[0])
+                       return S_IRUGO;
+               break;
+       case hwmon_temp:
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               if (BIT(attr) & driver->sensor_hwmon_configs[1])
+                       return S_IRUGO;
+               break;
+       case hwmon_power:
+               /* user power limit */
+               if (attr == hwmon_power_alarm)
+                       return S_IRUGO | S_IWUSR;
+               else if ((BIT(attr) & driver->sensor_hwmon_configs[2])
||
>> +                        (BIT(attr) & driver->sensor_hwmon_configs[3]))
>> +                       return S_IRUGO;
>> +               break;
>> +       default:
>> +               return 0;
Quoted 54 lines

Might as well use break; here for consistency.

Quoted 49 lines
drivers/hwmon/occ/occ_sysfs.c new file +23 -0
+       }
+
+       return 0;
+}
+
+static const struct hwmon_ops occ_hwmon_ops = {
+       .is_visible = occ_is_visible,
+       .read = occ_hwmon_read,
+       .read_string = occ_hwmon_read_string,
+       .write = occ_hwmon_write,
+};
+
+static const u32 occ_chip_config[] = {
+       HWMON_C_UPDATE_INTERVAL,
+       0
+};
+
+static const struct hwmon_channel_info occ_chip = {
+       .type = hwmon_chip,
+       .config = occ_chip_config
+};
+
+static const enum hwmon_sensor_types

occ_sensor_types[MAX_OCC_SENSOR_TYPE] = {

drivers/hwmon/occ/occ_sysfs.c new file +16 -0
+       hwmon_in,
+       hwmon_temp,
+       hwmon_power,
+       hwmon_power
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                                 const u32 *sensor_hwmon_configs,
+                                 const char *name)
+{
+       bool master_occ = false;
+       int rc, i, j, sensor_num, index = 0, id;
+       char *brk;
+       struct occ_blocks *resp = NULL;
+       u32 *sensor_config;
+       struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct

occ_sysfs),

drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+                                              GFP_KERNEL);
+       if (!hwmon)
+               return ERR_PTR(-ENOMEM);
+
+       /* need space for null-termination and occ chip */
+       hwmon->occ_sensors =
+               devm_kzalloc(dev, sizeof(struct hwmon_channel_info *) *
+                            (MAX_OCC_SENSOR_TYPE + 2), GFP_KERNEL);
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 193

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+       if (!hwmon->occ_sensors)
+               return ERR_PTR(-ENOMEM);
+
+       hwmon->occ = occ;
+       hwmon->sensor_hwmon_configs = (u32 *)sensor_hwmon_configs;

Either this is a const or it isn't. If it isn't, it should not be passed

in

Quoted 2 lines

as const.
If it is, it can be declared const in struct occ_sysfs.

It is const, i'll fix.

Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 196

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+       hwmon->occ_info.ops = &occ_hwmon_ops;
+       hwmon->occ_info.info =
+               (const struct hwmon_channel_info **)hwmon->occ_sensors;

Why is this typecast needed ?

struct hwmon_chip_info {
        const struct hwmon_ops *ops;
        const struct hwmon_channel_info **info;
};

My compiler throws an error without a cast here.

Quoted 8 lines

occ_sysfs.c:195:23: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
| hwmon->occ_info.info = hwmon->occ_sensors;

occ_sensors is not const, as it's dynamically zero-allocated and then if we poll and get a sensor, we allocate a new hwmon_channel_info. Not sure what the best solution is here except to cast.

Your statement makes me quite concerned. "if we poll and get a sensor, we allocate a new hwmon_channel_info". Are you doing that before or after registering with the hwmon core ? Hopefully before.

Quoted 57 lines
Quoted 42 lines
Quoted 42 lines
drivers/hwmon/occ/occ_sysfs.c new file +26 -0
+
+       occ_get_response_blocks(occ, &resp);
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+               resp->sensor_block_id[i] = -1;
+
+       /* read sensor data from occ */
+       rc = occ_update_device(occ);
+       if (rc) {
+               dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+               return ERR_PTR(rc);
+       }
+       if (!resp->blocks)
+               return ERR_PTR(-ENOMEM);
+
+       master_occ = resp->sensor_block_id[CAPS] >= 0;
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+               id = resp->sensor_block_id[i];
+               if (id < 0)
+                       continue;
+
+               sensor_num = resp->blocks[id].header.sensor_num;
+               /* need null-termination */
+               sensor_config = devm_kzalloc(dev,
+                                            sizeof(u32) * (sensor_num +

1),

drivers/hwmon/occ/occ_sysfs.c new file +9 -0
+                                            GFP_KERNEL);
+               if (!sensor_config)
+                       return ERR_PTR(-ENOMEM);
+
+               for (j = 0; j < sensor_num; j++)
+                       sensor_config[j] = sensor_hwmon_configs[i];
+
+               hwmon->occ_sensors[index] =
+                       devm_kzalloc(dev, sizeof(struct

hwmon_channel_info),

drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+                                    GFP_KERNEL);
+               if (!hwmon->occ_sensors[index])
+                       return ERR_PTR(-ENOMEM);
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 239

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+
+               hwmon->occ_sensors[index]->type = occ_sensor_types[i];
+               hwmon->occ_sensors[index]->config = sensor_config;
+               index++;
+       }

I had the impression from patch 1 that the number of sensors can change

at

Quoted 2 lines

runtime.
If so, this doesn't really work well; the sysfs attributes won't be

updated

Quoted 3 lines

in this
case.

Quoted 9 lines
Quoted 1 line

Can it really happen that the sensor information can change ?

Well, the number of sensors won't change while we are running. But we don't know how many sensors we have when we start up. So this first poll will tell us how many we need and allow the driver to initialize the correct number of hwmon attributes. But yes, it shouldn't change while we
are running.

I'll have to look into the other patch again. I seem to recall that it does handle changing number of sensors, but I may be wrong.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+
+       /* only need one of these for any number of occs */
+       if (master_occ)
+               hwmon->occ_sensors[index] =
+                       (struct hwmon_channel_info *)&occ_chip;
Quoted 4 lines
Quoted 1 line

Why this typecast ?

occ_chip is const but my occ_sensors field isn't.

Hmm .. I'll have to have a closer look. Maybe I need to make the core fields non-const. Having to use typecasts isn't really desirable and defeats the purpose.

Guenter

Quoted 98 lines
Quoted 24 lines
Quoted 24 lines
drivers/hwmon/occ/occ_sysfs.c new file +11 -0
+
+       /* search for bad chars */
+       strncpy(hwmon->hwmon_name, name, OCC_HWMON_NAME_LENGTH);
+       brk = strpbrk(hwmon->hwmon_name, "-* \t\n");
+       while (brk) {
+               *brk = '_';
+               brk = strpbrk(brk,  "-* \t\n");
+       }
+
+       hwmon->dev = devm_hwmon_device_register_with_info(dev,
+

hwmon->hwmon_name,

drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+                                                         hwmon,
+

&hwmon->occ_info,

drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+                                                         NULL);
+       if (IS_ERR(hwmon->dev)) {
+               dev_err(dev, "cannot register hwmon device %s: %ld\n",
+                       hwmon->hwmon_name, PTR_ERR(hwmon->dev));
+               return ERR_CAST(hwmon->dev);
+       }
+
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.h

Quoted 9 lines

drivers/hwmon/occ/occ_sysfs.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..7de92e7

drivers/hwmon/occ/occ_sysfs.h new file +4 -0
@@ -0,0 +1,44 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes for

the

Quoted 33 lines
Quoted 33 lines

OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ_sysfs.h new file +18 -0
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+#include <linux/hwmon.h>
+
+struct occ;
+struct device;
+
+#define OCC_HWMON_NAME_LENGTH  32
+
+struct occ_sysfs {
+       struct device *dev;
+       struct occ *occ;
+
+       char hwmon_name[OCC_HWMON_NAME_LENGTH + 1];
+       u32 *sensor_hwmon_configs;
+       struct hwmon_channel_info **occ_sensors;
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.h line 39

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.h new file +3 -0
+       struct hwmon_chip_info occ_info;
+       u16 user_powercap;
+};

Is this information used outside the sysfs code ? If not, it should be defined
in the source file and not be available outside of it.

OK. It is used in p8_occ_i2c.c but none of the members are accessed so we shouldn't need it defined there, just declared, you're right.

Quoted 9 lines
Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.h new file +5 -0
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                                 const u32 *sensor_hwmon_configs,
+                                 const char *name);
+#endif /* __OCC_SYSFS_H__ */
#10 lore raw

Re: [PATCH linux v3 2/6] hwmon: occ: Add sysfs interface

Guenter Roeck <[email protected]> wrote on 01/23/2017 04:15:40 PM:

Quoted 4 lines · Edward James
Quoted 3 lines

On Sat, Jan 21, 2017 at 12:08 PM, Guenter Roeck <[email protected]>

Quoted 19 lines · wrote:
Quoted 19 lines
Quoted 19 lines · On 01/16/2017 01:13 PM, [email protected] wrote:
Quoted 18 lines

From: "Edward A. James" <[email protected]>

Add a generic mechanism to expose the sensors provided by the OCC in sysfs.

---
 Documentation/hwmon/occ       |  62 ++++++++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 271
++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  44 +++++++
 4 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four

Quoted 5 lines
Documentation/hwmon/occ +0 -0
index 79d1642..d0bdf06 100644
@@ -25,6 +25,68 @@ Currently, all versions of the OCC support four

types

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 26

Quoted 2 lines
Documentation/hwmon/occ +0 -0
 of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and

thresholds

Quoted 3 lines
Quoted 3 lines

used
internally on the OCC.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Documentation/hwmon/occ

Quoted 7 lines
Documentation/hwmon/occ +4 -0
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to

userspace.

Documentation/hwmon/occ +2 -0
+
+The driver exports a number of sysfs files for each type of sensor.

The

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+sensor-specific files vary depending on the processor type, though

many

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 4 lines

of the

Documentation/hwmon/occ +3 -0
+attributes are common for both the POWER8 and POWER9.
+
+The hwmon interface cannot define every type of sensor that may be

used.

Quoted 1 line
Quoted 1 line

+Therefore, the frequency sensor on the OCC uses the "input" type

sensor

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

defined
+by the hwmon interface, rather than defining a new type of custom

sensor.

Quoted 2 lines

Documentation/hwmon/occ

Quoted 2 lines
Documentation/hwmon/occ +2 -0
+
+Below are detailed the names and meaning of each sensor file for

both

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

types of
+processors. All sensors are read-only unless otherwise specified.

<x>

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

indicates
+the hwmon index. sensor id indicates the unique internal OCC

identifer.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Please
+see the POWER OCC specification for details on all these sensor

values.

Quoted 19 lines
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
Documentation/hwmon/occ +10 -0
+
+frequency:
+       all processors:
+               in<x>_input - frequency value
+               in<x>_label - sensor id
+temperature:
+       POWER8:
+               temp<x>_input - temperature value
+               temp<x>_label - sensor id
+       POWER9 (in addition to above):
Quoted 8 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 8 lines
Documentation/hwmon/occ +8 -0
+               temp<x>_type - FRU type
+
+power:
+       POWER8:
+               power<x>_input - power value
+               power<x>_label - sensor id
+               power<x>_average - accumulator
+               power<x>_average_interval - update tag (number of

samples

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 7 lines

in

Documentation/hwmon/occ +6 -0
+                       accumulator)
+       POWER9:
+               power<x>_input - power value
+               power<x>_label - sensor id
+               power<x>_average_min - accumulator[0]
+               power<x>_average_max - accumulator[1] (64 bits

total)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Documentation/hwmon/occ

Quoted 2 lines
Documentation/hwmon/occ +2 -0
+               power<x>_average_interval - update tag
+               power<x>_reset_history - (function_id |

(apss_channel <<

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

8)

Documentation/hwmon/occ +7 -0
+
+caps:
+       POWER8:
+               power<x>_cap - current powercap
+               power<x>_cap_max - max powercap
+               power<x>_cap_min - min powercap
+               power<x>_max - normal powercap
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Documentation/hwmon/occ +6 -0
+               power<x>_alarm - user powercap, r/w
+       POWER9:
+               power<x>_cap_alarm - user powercap source
+
+The driver also provides two sysfs entries through hwmon to better
+control the driver and monitor the master OCC. Though there may be

multiple
+OCCs present on the system, these two files are only present for

the

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power,

Quoted 4 lines

"master"

Documentation/hwmon/occ +3 -0
+OCC.
+       name - read the name of the driver
+       update_interval - read or write the minimum interval for

polling

Quoted 15 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines

the

Documentation/hwmon/occ +2 -0
+               OCC.
+
 BMC - Host Communications
 -------------------------
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/Makefile +1 -1
index 93cb52f..a6881f9 100644
@@ -1 +1 @@
-obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o
+obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.c

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/hwmon/occ/occ_sysfs.c line 4

Quoted 9 lines

new file mode 100644
index 0000000..2f20c40

drivers/hwmon/occ/occ_sysfs.c new file +4 -0
@@ -0,0 +1,271 @@
+/*
+ * occ_sysfs.c - OCC sysfs interface
+ *
+ * This file contains the methods and data structures for

implementing

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

the OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * it under the terms of the GNU General Public License as

published by

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * the Free Software Foundation; either version 2 of the License,

or

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_sysfs.h"
+
+#define RESP_RETURN_CMD_INVAL  0x13
+
+static int occ_hwmon_read(struct device *dev, enum

hwmon_sensor_types

Quoted 27 lines
Quoted 27 lines
Quoted 7 lines

drivers/hwmon/occ/occ_sysfs.c line 39

Quoted 4 lines

type,

drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+                         u32 attr, int channel, long *val)
+{
+       int rc = 0;

Unnecessary initialization.

Got it.

Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+       struct occ_sysfs *driver = dev_get_drvdata(dev);
+       struct occ *occ = driver->occ;
+
+       switch (type) {
+       case hwmon_in:
+               rc = occ_get_sensor_value(occ, FREQ, channel, attr,

val);

Quoted 3 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               break;
+       case hwmon_temp:
+               rc = occ_get_sensor_value(occ, TEMP, channel, attr,

val);

Quoted 3 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               break;
+       case hwmon_power:
+               rc = occ_get_sensor_value(occ, POWER, channel, attr,

val);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+               break;
+       default:
+               rc = -EOPNOTSUPP;
+       }
+
+       return rc;
+}
+
+static int occ_hwmon_read_string(struct device *dev,
+                                enum hwmon_sensor_types type, u32

attr,

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+                                int channel, char **str)
+{
+       int rc;
+       unsigned long val = 0;
+

drivers/hwmon/occ/occ_sysfs.c line 76

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+       if (!((type == hwmon_in && attr == hwmon_in_label) ||
+           (type == hwmon_temp && attr == hwmon_temp_label) ||
+           (type == hwmon_power && attr == hwmon_power_label)))
+               return -EOPNOTSUPP;
+
+       rc = occ_hwmon_read(dev, type, attr, channel, &val);
+       if (rc < 0)
+               return rc;
+
+       rc = snprintf(*str, PAGE_SIZE - 1, "%ld", val);

val is unsigned long.

Quoted 1 line
Quoted 1 line
Quoted 1 line

I am quite confused what this is for, though. The function is used

for

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

string attributes,
or in other words for labels. What is the benefit of having a label

that

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

returns the same
data as the value attribute ? Is this maybe a misunderstanding ?

So this does work, though I admit it's a bit confusing. occ_get_sensor_value returns either the value or the occ sensor id depending on the "type" passed in. I'll rename occ_get_sensor_value and add a comment.

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 7 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       if (rc > 0)
+               rc = 0;
+
+       return rc;
+}
+
+static int occ_hwmon_write(struct device *dev, enum

hwmon_sensor_types

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 7 lines

type,

drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+                          u32 attr, int channel, long val)
+{
+       int rc = 0;
+       struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+       if (type == hwmon_chip && attr ==

hwmon_chip_update_interval) {

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+ occ_set_update_interval(driver->occ, val);

As mentioned in the other patch, please drop. This is not the

intended

Quoted 10 lines
Quoted 10 lines

use

Quoted 2 lines

case
for this attribute.

OK.

Quoted 4 lines
Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+               return 0;
+       } else if (type == hwmon_power && attr == hwmon_power_alarm)

{

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 11 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+               rc = occ_set_user_powercap(driver->occ, val);
+               if (rc) {
+                       if (rc == RESP_RETURN_CMD_INVAL) {
+                               dev_err(dev,
+                                       "set invalid powercap value:

%ld\n",

drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+                                       val);
+                               return -EINVAL;
+                       }
+
+                       dev_err(dev, "set user powercap failed:

0x:%x

Quoted 14 lines
Quoted 14 lines

\n",

Quoted 5 lines
Quoted 5 lines

rc);

drivers/hwmon/occ/occ_sysfs.c new file +4 -0
+                       return rc;
+               }
+
+               driver->user_powercap = val;
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+               return rc;
+       }
+
+       return -EOPNOTSUPP;
+}
+
+static umode_t occ_is_visible(const void *data, enum

hwmon_sensor_types

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

type,

drivers/hwmon/occ/occ_sysfs.c new file +14 -0
+                             u32 attr, int channel)
+{
+       const struct occ_sysfs *driver = data;
+
+       switch (type) {
+       case hwmon_chip:
+               if (attr == hwmon_chip_update_interval)
+                       return S_IRUGO | S_IWUSR;
+               break;
+       case hwmon_in:
+               if (BIT(attr) & driver->sensor_hwmon_configs[0])
+                       return S_IRUGO;
+               break;
+       case hwmon_temp:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               if (BIT(attr) & driver->sensor_hwmon_configs[1])
+                       return S_IRUGO;
+               break;
+       case hwmon_power:
+               /* user power limit */
+               if (attr == hwmon_power_alarm)
+                       return S_IRUGO | S_IWUSR;
+               else if ((BIT(attr) & driver->sensor_hwmon_configs

[2])

Quoted 2 lines
Quoted 2 lines
||
>> +                        (BIT(attr) & driver->sensor_hwmon_configs

[3]))

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines

drivers/hwmon/occ/occ_sysfs.c line 141

Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.c new file +4 -0
+                       return S_IRUGO;
+               break;
+       default:
+               return 0;

Might as well use break; here for consistency.

Quoted 23 lines
drivers/hwmon/occ/occ_sysfs.c new file +23 -0
+       }
+
+       return 0;
+}
+
+static const struct hwmon_ops occ_hwmon_ops = {
+       .is_visible = occ_is_visible,
+       .read = occ_hwmon_read,
+       .read_string = occ_hwmon_read_string,
+       .write = occ_hwmon_write,
+};
+
+static const u32 occ_chip_config[] = {
+       HWMON_C_UPDATE_INTERVAL,
+       0
+};
+
+static const struct hwmon_channel_info occ_chip = {
+       .type = hwmon_chip,
+       .config = occ_chip_config
+};
+
+static const enum hwmon_sensor_types
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines

occ_sensor_types[MAX_OCC_SENSOR_TYPE] = {

drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       hwmon_in,
+       hwmon_temp,
+       hwmon_power,
+       hwmon_power
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ

*occ,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/hwmon/occ/occ_sysfs.c new file +9 -0
+                                 const u32 *sensor_hwmon_configs,
+                                 const char *name)
+{
+       bool master_occ = false;
+       int rc, i, j, sensor_num, index = 0, id;
+       char *brk;
+       struct occ_blocks *resp = NULL;
+       u32 *sensor_config;
+       struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines

occ_sysfs),

drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+                                              GFP_KERNEL);
+       if (!hwmon)
+               return ERR_PTR(-ENOMEM);
+
+       /* need space for null-termination and occ chip */
+       hwmon->occ_sensors =
+               devm_kzalloc(dev, sizeof(struct hwmon_channel_info

*) *

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/hwmon/occ/occ_sysfs.c line 193

Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+                            (MAX_OCC_SENSOR_TYPE + 2), GFP_KERNEL);
+       if (!hwmon->occ_sensors)
+               return ERR_PTR(-ENOMEM);
+
+       hwmon->occ = occ;
+       hwmon->sensor_hwmon_configs = (u32 *)sensor_hwmon_configs;

Either this is a const or it isn't. If it isn't, it should not be

passed

Quoted 11 lines
Quoted 11 lines

in

Quoted 2 lines

as const.
If it is, it can be declared const in struct occ_sysfs.

It is const, i'll fix.

Quoted 5 lines
Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+       hwmon->occ_info.ops = &occ_hwmon_ops;
+       hwmon->occ_info.info =
+               (const struct hwmon_channel_info **)hwmon->

occ_sensors;

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

Why is this typecast needed ?

struct hwmon_chip_info {
        const struct hwmon_ops *ops;
        const struct hwmon_channel_info **info;
};

My compiler throws an error without a cast here.

occ_sysfs.c:195:23: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
| hwmon->occ_info.info = hwmon->occ_sensors;

Quoted 8 lines
Quoted 4 lines

occ_sensors is not const, as it's dynamically zero-allocated and then if we poll and get a sensor, we allocate a new hwmon_channel_info. Not sure what the best solution is here except to cast.

Your statement makes me quite concerned. "if we poll and get a sensor, we allocate a new hwmon_channel_info". Are you doing that before or after registering with the hwmon core ? Hopefully before.

All before registration. There is no changing of hwmon attributes after we probe the driver. But we need the first poll response to allocate the hwmon_channel_info structures and others to match the sensors found in the first poll response.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 13 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+       occ_get_response_blocks(occ, &resp);
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; ++i)
+               resp->sensor_block_id[i] = -1;
+
+       /* read sensor data from occ */
+       rc = occ_update_device(occ);
+       if (rc) {
+               dev_err(dev, "cannot get occ sensor data: %d\n",

rc);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               return ERR_PTR(rc);
+       }
+       if (!resp->blocks)
+               return ERR_PTR(-ENOMEM);
+
+       master_occ = resp->sensor_block_id[CAPS] >= 0;
+
+       for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               id = resp->sensor_block_id[i];
+               if (id < 0)
+                       continue;
+
+               sensor_num = resp->blocks[id].header.sensor_num;
+               /* need null-termination */
+               sensor_config = devm_kzalloc(dev,
+                                            sizeof(u32) *

(sensor_num +

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

1),

drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+                                            GFP_KERNEL);
+               if (!sensor_config)
+                       return ERR_PTR(-ENOMEM);
+
+               for (j = 0; j < sensor_num; j++)
+                       sensor_config[j] = sensor_hwmon_configs[i];
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+               hwmon->occ_sensors[index] =
+                       devm_kzalloc(dev, sizeof(struct

hwmon_channel_info),

drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+                                    GFP_KERNEL);
+               if (!hwmon->occ_sensors[index])
+                       return ERR_PTR(-ENOMEM);
+
+               hwmon->occ_sensors[index]->type = occ_sensor_types

[i];

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/hwmon/occ/occ_sysfs.c line 239

Quoted 3 lines
drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+               hwmon->occ_sensors[index]->config = sensor_config;
+               index++;
+       }

I had the impression from patch 1 that the number of sensors can

change

Quoted 7 lines
Quoted 7 lines

at

Quoted 2 lines

runtime.
If so, this doesn't really work well; the sysfs attributes won't be

updated

Quoted 3 lines

in this
case.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Can it really happen that the sensor information can change ?

Well, the number of sensors won't change while we are running. But we don't know how many sensors we have when we start up. So this first poll will tell us how many we need and allow the driver to initialize the correct number of hwmon attributes. But yes, it shouldn't change

while

Quoted 4 lines
Quoted 3 lines

we
are running.

I'll have to look into the other patch again. I seem to recall that it

does

Quoted 1 line

handle changing number of sensors, but I may be wrong.

Yes, the core driver does handle varying sensors. But this was designed mainly
just to handle the first poll response. The core doesn't know which poll response is the first, so it made sense to just handle any number of sensors
each time we poll.

Also, the OCC specification doesn't indicate that the poll response *must* have the same number of sensors each time it's polled... so perhaps it may change in some conditions. But the hwmon attributes won't change.
That's OK for us.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+
+       /* only need one of these for any number of occs */
+       if (master_occ)
+               hwmon->occ_sensors[index] =
+                       (struct hwmon_channel_info *)&occ_chip;
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Why this typecast ?

occ_chip is const but my occ_sensors field isn't.

Hmm .. I'll have to have a closer look. Maybe I need to make the core

fields

Quoted 2 lines

non-const. Having to use typecasts isn't really desirable and defeats the purpose.

OK, agreed, thanks.

Eddie

Quoted 15 lines

Guenter

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+       /* search for bad chars */
+       strncpy(hwmon->hwmon_name, name, OCC_HWMON_NAME_LENGTH);
+       brk = strpbrk(hwmon->hwmon_name, "-* \t\n");
+       while (brk) {
+               *brk = '_';
+               brk = strpbrk(brk,  "-* \t\n");
+       }
+
+       hwmon->dev = devm_hwmon_device_register_with_info(dev,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines

+
hwmon->hwmon_name,

drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+                                                         hwmon,
+

&hwmon->occ_info,

drivers/hwmon/occ/occ_sysfs.c new file +3 -0
+                                                         NULL);
+       if (IS_ERR(hwmon->dev)) {
+               dev_err(dev, "cannot register hwmon device %s: %ld

\n",

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.c new file +4 -0
+                       hwmon->hwmon_name, PTR_ERR(hwmon->dev));
+               return ERR_CAST(hwmon->dev);
+       }
+
Quoted 8 lines

Review

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +7 -0
+       return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
(quoted diff) +0 -0

b/drivers/hwmon/occ/occ_sysfs.h

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/hwmon/occ/occ_sysfs.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..7de92e7

drivers/hwmon/occ/occ_sysfs.h new file +4 -0
@@ -0,0 +1,44 @@
+/*
+ * occ_sysfs.h - OCC sysfs interface
+ *
+ * This file contains the data structures and function prototypes

for

Quoted 7 lines
Quoted 7 lines

the

Quoted 6 lines
Quoted 6 lines

OCC
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * it under the terms of the GNU General Public License as

published by

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ * the Free Software Foundation; either version 2 of the License,

or

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */

drivers/hwmon/occ/occ_sysfs.h new file +16 -0
+
+#ifndef __OCC_SYSFS_H__
+#define __OCC_SYSFS_H__
+
+#include <linux/hwmon.h>
+
+struct occ;
+struct device;
+
+#define OCC_HWMON_NAME_LENGTH  32
+
+struct occ_sysfs {
+       struct device *dev;
+       struct occ *occ;
+
+       char hwmon_name[OCC_HWMON_NAME_LENGTH + 1];
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.h line 39

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.h new file +5 -0
+       u32 *sensor_hwmon_configs;
+       struct hwmon_channel_info **occ_sensors;
+       struct hwmon_chip_info occ_info;
+       u16 user_powercap;
+};

Is this information used outside the sysfs code ? If not, it should

be

Quoted 11 lines
Quoted 11 lines
Quoted 2 lines

defined
in the source file and not be available outside of it.

OK. It is used in p8_occ_i2c.c but none of the members are accessed so we shouldn't need it defined there, just declared, you're right.

Quoted 4 lines

drivers/hwmon/occ/occ_sysfs.h

Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.h new file +2 -0
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ

*occ,

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.h new file +3 -0
+                                 const u32 *sensor_hwmon_configs,
+                                 const char *name);
+#endif /* __OCC_SYSFS_H__ */
#11 lore raw

PATCH v3 3/6 hwmon: occ: Add I2C transport implementation for SCOM operations

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add functions to send SCOM operations over I2C bus. The BMC can communicate with the Power8 host processor over I2C, but needs to use SCOM operations in order to access the OCC register space.

---
 drivers/hwmon/occ/occ_scom_i2c.c | 72 ++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_scom_i2c.h | 26 +++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
drivers/hwmon/occ/occ_scom_i2c.c new file +72 -0
new file mode 100644
index 0000000..8b4ca13
@@ -0,0 +1,72 @@
+/*
+ * occ_scom_i2c.c - hwmon OCC driver
+ *
+ * This file contains the functions for performing SCOM operations over I2C bus
+ * to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include "occ_scom_i2c.h"
+#include "scom.h"
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+	ssize_t rc;
+	u64 buf;
+	struct i2c_client *client = bus;
+
+	rc = i2c_master_send(client, (const char *)&address, sizeof(u32));
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u32))
+		return -EIO;
+
+	rc = i2c_master_recv(client, (char *)&buf, sizeof(u64));
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u64))
+		return -EIO;
+
+	*data = be64_to_cpu(buf);
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);
+
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1)
+{
+	u32 buf[3];
+	ssize_t rc;
+	struct i2c_client *client = bus;
+
+	buf[0] = address;
+	buf[1] = data1;
+	buf[2] = data0;
+
+	rc = i2c_master_send(client, (const char *)buf, sizeof(u32) * 3);
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u32) * 3)
+		return -EIO;
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_i2c_putscom);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("I2C OCC SCOM transport");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_scom_i2c.h new file +26 -0
new file mode 100644
index 0000000..945739c
@@ -0,0 +1,26 @@
+/*
+ * occ_scom_i2c.h - hwmon OCC driver
+ *
+ * This file contains function protoypes for peforming SCOM operations over I2C
+ * bus to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SCOM_I2C_H__
+#define __OCC_SCOM_I2C_H__
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data);
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1);
+
+#endif /* __OCC_SCOM_I2C_H__ */
Signature · 1 line
1.9.1
maintainer
#12 lore raw

Re: [PATCH linux v3 3/6] hwmon: occ: Add I2C transport implementation for SCOM operations

Quoted 70 lines · On 01/16/2017 01:13 PM, [email protected]…

From: "Edward A. James" <eajames-r/[email protected]>

Add functions to send SCOM operations over I2C bus. The BMC can communicate with the Power8 host processor over I2C, but needs to use SCOM operations in order to access the OCC register space.

---
 drivers/hwmon/occ/occ_scom_i2c.c | 72 ++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_scom_i2c.h | 26 +++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
drivers/hwmon/occ/occ_scom_i2c.c new file +49 -0
new file mode 100644
index 0000000..8b4ca13
@@ -0,0 +1,72 @@
+/*
+ * occ_scom_i2c.c - hwmon OCC driver
+ *
+ * This file contains the functions for performing SCOM operations over I2C bus
+ * to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include "occ_scom_i2c.h"
+#include "scom.h"
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+	ssize_t rc;
+	u64 buf;
+	struct i2c_client *client = bus;
+
+	rc = i2c_master_send(client, (const char *)&address, sizeof(u32));
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u32))
+		return -EIO;
+
+	rc = i2c_master_recv(client, (char *)&buf, sizeof(u64));
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u64))
+		return -EIO;
+
+	*data = be64_to_cpu(buf);
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);
+

drivers/hwmon/occ/occ_scom_i2c.c line 59

Quoted 10 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +10 -0
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1)
+{
+	u32 buf[3];
+	ssize_t rc;
+	struct i2c_client *client = bus;
+
+	buf[0] = address;
+	buf[1] = data1;
+	buf[2] = data0;
+

Receive data is converted from be64 to host byte order, transmit data is sent as-is. Yet, if I recall correctly, some of the calling code passes in constants. Are you sure this works on both little and big endian systems ?

Quoted 36 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +13 -0
+	rc = i2c_master_send(client, (const char *)buf, sizeof(u32) * 3);
+	if (rc < 0)
+		return rc;
+	else if (rc != sizeof(u32) * 3)
+		return -EIO;
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_i2c_putscom);
+
+MODULE_AUTHOR("Eddie James <eajames-r/[email protected]>");
+MODULE_DESCRIPTION("I2C OCC SCOM transport");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_scom_i2c.h new file +16 -0
new file mode 100644
index 0000000..945739c
@@ -0,0 +1,26 @@
+/*
+ * occ_scom_i2c.h - hwmon OCC driver
+ *
+ * This file contains function protoypes for peforming SCOM operations over I2C
+ * bus to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

drivers/hwmon/occ/occ_scom_i2c.h line 26

Quoted 10 lines
drivers/hwmon/occ/occ_scom_i2c.h new file +10 -0
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SCOM_I2C_H__
+#define __OCC_SCOM_I2C_H__
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data);
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1);
+
+#endif /* __OCC_SCOM_I2C_H__ */

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw

Re: [PATCH linux v3 3/6] hwmon: occ: Add I2C transport implementation for SCOM operations

Quoted 16 lines · Guenter Roeck
Quoted 16 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +7 -0
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+     ssize_t rc;
+     u64 buf;
+     struct i2c_client *client = bus;
+
+     rc = i2c_master_send(client, (const char *)&address,

sizeof(u32));
+     if (rc < 0)
+             return rc;
+     else if (rc != sizeof(u32))
+             return -EIO;
+
+     rc = i2c_master_recv(client, (char *)&buf, sizeof(u64)); +     if (rc < 0)
+             return rc;

Quoted 8 lines

drivers/hwmon/occ/occ_scom_i2c.c line 48

Quoted 8 lines

+     else if (rc != sizeof(u64))
+             return -EIO;

drivers/hwmon/occ/occ_scom_i2c.c new file +6 -0
+
+     *data = be64_to_cpu(buf);
+
+     return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);

Additionally, this assumes that is is the only other user of that i2c path to P8. Something interleaving will break it. pdbg for example.

Talk to Alistair, the right thing here would probably be to have a separate driver that provides the XSCOM interface via i2c to both in-kernel and userspace, with proper arbitration.

An expedient might be to instead have the address and data cycles of the above be 2 i2c messages in one i2c request, thus being serialized somewhat atomically with other transactions to that i2c bus.

You may need to make sure the underlying i2c controller driver supports dual messages, and if it doesn't fix it. This is rather classic 4-bytes subaddress style i2c, it shouldn't be too hard.

Cheers,
Ben.

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#14 lore raw

Re: [PATCH linux v3 3/6] hwmon: occ: Add I2C transport implementation for SCOM operations

Benjamin Herrenschmidt <[email protected]> wrote on 01/21/2017 02:53:17 PM:

Quoted 33 lines · Guenter Roeck
Quoted 16 lines
Quoted 16 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +7 -0
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+     ssize_t rc;
+     u64 buf;
+     struct i2c_client *client = bus;
+
+     rc = i2c_master_send(client, (const char *)&address,

sizeof(u32));
+     if (rc < 0)
+             return rc;
+     else if (rc != sizeof(u32))
+             return -EIO;
+
+     rc = i2c_master_recv(client, (char *)&buf, sizeof(u64)); +     if (rc < 0)
+             return rc;

Quoted 8 lines

drivers/hwmon/occ/occ_scom_i2c.c line 48

Quoted 8 lines

+     else if (rc != sizeof(u64))
+             return -EIO;

drivers/hwmon/occ/occ_scom_i2c.c new file +6 -0
+
+     *data = be64_to_cpu(buf);
+
+     return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);

Additionally, this assumes that is is the only other user of that i2c path to P8. Something interleaving will break it. pdbg for example.

Talk to Alistair, the right thing here would probably be to have a separate driver that provides the XSCOM interface via i2c to both in-kernel and userspace, with proper arbitration.

Quoted 8 lines

An expedient might be to instead have the address and data cycles of the above be 2 i2c messages in one i2c request, thus being serialized somewhat atomically with other transactions to that i2c bus.

You may need to make sure the underlying i2c controller driver supports dual messages, and if it doesn't fix it. This is rather classic 4-bytes subaddress style i2c, it shouldn't be too hard.

Agreed, no one has experienced any issues, but it is a hole that should be addressed. I just tested with i2c_transfer instead of send and receive and it seems to work fine. I'll include that in the next patch set.

Thanks,
Eddie

Quoted 4 lines

Cheers,
Ben.

#15 lore raw

Re: [PATCH linux v3 3/6] hwmon: occ: Add I2C transport implementation for SCOM operations

In reply to Guenter Roeck

Guenter Roeck <[email protected]> wrote on 01/21/2017 12:11:21 PM:

Quoted 5 lines · On 01/16/2017 01:13 PM, [email protected] wrote:
Quoted 4 lines

From: "Edward A. James" <[email protected]>

Add functions to send SCOM operations over I2C bus. The BMC can communicate with the Power8 host processor over I2C, but needs to use

SCOM

Quoted 14 lines
Quoted 6 lines

operations in order to access the OCC register space.

---
 drivers/hwmon/occ/occ_scom_i2c.c | 72 +++++++++++++++++++++++++++

+++++++++++++

drivers/hwmon/occ/

Quoted 6 lines
 drivers/hwmon/occ/occ_scom_i2c.h | 26 +++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
drivers/hwmon/occ/ renamed from drivers/hwmon/occ/occ_scom_i2c.c +0 -0

occ_scom_i2c.c

Quoted 15 lines

drivers/hwmon/occ/occ_scom_i2c.c line 4

Quoted 9 lines

new file mode 100644
index 0000000..8b4ca13

drivers/hwmon/occ/occ_scom_i2c.c new file +4 -0
@@ -0,0 +1,72 @@
+/*
+ * occ_scom_i2c.c - hwmon OCC driver
+ *
+ * This file contains the functions for performing SCOM

operations over I2C bus

Quoted 5 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +5 -0
+ * to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_scom_i2c.c line 10

Quoted 1 line
drivers/hwmon/occ/occ_scom_i2c.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 47 lines
Quoted 47 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +47 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include "occ_scom_i2c.h"
+#include "scom.h"
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+   ssize_t rc;
+   u64 buf;
+   struct i2c_client *client = bus;
+
+   rc = i2c_master_send(client, (const char *)&address, sizeof(u32));
+   if (rc < 0)
+      return rc;
+   else if (rc != sizeof(u32))
+      return -EIO;
+
+   rc = i2c_master_recv(client, (char *)&buf, sizeof(u64));
+   if (rc < 0)
+      return rc;
+   else if (rc != sizeof(u64))
+      return -EIO;
+
+   *data = be64_to_cpu(buf);
+
+   return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);
+
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1)
+{
+   u32 buf[3];
+   ssize_t rc;
+   struct i2c_client *client = bus;
+
+   buf[0] = address;
+   buf[1] = data1;
Quoted 8 lines

drivers/hwmon/occ/occ_scom_i2c.c line 59

Quoted 2 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +2 -0
+   buf[2] = data0;
+

Receive data is converted from be64 to host byte order, transmit data is sent as-is.
Yet, if I recall correctly, some of the calling code passes in constants. Are you sure
this works on both little and big endian systems ?

Not at all sure, no. I'll have another look at this. The constants aren't an issue I believe, for putscom, as we don't modify data0/1. Maybe I'll make
data0 and data1 const anyway, we shoulnd't be modifying those.

Thanks,
Eddie

Quoted 16 lines

drivers/hwmon/occ/

Quoted 15 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +13 -0
+   rc = i2c_master_send(client, (const char *)buf, sizeof(u32) * 3);
+   if (rc < 0)
+      return rc;
+   else if (rc != sizeof(u32) * 3)
+      return -EIO;
+
+   return 0;
+}
+EXPORT_SYMBOL(occ_i2c_putscom);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("I2C OCC SCOM transport");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/ renamed from drivers/hwmon/occ/occ_scom_i2c.h +0 -0

occ_scom_i2c.h

Quoted 15 lines

drivers/hwmon/occ/occ_scom_i2c.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..945739c

drivers/hwmon/occ/occ_scom_i2c.h new file +4 -0
@@ -0,0 +1,26 @@
+/*
+ * occ_scom_i2c.h - hwmon OCC driver
+ *
+ * This file contains function protoypes for peforming SCOM

operations over I2C

Quoted 5 lines
drivers/hwmon/occ/occ_scom_i2c.h new file +5 -0
+ * bus to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_scom_i2c.h line 10

Quoted 1 line
drivers/hwmon/occ/occ_scom_i2c.h new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 18 lines
Quoted 17 lines
drivers/hwmon/occ/occ_scom_i2c.h new file +16 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SCOM_I2C_H__
+#define __OCC_SCOM_I2C_H__
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data);
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1);
+
+#endif /* __OCC_SCOM_I2C_H__ */
#16 lore raw

Re: [PATCH linux v3 3/6] hwmon: occ: Add I2C transport implementation for SCOM operations

In reply to Guenter Roeck

Guenter Roeck <[email protected]> wrote on 01/21/2017 12:11:21 PM:

Quoted 5 lines · On 01/16/2017 01:13 PM, [email protected] wrote:
Quoted 4 lines

From: "Edward A. James" <[email protected]>

Add functions to send SCOM operations over I2C bus. The BMC can communicate with the Power8 host processor over I2C, but needs to use

SCOM

Quoted 14 lines
Quoted 6 lines

operations in order to access the OCC register space.

---
 drivers/hwmon/occ/occ_scom_i2c.c | 72 +++++++++++++++++++++++++++

+++++++++++++

drivers/hwmon/occ/

Quoted 6 lines
 drivers/hwmon/occ/occ_scom_i2c.h | 26 +++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.c
 create mode 100644 drivers/hwmon/occ/occ_scom_i2c.h
drivers/hwmon/occ/ renamed from drivers/hwmon/occ/occ_scom_i2c.c +0 -0

occ_scom_i2c.c

Quoted 15 lines

drivers/hwmon/occ/occ_scom_i2c.c line 4

Quoted 9 lines

new file mode 100644
index 0000000..8b4ca13

drivers/hwmon/occ/occ_scom_i2c.c new file +4 -0
@@ -0,0 +1,72 @@
+/*
+ * occ_scom_i2c.c - hwmon OCC driver
+ *
+ * This file contains the functions for performing SCOM

operations over I2C bus

Quoted 5 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +5 -0
+ * to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_scom_i2c.c line 10

Quoted 1 line
drivers/hwmon/occ/occ_scom_i2c.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 47 lines
Quoted 47 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +47 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include "occ_scom_i2c.h"
+#include "scom.h"
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+   ssize_t rc;
+   u64 buf;
+   struct i2c_client *client = bus;
+
+   rc = i2c_master_send(client, (const char *)&address, sizeof(u32));
+   if (rc < 0)
+      return rc;
+   else if (rc != sizeof(u32))
+      return -EIO;
+
+   rc = i2c_master_recv(client, (char *)&buf, sizeof(u64));
+   if (rc < 0)
+      return rc;
+   else if (rc != sizeof(u64))
+      return -EIO;
+
+   *data = be64_to_cpu(buf);
+
+   return 0;
+}
+EXPORT_SYMBOL(occ_i2c_getscom);
+
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1)
+{
+   u32 buf[3];
+   ssize_t rc;
+   struct i2c_client *client = bus;
+
+   buf[0] = address;
+   buf[1] = data1;
Quoted 8 lines

drivers/hwmon/occ/occ_scom_i2c.c line 59

Quoted 2 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +2 -0
+   buf[2] = data0;
+

Receive data is converted from be64 to host byte order, transmit data is sent as-is.
Yet, if I recall correctly, some of the calling code passes in constants. Are you sure
this works on both little and big endian systems ?

Spent a lot of time testing this and trying to make it more endian agnostic. I didn't have any luck. Most of the data passed to putscom is just magic number constants initializing and setting up the OCC command. See all the #defines at the top of occ.c. These have to be hardcoded to some endianness anyway, so may as well just hardcode them to just work as-is.

If you flip the byte order of all those constants and then use cpu_to_be, I still don't think that works on big-endian systems? I couldn't get that working today.

This code will never run on anything except a bmc, which is an arm core. If the bmc is changing dramatically enough to change endianness, I suspect this driver may have other problems.

What do you think Guenter? Can we leave those constants as-is and do the putscoms raw?

Thanks,
Eddie

Quoted 16 lines

drivers/hwmon/occ/

Quoted 15 lines
drivers/hwmon/occ/occ_scom_i2c.c new file +13 -0
+   rc = i2c_master_send(client, (const char *)buf, sizeof(u32) * 3);
+   if (rc < 0)
+      return rc;
+   else if (rc != sizeof(u32) * 3)
+      return -EIO;
+
+   return 0;
+}
+EXPORT_SYMBOL(occ_i2c_putscom);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("I2C OCC SCOM transport");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/ renamed from drivers/hwmon/occ/occ_scom_i2c.h +0 -0

occ_scom_i2c.h

Quoted 15 lines

drivers/hwmon/occ/occ_scom_i2c.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..945739c

drivers/hwmon/occ/occ_scom_i2c.h new file +4 -0
@@ -0,0 +1,26 @@
+/*
+ * occ_scom_i2c.h - hwmon OCC driver
+ *
+ * This file contains function protoypes for peforming SCOM

operations over I2C

Quoted 5 lines
drivers/hwmon/occ/occ_scom_i2c.h new file +5 -0
+ * bus to access the OCC.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_scom_i2c.h line 10

Quoted 1 line
drivers/hwmon/occ/occ_scom_i2c.h new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 18 lines
Quoted 17 lines
drivers/hwmon/occ/occ_scom_i2c.h new file +16 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_SCOM_I2C_H__
+#define __OCC_SCOM_I2C_H__
+
+int occ_i2c_getscom(void *bus, u32 address, u64 *data);
+int occ_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1);
+
+#endif /* __OCC_SCOM_I2C_H__ */
#17 lore raw

PATCH v3 4/6 hwmon: occ: Add callbacks for parsing P8 OCC datastructures

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add functions to parse the data structures that are specific to the OCC on the POWER8 processor. These are the sensor data structures, including temperature, frequency, power, and "caps."

---
 Documentation/hwmon/occ    |   9 ++
 drivers/hwmon/occ/occ_p8.c | 247 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_p8.h |  30 ++++++
 3 files changed, 286 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_p8.c
 create mode 100644 drivers/hwmon/occ/occ_p8.h
Documentation/hwmon/occ +9 -0
index d0bdf06..143951e 100644
@@ -25,6 +25,15 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+The format for the POWER8 OCC sensor data can be found in the P8 OCC
+specification:
+github.com/open-power/docs/blob/master/occ/OCC_OpenPwr_FW_Interfaces.pdf
+This document provides the details of the OCC sensors: power, frequency,
+temperature, and caps. These sensor formats are specific to the POWER8 OCC. A
+number of data structures, such as command format, response headers, and the
+like, are also defined in this specification, and are common to both POWER8 and
+POWER9 OCCs.
+
 sysfs Entries
 -------------
 
drivers/hwmon/occ/occ_p8.c new file +247 -0

247 lines not on this page · raw message

drivers/hwmon/occ/occ_p8.h new file +30 -0
new file mode 100644
index 0000000..3fe6417
@@ -0,0 +1,30 @@
+/*
+ * occ_p8.h - OCC hwmon driver
+ *
+ * This file contains Power8-specific function prototypes
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_P8_H__
+#define __OCC_P8_H__
+
+#include "scom.h"
+
+struct device;
+
+const u32 *p8_get_sensor_hwmon_configs(void);
+struct occ *p8_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+
+#endif /* __OCC_P8_H__ */
Signature · 1 line
1.9.1
maintainer
#18 lore raw

Re: [PATCH linux v3 4/6] hwmon: occ: Add callbacks for parsing P8 OCC datastructures

Quoted 192 lines · On 01/16/2017 01:13 PM, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add functions to parse the data structures that are specific to the OCC on the POWER8 processor. These are the sensor data structures, including temperature, frequency, power, and "caps."

---
 Documentation/hwmon/occ    |   9 ++
 drivers/hwmon/occ/occ_p8.c | 247 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_p8.h |  30 ++++++
 3 files changed, 286 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_p8.c
 create mode 100644 drivers/hwmon/occ/occ_p8.h
Documentation/hwmon/occ +9 -0
index d0bdf06..143951e 100644
@@ -25,6 +25,15 @@ Currently, all versions of the OCC support four types of sensor data: power,
 temperature, frequency, and "caps," which indicate limits and thresholds used
 internally on the OCC.
 
+The format for the POWER8 OCC sensor data can be found in the P8 OCC
+specification:
+github.com/open-power/docs/blob/master/occ/OCC_OpenPwr_FW_Interfaces.pdf
+This document provides the details of the OCC sensors: power, frequency,
+temperature, and caps. These sensor formats are specific to the POWER8 OCC. A
+number of data structures, such as command format, response headers, and the
+like, are also defined in this specification, and are common to both POWER8 and
+POWER9 OCCs.
+
 sysfs Entries
 -------------
 
drivers/hwmon/occ/occ_p8.c new file +150 -0
new file mode 100644
index 0000000..32215ed
@@ -0,0 +1,247 @@
+/*
+ * occ_p8.c - OCC hwmon driver
+ *
+ * This file contains the Power8-specific methods and data structures for
+ * the OCC hwmon driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <asm/unaligned.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_p8.h"
+
+/* P8 OCC sensor data format */
+struct p8_occ_sensor {
+	u16 sensor_id;
+	u16 value;
+};
+
+struct p8_power_sensor {
+	u16 sensor_id;
+	u32 update_tag;
+	u32 accumulator;
+	u16 value;
+};
+
+struct p8_caps_sensor {
+	u16 curr_powercap;
+	u16 curr_powerreading;
+	u16 norm_powercap;
+	u16 max_powercap;
+	u16 min_powercap;
+	u16 user_powerlimit;
+};
+
+static const u32 p8_sensor_hwmon_configs[MAX_OCC_SENSOR_TYPE] = {
+	HWMON_I_INPUT | HWMON_I_LABEL,	/* freq: value | label */
+	HWMON_T_INPUT | HWMON_T_LABEL,	/* temp: value | label */
+	/* power: value | label | accumulator | update_tag */
+	HWMON_P_INPUT | HWMON_P_LABEL | HWMON_P_AVERAGE |
+		HWMON_P_AVERAGE_INTERVAL,
+	/* caps: curr | max | min | norm | user */
+	HWMON_P_CAP | HWMON_P_CAP_MAX | HWMON_P_CAP_MIN | HWMON_P_MAX |
+		HWMON_P_ALARM,
+};
+
+void p8_parse_sensor(u8 *data, void *sensor, int sensor_type, int off,
+		     int snum)
+{
+	switch (sensor_type) {
+	case FREQ:
+	case TEMP:
+	{
+		struct p8_occ_sensor *os =
+			&(((struct p8_occ_sensor *)sensor)[snum]);
+
+		os->sensor_id = be16_to_cpu(get_unaligned((u16 *)&data[off]));
+		os->value = be16_to_cpu(get_unaligned((u16 *)&data[off + 2]));
+	}
+		break;
+	case POWER:
+	{
+		struct p8_power_sensor *ps =
+			&(((struct p8_power_sensor *)sensor)[snum]);
+
+		ps->sensor_id = be16_to_cpu(get_unaligned((u16 *)&data[off]));
+		ps->update_tag =
+			be32_to_cpu(get_unaligned((u32 *)&data[off + 2]));
+		ps->accumulator =
+			be32_to_cpu(get_unaligned((u32 *)&data[off + 6]));
+		ps->value = be16_to_cpu(get_unaligned((u16 *)&data[off + 10]));
+	}
+		break;
+	case CAPS:
+	{
+		struct p8_caps_sensor *cs =
+			&(((struct p8_caps_sensor *)sensor)[snum]);
+
+		cs->curr_powercap =
+			be16_to_cpu(get_unaligned((u16 *)&data[off]));
+		cs->curr_powerreading =
+			be16_to_cpu(get_unaligned((u16 *)&data[off + 2]));
+		cs->norm_powercap =
+			be16_to_cpu(get_unaligned((u16 *)&data[off + 4]));
+		cs->max_powercap =
+			be16_to_cpu(get_unaligned((u16 *)&data[off + 6]));
+		cs->min_powercap =
+			be16_to_cpu(get_unaligned((u16 *)&data[off + 8]));
+		cs->user_powerlimit =
+			be16_to_cpu(get_unaligned((u16 *)&data[off + 10]));
+	}
+		break;
+	};
+}
+
+void *p8_alloc_sensor(int sensor_type, int num_sensors)
+{
+	switch (sensor_type) {
+	case FREQ:
+	case TEMP:
+		return kcalloc(num_sensors, sizeof(struct p8_occ_sensor),
+			       GFP_KERNEL);
+	case POWER:
+		return kcalloc(num_sensors, sizeof(struct p8_power_sensor),
+			       GFP_KERNEL);
+	case CAPS:
+		return kcalloc(num_sensors, sizeof(struct p8_caps_sensor),
+			       GFP_KERNEL);
+	default:
+		return NULL;
+	}
+}
+
+int p8_get_sensor(struct occ *driver, int sensor_type, int sensor_num,
+		  u32 hwmon, long *val)
+{
+	int rc = 0;
+	void *sensor;
+
+	if (sensor_type == POWER) {
+		if (hwmon == hwmon_power_cap || hwmon == hwmon_power_cap_max ||
+		    hwmon == hwmon_power_cap_min || hwmon == hwmon_power_max ||
+		    hwmon == hwmon_power_alarm)
+			sensor_type = CAPS;
+	}
+
+	sensor = occ_get_sensor(driver, sensor_type);
+	if (!sensor)
+		return -ENODEV;
+
+	switch (sensor_type) {

drivers/hwmon/occ/occ_p8.c line 160

Quoted 10 lines
drivers/hwmon/occ/occ_p8.c new file +10 -0
+	case FREQ:
+	case TEMP:
+	{
+		struct p8_occ_sensor *os =
+			&(((struct p8_occ_sensor *)sensor)[sensor_num]);
+
+		if (hwmon == hwmon_in_input || hwmon == hwmon_temp_input)
+			*val = os->value;
+		else if (hwmon == hwmon_in_label || hwmon == hwmon_temp_label)
+			*val = os->sensor_id;

Guess that explains the numeric labels I wondered about earlier. Are you really sure you want to have such numbered labels, ie temp1_label = "1" and so on ? Your call, really, just wondering.

Quoted 124 lines
drivers/hwmon/occ/occ_p8.c new file +87 -0
+		else
+			rc = -EOPNOTSUPP;
+	}
+		break;
+	case POWER:
+	{
+		struct p8_power_sensor *ps =
+			&(((struct p8_power_sensor *)sensor)[sensor_num]);
+
+		switch (hwmon) {
+		case hwmon_power_input:
+			*val = ps->value;
+			break;
+		case hwmon_power_label:
+			*val = ps->sensor_id;
+			break;
+		case hwmon_power_average:
+			*val = ps->accumulator;
+			break;
+		case hwmon_power_average_interval:
+			*val = ps->update_tag;
+			break;
+		default:
+			rc = -EOPNOTSUPP;
+		}
+	}
+		break;
+	case CAPS:
+	{
+		struct p8_caps_sensor *cs =
+			&(((struct p8_caps_sensor *)sensor)[sensor_num]);
+
+		switch (hwmon) {
+		case hwmon_power_cap:
+			*val = cs->curr_powercap;
+			break;
+		case hwmon_power_cap_max:
+			*val = cs->max_powercap;
+			break;
+		case hwmon_power_cap_min:
+			*val = cs->min_powercap;
+			break;
+		case hwmon_power_max:
+			*val = cs->norm_powercap;
+			break;
+		case hwmon_power_alarm:
+			*val = cs->user_powerlimit;
+			break;
+		default:
+			rc = -EOPNOTSUPP;
+		}
+	}
+		break;
+	default:
+		rc = -EINVAL;
+	}
+
+	return rc;
+}
+
+static const struct occ_ops p8_ops = {
+	.parse_sensor = p8_parse_sensor,
+	.alloc_sensor = p8_alloc_sensor,
+	.get_sensor = p8_get_sensor,
+};
+
+static const struct occ_config p8_config = {
+	.command_addr = 0xFFFF6000,
+	.response_addr = 0xFFFF7000,
+};
+
+const u32 *p8_get_sensor_hwmon_configs()
+{
+	return p8_sensor_hwmon_configs;
+}
+EXPORT_SYMBOL(p8_get_sensor_hwmon_configs);
+
+struct occ *p8_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops)
+{
+	return occ_start(dev, bus, bus_ops, &p8_ops, &p8_config);
+}
+EXPORT_SYMBOL(p8_occ_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("P8 OCC sensors");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_p8.h new file +30 -0
new file mode 100644
index 0000000..3fe6417
@@ -0,0 +1,30 @@
+/*
+ * occ_p8.h - OCC hwmon driver
+ *
+ * This file contains Power8-specific function prototypes
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_P8_H__
+#define __OCC_P8_H__
+
+#include "scom.h"
+
+struct device;
+
+const u32 *p8_get_sensor_hwmon_configs(void);
+struct occ *p8_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+
+#endif /* __OCC_P8_H__ */
#19 lore raw

Re: [PATCH linux v3 4/6] hwmon: occ: Add callbacks for parsing P8 OCC datastructures

Guenter Roeck <[email protected]> wrote on 01/21/2017 12:18:23 PM:

Quoted 4 lines · On 01/16/2017 01:13 PM, [email protected] wrote:
Quoted 3 lines

From: "Edward A. James" <[email protected]>

Add functions to parse the data structures that are specific to the OCC

on

Quoted 20 lines
Quoted 8 lines

the POWER8 processor. These are the sensor data structures, including temperature, frequency, power, and "caps."

---
 Documentation/hwmon/occ    |   9 ++
 drivers/hwmon/occ/occ_p8.c | 247 ++++++++++++++++++++++++++++++++

+++++++++++++

Documentation/hwmon/occ Currently, all versions of the OCC support four

Quoted 10 lines
 drivers/hwmon/occ/occ_p8.h |  30 ++++++
 3 files changed, 286 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_p8.c
 create mode 100644 drivers/hwmon/occ/occ_p8.h
Documentation/hwmon/occ +0 -0
index d0bdf06..143951e 100644
@@ -25,6 +25,15 @@ Currently, all versions of the OCC support four

types of sensor data: power,

Quoted 7 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 25

Quoted 1 line
Documentation/hwmon/occ +0 -0
 temperature, frequency, and "caps," which indicate limits and

thresholds used

Documentation/hwmon/occ Currently, all versions of the OCC support four types of sensor data: power, · line 39

Quoted 5 lines
Documentation/hwmon/occ +2 -0
 internally on the OCC.
 
+The format for the POWER8 OCC sensor data can be found in the P8 OCC
+specification:
 

+github.com/open-power/docs/blob/master/occ/OCC_OpenPwr_FW_Interfaces.pdf

Quoted 1 line

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 31

Quoted 1 line
Documentation/hwmon/occ +1 -0
+This document provides the details of the OCC sensors: power,

frequency,

Quoted 11 lines

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 32

Quoted 1 line
Documentation/hwmon/occ +1 -0
+temperature, and caps. These sensor formats are specific to the

POWER8 OCC. A

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 33

Quoted 1 line
Documentation/hwmon/occ +1 -0
+number of data structures, such as command format, response

headers, and the

Documentation/hwmon/occ Currently, all versions of the OCC support four · line 34

Quoted 1 line
Documentation/hwmon/occ +1 -0
+like, are also defined in this specification, and are common to

both POWER8 and

Quoted 5 lines
Documentation/hwmon/occ +2 -0
+POWER9 OCCs.
+
 sysfs Entries
 -------------
Quoted 10 lines

drivers/hwmon/occ/occ_p8.c line 4

Quoted 10 lines
drivers/hwmon/occ/occ_p8.c new file +4 -0
new file mode 100644
index 0000000..32215ed
@@ -0,0 +1,247 @@
+/*
+ * occ_p8.c - OCC hwmon driver
+ *
+ * This file contains the Power8-specific methods and data structures

for

Quoted 5 lines

drivers/hwmon/occ/occ_p8.c

Quoted 5 lines
drivers/hwmon/occ/occ_p8.c new file +5 -0
+ * the OCC hwmon driver.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_p8.c line 10

Quoted 1 line
drivers/hwmon/occ/occ_p8.c new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 148 lines
Quoted 148 lines
drivers/hwmon/occ/occ_p8.c new file +148 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <asm/unaligned.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include "occ.h"
+#include "occ_p8.h"
+
+/* P8 OCC sensor data format */
+struct p8_occ_sensor {
+   u16 sensor_id;
+   u16 value;
+};
+
+struct p8_power_sensor {
+   u16 sensor_id;
+   u32 update_tag;
+   u32 accumulator;
+   u16 value;
+};
+
+struct p8_caps_sensor {
+   u16 curr_powercap;
+   u16 curr_powerreading;
+   u16 norm_powercap;
+   u16 max_powercap;
+   u16 min_powercap;
+   u16 user_powerlimit;
+};
+
+static const u32 p8_sensor_hwmon_configs[MAX_OCC_SENSOR_TYPE] = {
+   HWMON_I_INPUT | HWMON_I_LABEL,   /* freq: value | label */
+   HWMON_T_INPUT | HWMON_T_LABEL,   /* temp: value | label */
+   /* power: value | label | accumulator | update_tag */
+   HWMON_P_INPUT | HWMON_P_LABEL | HWMON_P_AVERAGE |
+      HWMON_P_AVERAGE_INTERVAL,
+   /* caps: curr | max | min | norm | user */
+   HWMON_P_CAP | HWMON_P_CAP_MAX | HWMON_P_CAP_MIN | HWMON_P_MAX |
+      HWMON_P_ALARM,
+};
+
+void p8_parse_sensor(u8 *data, void *sensor, int sensor_type, int off,
+           int snum)
+{
+   switch (sensor_type) {
+   case FREQ:
+   case TEMP:
+   {
+      struct p8_occ_sensor *os =
+         &(((struct p8_occ_sensor *)sensor)[snum]);
+
+      os->sensor_id = be16_to_cpu(get_unaligned((u16 *)&data[off]));
+      os->value = be16_to_cpu(get_unaligned((u16 *)&data[off + 2]));
+   }
+      break;
+   case POWER:
+   {
+      struct p8_power_sensor *ps =
+         &(((struct p8_power_sensor *)sensor)[snum]);
+
+      ps->sensor_id = be16_to_cpu(get_unaligned((u16 *)&data[off]));
+      ps->update_tag =
+         be32_to_cpu(get_unaligned((u32 *)&data[off + 2]));
+      ps->accumulator =
+         be32_to_cpu(get_unaligned((u32 *)&data[off + 6]));
+      ps->value = be16_to_cpu(get_unaligned((u16 *)&data[off + 10]));
+   }
+      break;
+   case CAPS:
+   {
+      struct p8_caps_sensor *cs =
+         &(((struct p8_caps_sensor *)sensor)[snum]);
+
+      cs->curr_powercap =
+         be16_to_cpu(get_unaligned((u16 *)&data[off]));
+      cs->curr_powerreading =
+         be16_to_cpu(get_unaligned((u16 *)&data[off + 2]));
+      cs->norm_powercap =
+         be16_to_cpu(get_unaligned((u16 *)&data[off + 4]));
+      cs->max_powercap =
+         be16_to_cpu(get_unaligned((u16 *)&data[off + 6]));
+      cs->min_powercap =
+         be16_to_cpu(get_unaligned((u16 *)&data[off + 8]));
+      cs->user_powerlimit =
+         be16_to_cpu(get_unaligned((u16 *)&data[off + 10]));
+   }
+      break;
+   };
+}
+
+void *p8_alloc_sensor(int sensor_type, int num_sensors)
+{
+   switch (sensor_type) {
+   case FREQ:
+   case TEMP:
+      return kcalloc(num_sensors, sizeof(struct p8_occ_sensor),
+                GFP_KERNEL);
+   case POWER:
+      return kcalloc(num_sensors, sizeof(struct p8_power_sensor),
+                GFP_KERNEL);
+   case CAPS:
+      return kcalloc(num_sensors, sizeof(struct p8_caps_sensor),
+                GFP_KERNEL);
+   default:
+      return NULL;
+   }
+}
+
+int p8_get_sensor(struct occ *driver, int sensor_type, int sensor_num,
+        u32 hwmon, long *val)
+{
+   int rc = 0;
+   void *sensor;
+
+   if (sensor_type == POWER) {
+      if (hwmon == hwmon_power_cap || hwmon == hwmon_power_cap_max ||
+          hwmon == hwmon_power_cap_min || hwmon == hwmon_power_max ||
+          hwmon == hwmon_power_alarm)
+         sensor_type = CAPS;
+   }
+
+   sensor = occ_get_sensor(driver, sensor_type);
+   if (!sensor)
+      return -ENODEV;
+
+   switch (sensor_type) {
+   case FREQ:
+   case TEMP:
+   {
+      struct p8_occ_sensor *os =
+         &(((struct p8_occ_sensor *)sensor)[sensor_num]);
+
+      if (hwmon == hwmon_in_input || hwmon == hwmon_temp_input)
+         *val = os->value;
Quoted 8 lines

drivers/hwmon/occ/occ_p8.c line 160

Quoted 2 lines
drivers/hwmon/occ/occ_p8.c new file +2 -0
+      else if (hwmon == hwmon_in_label || hwmon == hwmon_temp_label)
+         *val = os->sensor_id;

Guess that explains the numeric labels I wondered about earlier. Are you really sure
you want to have such numbered labels, ie temp1_label = "1" and so on ? Your call,
really, just wondering.

Yes, that is our requirement I think. They aren't just arbitrary numbers, the sensor_id is read off the OCC. It corresponds to some identifier that users can look up on our side with the OCC documentation. It is pretty meaningless in the driver though, agreed. But it's system-specific, so without a bunch of tables stored in the driver, not sure what else we could do.

Thanks,

Eddie

Quoted 94 lines
Quoted 94 lines
drivers/hwmon/occ/occ_p8.c new file +87 -0
+      else
+         rc = -EOPNOTSUPP;
+   }
+      break;
+   case POWER:
+   {
+      struct p8_power_sensor *ps =
+         &(((struct p8_power_sensor *)sensor)[sensor_num]);
+
+      switch (hwmon) {
+      case hwmon_power_input:
+         *val = ps->value;
+         break;
+      case hwmon_power_label:
+         *val = ps->sensor_id;
+         break;
+      case hwmon_power_average:
+         *val = ps->accumulator;
+         break;
+      case hwmon_power_average_interval:
+         *val = ps->update_tag;
+         break;
+      default:
+         rc = -EOPNOTSUPP;
+      }
+   }
+      break;
+   case CAPS:
+   {
+      struct p8_caps_sensor *cs =
+         &(((struct p8_caps_sensor *)sensor)[sensor_num]);
+
+      switch (hwmon) {
+      case hwmon_power_cap:
+         *val = cs->curr_powercap;
+         break;
+      case hwmon_power_cap_max:
+         *val = cs->max_powercap;
+         break;
+      case hwmon_power_cap_min:
+         *val = cs->min_powercap;
+         break;
+      case hwmon_power_max:
+         *val = cs->norm_powercap;
+         break;
+      case hwmon_power_alarm:
+         *val = cs->user_powerlimit;
+         break;
+      default:
+         rc = -EOPNOTSUPP;
+      }
+   }
+      break;
+   default:
+      rc = -EINVAL;
+   }
+
+   return rc;
+}
+
+static const struct occ_ops p8_ops = {
+   .parse_sensor = p8_parse_sensor,
+   .alloc_sensor = p8_alloc_sensor,
+   .get_sensor = p8_get_sensor,
+};
+
+static const struct occ_config p8_config = {
+   .command_addr = 0xFFFF6000,
+   .response_addr = 0xFFFF7000,
+};
+
+const u32 *p8_get_sensor_hwmon_configs()
+{
+   return p8_sensor_hwmon_configs;
+}
+EXPORT_SYMBOL(p8_get_sensor_hwmon_configs);
+
+struct occ *p8_occ_start(struct device *dev, void *bus,
+          struct occ_bus_ops *bus_ops)
+{
+   return occ_start(dev, bus, bus_ops, &p8_ops, &p8_config);
+}
+EXPORT_SYMBOL(p8_occ_start);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("P8 OCC sensors");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_p8.h new file +0 -0
new file mode 100644
index 0000000..3fe6417
@@ -0,0 +1,30 @@
Quoted 8 lines

drivers/hwmon/occ/occ_p8.h

Quoted 8 lines
drivers/hwmon/occ/occ_p8.h new file +8 -0
+/*
+ * occ_p8.h - OCC hwmon driver
+ *
+ * This file contains Power8-specific function prototypes
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_p8.h line 9

Quoted 1 line
drivers/hwmon/occ/occ_p8.h new file +1 -0
+ * it under the terms of the GNU General Public License as published

by

Quoted 23 lines
Quoted 22 lines
drivers/hwmon/occ/occ_p8.h new file +21 -0
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_P8_H__
+#define __OCC_P8_H__
+
+#include "scom.h"
+
+struct device;
+
+const u32 *p8_get_sensor_hwmon_configs(void);
+struct occ *p8_occ_start(struct device *dev, void *bus,
+          struct occ_bus_ops *bus_ops);
+
+#endif /* __OCC_P8_H__ */
#20 lore raw

PATCH v3 5/6 hwmon: occ: Add hwmon implementation for the P8 OCC

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add code to tie the hwmon sysfs code and the POWER8 OCC code together, as well as probe the entire driver from the I2C bus. I2C is the communication method between the BMC and the P8 OCC.

---
 Documentation/devicetree/bindings/hwmon/occ.txt |  13 +++
 drivers/hwmon/occ/Kconfig                       |  14 ++++
 drivers/hwmon/occ/Makefile                      |   1 +
 drivers/hwmon/occ/p8_occ_i2c.c                  | 104 ++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/occ.txt
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
Documentation/devicetree/bindings/hwmon/occ.txt new file +13 -0
new file mode 100644
index 0000000..b0d2b36
@@ -0,0 +1,13 @@
+HWMON I2C driver for IBM POWER CPU OCC (On Chip Controller)
+
+Required properties:
+ - compatible: must be "ibm,p8-occ-i2c"
+ - reg: physical address
+
+Example:
+i2c3: i2c-bus@100 {
+	occ@50 {
+		compatible = "ibm,p8-occ-i2c";
+		reg = <0x50>;
+	};
+};
drivers/hwmon/occ/Kconfig +14 -0
index cdb64a7..3a5188f 100644
@@ -13,3 +13,17 @@ menuconfig SENSORS_PPC_OCC
 
 	  This driver can also be built as a module. If so, the module
 	  will be called occ.
+
+if SENSORS_PPC_OCC
+
+config SENSORS_PPC_OCC_P8_I2C
+	tristate "POWER8 OCC hwmon support"
+	depends on I2C
+	help
+	 Provide a hwmon sysfs interface for the POWER8 On-Chip Controller,
+	 exposing temperature, frequency and power measurements.
+
+	 This driver can also be built as a module. If so, the module will be
+	 called p8-occ-i2c.
+
+endif
drivers/hwmon/occ/Makefile +1 -0
index a6881f9..9294b58 100644
@@ -1 +1,2 @@
 obj-$(CONFIG_SENSORS_PPC_OCC) += occ.o occ_sysfs.o
+obj-$(CONFIG_SENSORS_PPC_OCC_P8_I2C) += occ_scom_i2c.o occ_p8.o p8_occ_i2c.o
drivers/hwmon/occ/p8_occ_i2c.c new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer Acked-by on v3 5/6
#21 lore raw

Re: [PATCH linux v3 5/6] hwmon: occ: Add hwmon implementation for the P8 OCC

Quoted 10 lines · On Mon, Jan 16, 2017 at 03:13:38PM -0600, [email protected] wrote:

From: "Edward A. James" <[email protected]>

Add code to tie the hwmon sysfs code and the POWER8 OCC code together, as well as probe the entire driver from the I2C bus. I2C is the communication method between the BMC and the P8 OCC.

---
 Documentation/devicetree/bindings/hwmon/occ.txt |  13 +++
Quoted 6 lines
 drivers/hwmon/occ/Kconfig                       |  14 ++++
 drivers/hwmon/occ/Makefile                      |   1 +
 drivers/hwmon/occ/p8_occ_i2c.c                  | 104 ++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/occ.txt
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
#22 lore raw

Re: [PATCH linux v3 5/6] hwmon: occ: Add hwmon implementation for the P8 OCC

In reply to [email protected]
reproduce:

Hi Edward,

[auto build test ERROR on hwmon/hwmon-next]
[also build test ERROR on v4.10-rc5 next-20170125]
[cannot apply to linux/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/eajames-ibm-gmail-com/drivers-hwmon-Add-On-Chip-Controller-driver/20170117-062329
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
config: i386-randconfig-x078-01291145 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

>> ERROR: "occ_sysfs_start" [drivers/hwmon/occ/p8_occ_i2c.ko] undefined!
>> ERROR: "occ_start" [drivers/hwmon/occ/occ_p8.ko] undefined!
>> ERROR: "occ_get_sensor" [drivers/hwmon/occ/occ_p8.ko] undefined!

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#23 lore raw

PATCH v3 6/6 hwmon: occ: Add callbacks for parsing P9 OCC datastructures

In reply to [email protected]

From: "Edward A. James" <[email protected]>

Add functions to parse the data structures that are specific to the OCC on the POWER9 processor. These are the sensor data structures, including temperature, frequency, power, and "caps."

---
 Documentation/hwmon/occ    |   3 +
 drivers/hwmon/occ/occ_p9.c | 308 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_p9.h |  30 +++++
 3 files changed, 341 insertions(+)
 create mode 100644 drivers/hwmon/occ/occ_p9.c
 create mode 100644 drivers/hwmon/occ/occ_p9.h
Documentation/hwmon/occ +3 -0
index 143951e..6cea853 100644
@@ -34,6 +34,9 @@ number of data structures, such as command format, response headers, and the
 like, are also defined in this specification, and are common to both POWER8 and
 POWER9 OCCs.
 
+There is currently no public P9 OCC specification, and the data structures
+defined in the POWER9 OCC driver are subject to change.
+
 sysfs Entries
 -------------
 
drivers/hwmon/occ/occ_p9.c new file +308 -0

308 lines not on this page · raw message

drivers/hwmon/occ/occ_p9.h new file +30 -0
new file mode 100644
index 0000000..18ca16a
@@ -0,0 +1,30 @@
+/*
+ * occ_p9.h - OCC hwmon driver
+ *
+ * This file contains Power9-specific function prototypes
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __OCC_P9_H__
+#define __OCC_P9_H__
+
+#include "scom.h"
+
+struct device;
+
+const u32 *p9_get_sensor_hwmon_configs(void);
+struct occ *p9_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+
+#endif /* __OCC_P9_H__ */
Signature · 1 line
1.9.1