Skip to contentSkip to conversation

drivers: hwmon: Add On-Chip Controller driver

PATCH Superseded [email protected] 9 participants 26 messages Dec 30, 2016 – Jan 10, 2017

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

 .../devicetree/bindings/i2c/i2c-ibm-occ.txt        |  13 +
 Documentation/hwmon/occ                            | 100 ++++
 drivers/hwmon/Kconfig                              |   2 +
 drivers/hwmon/Makefile                             |   1 +
 drivers/hwmon/occ/Kconfig                          |  29 ++
 drivers/hwmon/occ/Makefile                         |   2 +
 drivers/hwmon/occ/occ.c                            | 544 +++++++++++++++++++++
 drivers/hwmon/occ/occ.h                            |  86 ++++
 drivers/hwmon/occ/occ_p8.c                         | 217 ++++++++
 drivers/hwmon/occ/occ_p8.h                         |  30 ++
 drivers/hwmon/occ/occ_scom_i2c.c                   |  73 +++
 drivers/hwmon/occ/occ_scom_i2c.h                   |  26 +
 drivers/hwmon/occ/occ_sysfs.c                      | 492 +++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h                      |  52 ++
 drivers/hwmon/occ/p8_occ_i2c.c                     | 141 ++++++
 drivers/hwmon/occ/p9_occ.c                         | 243 +++++++++
 drivers/hwmon/occ/p9_occ.h                         |  30 ++
 drivers/hwmon/occ/scom.h                           |  47 ++
 18 files changed, 2128 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/i2c-ibm-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_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/p9_occ.c
 create mode 100644 drivers/hwmon/occ/p9_occ.h
 create mode 100644 drivers/hwmon/occ/scom.h
Signature · 1 line
1.9.1
#1 lore raw

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

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 ++++
 drivers/hwmon/Kconfig      |   2 +
 drivers/hwmon/Makefile     |   1 +
 drivers/hwmon/occ/Kconfig  |  15 ++
 drivers/hwmon/occ/Makefile |   1 +
 drivers/hwmon/occ/occ.c    | 544 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ.h    |  86 +++++++
 drivers/hwmon/occ/scom.h   |  47 ++++
 8 files changed, 736 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.
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 +544 -0

544 lines not on this page · raw message

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

86 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 · 1 line
1.9.1
#2 lore raw

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

reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Edward,

[auto build test WARNING on hwmon/hwmon-next]
[also build test WARNING on v4.10-rc1 next-20161224]
[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/20161231-021324
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=ia64

All warnings (new ones prefixed by >>):

drivers/hwmon/occ/occ.c: In function 'occ_get_all':

>> drivers/hwmon/occ/occ.c:390:44: warning: passing argument 5 of 'occ_send_cmd' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
     rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data, occ_data);
                                               ^

drivers/hwmon/occ/occ.c:281:11: note: expected 'u8 * {aka unsigned char *}' but argument is of type 'const u8 * {aka const unsigned char *}'
static u8 occ_send_cmd(struct occ *driver, u8 seq, u8 type, u16 length,
^~~~~~~~~~~~

vim +390 drivers/hwmon/occ/occ.c

374 return rc;
375 }
376
377 static int occ_get_all(struct occ *driver)
378 {
379 int i = 0, rc;
380 u8 *occ_data;
381 u16 num_bytes;
382 const u8 poll_cmd_data = OCC_POLL_STAT_SENSOR;
383 struct device *dev = driver->dev;
384 struct occ_response *resp = &driver->response;
385
386 occ_data = devm_kzalloc(dev, OCC_DATA_MAX, GFP_KERNEL);
387 if (!occ_data)
388 return -ENOMEM;
389

 > 390		rc = occ_send_cmd(driver, 0, OCC_POLL, 1, &poll_cmd_data, occ_data);

391 if (rc) {
392 dev_err(dev, "OCC poll failed: %d\n", rc);
393 goto out;
394 }
395
396 num_bytes = get_unaligned((u16 *)&occ_data[RESP_DATA_LENGTH]);
397 num_bytes = be16_to_cpu(num_bytes);
398 dev_dbg(dev, "OCC data length: %d\n", num_bytes);

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw

PATCH 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       |  48 +++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 492 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  52 +++++
 4 files changed, 593 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +48 -0
index 79d1642..1ee8689 100644
@@ -25,6 +25,54 @@ 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 two sysfs files for each frequency, temperature, and power
+sensor. These are "input" and "label". The input file contains the value of the
+sensor. The label file contains the sensor id. The sensor id is the unique
+internal OCC identifier. Sensor ids may be provided by the OCC specification.
+The names of these files will be in the following format:
+	<sensor type><sensor index>_input
+	<sensor type><sensor index>_label
+Sensor types will be one of "temp", "freq", or "power". The sensor index is
+an index to differentiate different sensor files. For example, a single
+temperature sensor will have two sysfs files: temp1_input and temp1_label.
+
+Caps sensors are exported differently. For each caps sensor, the driver will
+export 6 entries:
+	curr_powercap - current power cap in watts
+	curr_powerreading - current power output in watts
+	norm_powercap - power cap without redundant power
+	max_powercap - maximum power cap that can be set in watts
+	min_powercap - minimum power cap that can be set in watts
+	user_powerlimit - power limit specified by the user in watts
+In addition, the OCC driver for P9 will export a 7th entry:
+	user_powerlimit_source - can be one of two values depending on who set
+		the user_powerlimit. 0x1 - out of band from BMC or host. 0x2 -
+		in band from other source.
+The format for these files is caps<sensor index>_<entry type>. For example,
+caps1_curr_powercap.
+
+The driver also provides a number of sysfs entries through hwmon to better
+control the driver and monitor the OCC.
+	powercap - read or write the OCC user power limit in watts.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
+The driver also exports a single sysfs file through the communication protocol
+device (see BMC - Host Communications). The filename is "online" and represents
+the status of the OCC with respect to the driver. The OCC can be in one of two
+states: OCC polling enabled or OCC polling disabled. The purpose of this file
+is to control the behavior of the driver and it's hwmon sysfs entries, not to
+infer any information about the state of the physical OCC. Reading the file
+returns either a 0 (polling disabled) or 1 (polling enabled). Writing 1 to the
+file enables OCC polling in the driver if communications can be established
+with the OCC. Writing a 0 to the driver disables OCC polling.
+
 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 +492 -0

492 lines not on this page · raw message

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

52 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#4 lore raw

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

Quoted 501 lines · On Fri, Dec 30, 2016 at 11:56:04AM -0600, [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       |  48 +++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 492 ++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_sysfs.h |  52 +++++
 4 files changed, 593 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +48 -0
index 79d1642..1ee8689 100644
@@ -25,6 +25,54 @@ 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 two sysfs files for each frequency, temperature, and power
+sensor. These are "input" and "label". The input file contains the value of the
+sensor. The label file contains the sensor id. The sensor id is the unique
+internal OCC identifier. Sensor ids may be provided by the OCC specification.
+The names of these files will be in the following format:
+	<sensor type><sensor index>_input
+	<sensor type><sensor index>_label
+Sensor types will be one of "temp", "freq", or "power". The sensor index is
+an index to differentiate different sensor files. For example, a single
+temperature sensor will have two sysfs files: temp1_input and temp1_label.
+
+Caps sensors are exported differently. For each caps sensor, the driver will
+export 6 entries:
+	curr_powercap - current power cap in watts
+	curr_powerreading - current power output in watts
+	norm_powercap - power cap without redundant power
+	max_powercap - maximum power cap that can be set in watts
+	min_powercap - minimum power cap that can be set in watts
+	user_powerlimit - power limit specified by the user in watts
+In addition, the OCC driver for P9 will export a 7th entry:
+	user_powerlimit_source - can be one of two values depending on who set
+		the user_powerlimit. 0x1 - out of band from BMC or host. 0x2 -
+		in band from other source.
+The format for these files is caps<sensor index>_<entry type>. For example,
+caps1_curr_powercap.
+
+The driver also provides a number of sysfs entries through hwmon to better
+control the driver and monitor the OCC.
+	powercap - read or write the OCC user power limit in watts.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
+The driver also exports a single sysfs file through the communication protocol
+device (see BMC - Host Communications). The filename is "online" and represents
+the status of the OCC with respect to the driver. The OCC can be in one of two
+states: OCC polling enabled or OCC polling disabled. The purpose of this file
+is to control the behavior of the driver and it's hwmon sysfs entries, not to
+infer any information about the state of the physical OCC. Reading the file
+returns either a 0 (polling disabled) or 1 (polling enabled). Writing 1 to the
+file enables OCC polling in the driver if communications can be established
+with the OCC. Writing a 0 to the driver disables OCC polling.
+
 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 +412 -0
new file mode 100644
index 0000000..b0e063da
@@ -0,0 +1,492 @@
+/*
+ * 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/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+
+#include "occ_sysfs.h"
+
+#define MAX_SENSOR_ATTR_LEN	32
+
+#define RESP_RETURN_CMD_INVAL	0x13
+
+struct sensor_attr_data {
+	enum sensor_type type;
+	u32 hwmon_index;
+	u32 attr_id;
+	char name[MAX_SENSOR_ATTR_LEN];
+	struct device_attribute dev_attr;
+};
+
+static ssize_t show_input(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct sensor_attr_data *sdata = container_of(attr,
+						      struct sensor_attr_data,
+						      dev_attr);
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	val = occ_get_sensor_value(driver->occ, sdata->type,
+				   sdata->hwmon_index - 1);
+	if (sdata->type == TEMP)
+		val *= 1000;	/* in millidegree Celsius */
+
+	return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+/* show_label provides the OCC sensor id. The sensor id will be either a
+ * 2-byte (for P8) or 4-byte (for P9) value. The sensor id is a way to
+ * identify what each sensor represents, according to the OCC specification.
+ */
+static ssize_t show_label(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct sensor_attr_data *sdata = container_of(attr,
+						      struct sensor_attr_data,
+						      dev_attr);
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	val = occ_get_sensor_id(driver->occ, sdata->type,
+				sdata->hwmon_index - 1);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_caps(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct caps_sensor *sensor;
+	struct sensor_attr_data *sdata = container_of(attr,
+						      struct sensor_attr_data,
+						      dev_attr);
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	sensor = occ_get_sensor(driver->occ, CAPS);
+	if (!sensor) {
+		val = -1;
+		return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+	}
+
+	val = occ_get_caps_value(driver->occ, sensor, sdata->hwmon_index - 1,
+				 sdata->attr_id);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_update_interval(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%lu\n", driver->update_interval);
+}
+
+static ssize_t store_update_interval(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	unsigned long val;
+	int rc;
+
+	rc = kstrtoul(buf, 10, &val);
+	if (rc)
+		return rc;
+
+	driver->update_interval = val;
+	occ_set_update_interval(driver->occ, val);
+
+	return count;
+}
+
+static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_update_interval,
+		   store_update_interval);
+
+static ssize_t show_name(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE - 1, "occ\n");
+}
+
+static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+
+static ssize_t show_user_powercap(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->user_powercap);
+}
+
+static ssize_t store_user_powercap(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	u16 val;
+	int rc;
+
+	rc = kstrtou16(buf, 10, &val);
+	if (rc)
+		return rc;
+
+	dev_dbg(dev, "set user powercap to: %d\n", val);
+	rc = occ_set_user_powercap(driver->occ, val);
+	if (rc) {
+		dev_err(dev, "set user powercap failed: 0x%x\n", rc);
+		if (rc == RESP_RETURN_CMD_INVAL) {
+			dev_err(dev, "set invalid powercap value: %d\n", val);
+			return -EINVAL;
+		}
+
+		return rc;
+	}
+
+	driver->user_powercap = val;
+
+	return count;
+}
+
+static DEVICE_ATTR(user_powercap, S_IWUSR | S_IRUGO, show_user_powercap,
+		   store_user_powercap);
+
+static void deinit_sensor_groups(struct device *dev,
+				 struct sensor_group *sensor_groups)
+{
+	int i;
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+		if (sensor_groups[i].group.attrs)
+			devm_kfree(dev, sensor_groups[i].group.attrs);
+		if (sensor_groups[i].sattr)
+			devm_kfree(dev, sensor_groups[i].sattr);
+		sensor_groups[i].group.attrs = NULL;
+		sensor_groups[i].sattr = NULL;
+	}
+}
+
+static void sensor_attr_init(struct sensor_attr_data *sdata,
+			     char *sensor_group_name,
+			     char *attr_name,
+			     ssize_t (*show)(struct device *dev,
+					     struct device_attribute *attr,
+					     char *buf))
+{
+	sysfs_attr_init(&sdata->dev_attr.attr);
+
+	snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+		 sensor_group_name, sdata->hwmon_index, attr_name);
+	sdata->dev_attr.attr.name = sdata->name;
+	sdata->dev_attr.attr.mode = S_IRUGO;
+	sdata->dev_attr.show = show;
+}
+
+static int create_sensor_group(struct occ_sysfs *driver,
+			       enum sensor_type type, int sensor_num)
+{
+	struct device *dev = driver->dev;
+	struct sensor_group *sensor_groups = driver->sensor_groups;
+	struct sensor_attr_data *sdata;
+	int rc, i;
+
+	/* each sensor has 'label' and 'input' attributes */
+	sensor_groups[type].group.attrs =
+		devm_kzalloc(dev, sizeof(struct attribute *) *
+			     sensor_num * 2 + 1, GFP_KERNEL);
+	if (!sensor_groups[type].group.attrs) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	sensor_groups[type].sattr =
+		devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+			     sensor_num * 2, GFP_KERNEL);
+	if (!sensor_groups[type].sattr) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	for (i = 0; i < sensor_num; i++) {
+		sdata = &sensor_groups[type].sattr[i];
+		/* hwmon attributes index starts from 1 */
+		sdata->hwmon_index = i + 1;
+		sdata->type = type;
+		sensor_attr_init(sdata, sensor_groups[type].name, "input",
+				 show_input);
+		sensor_groups[type].group.attrs[i] = &sdata->dev_attr.attr;
+
+		sdata = &sensor_groups[type].sattr[i + sensor_num];
+		sdata->hwmon_index = i + 1;
+		sdata->type = type;
+		sensor_attr_init(sdata, sensor_groups[type].name, "label",
+				 show_label);
+		sensor_groups[type].group.attrs[i + sensor_num] =
+			&sdata->dev_attr.attr;
+	}
+
+	rc = sysfs_create_group(&dev->kobj, &sensor_groups[type].group);
+	if (rc)
+		goto err;
+
+	return 0;
+err:
+	deinit_sensor_groups(dev, sensor_groups);
+	return rc;
+}
+
+static void caps_sensor_attr_init(struct sensor_attr_data *sdata,
+				  char *attr_name, uint32_t hwmon_index,
+				  uint32_t attr_id)
+{
+	sdata->type = CAPS;
+	sdata->hwmon_index = hwmon_index;
+	sdata->attr_id = attr_id;
+
+	snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+		 "caps", sdata->hwmon_index, attr_name);
+
+	sysfs_attr_init(&sdata->dev_attr.attr);
+	sdata->dev_attr.attr.name = sdata->name;
+	sdata->dev_attr.attr.mode = S_IRUGO;
+	sdata->dev_attr.show = show_caps;
+}
+
+static int create_caps_sensor_group(struct occ_sysfs *driver, int sensor_num)
+{
+	struct device *dev = driver->dev;
+	struct sensor_group *sensor_groups = driver->sensor_groups;
+	int field_num = driver->num_caps_fields;
+	struct sensor_attr_data *sdata;
+	int i, j, rc;
+
+	sensor_groups[CAPS].group.attrs =
+		devm_kzalloc(dev, sizeof(struct attribute *) * sensor_num *
+			     field_num + 1, GFP_KERNEL);
+	if (!sensor_groups[CAPS].group.attrs) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	sensor_groups[CAPS].sattr =
+		devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+			     sensor_num * field_num, GFP_KERNEL);
+	if (!sensor_groups[CAPS].sattr) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	for (j = 0; j < sensor_num; ++j) {
+		for (i = 0; i < field_num; ++i) {
+			sdata = &sensor_groups[CAPS].sattr[j * field_num + i];
+			caps_sensor_attr_init(sdata,
+					      driver->caps_names[i], j + 1, i);
+			sensor_groups[CAPS].group.attrs[j * field_num + i] =
+				&sdata->dev_attr.attr;
+		}
+	}
+
+	rc = sysfs_create_group(&dev->kobj, &sensor_groups[CAPS].group);
+	if (rc)
+		goto err;
+
+	return rc;
+err:
+	deinit_sensor_groups(dev, sensor_groups);
+	return rc;
+}
+
+static void occ_remove_hwmon_attrs(struct occ_sysfs *driver)
+{
+	struct device *dev = driver->dev;
+
+	device_remove_file(dev, &dev_attr_user_powercap);
+	device_remove_file(dev, &dev_attr_update_interval);
+	device_remove_file(dev, &dev_attr_name);
+}
+
+static int occ_create_hwmon_attrs(struct occ_sysfs *driver)
+{
+	int i, rc, id, sensor_num;
+	struct device *dev = driver->dev;
+	struct sensor_group *sensor_groups = driver->sensor_groups;
+	struct occ_blocks *resp = NULL;
+
+	occ_get_response_blocks(driver->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(driver->occ);
+	if (rc) {
+		dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+		return rc;
+	}
+	if (!resp->blocks)
+		return -ENOMEM;
+
+	rc = device_create_file(dev, &dev_attr_name);
+	if (rc)
+		goto error;
+
+	rc = device_create_file(dev, &dev_attr_update_interval);
+	if (rc)
+		goto error;
+
+	if (resp->sensor_block_id[CAPS] >= 0) {
+		/* user powercap: only for master OCC */
+		rc = device_create_file(dev, &dev_attr_user_powercap);
+		if (rc)
+			goto error;
+	}
+
+	sensor_groups[FREQ].name = "freq";
+	sensor_groups[TEMP].name = "temp";
+	sensor_groups[POWER].name = "power";
+	sensor_groups[CAPS].name = "caps";
+
+	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;
+		if (i == CAPS)
+			rc = create_caps_sensor_group(driver, sensor_num);
+		else
+			rc = create_sensor_group(driver, i, sensor_num);
+		if (rc)
+			goto error;
+	}
+
+	return 0;
+
+error:
+	dev_err(dev, "cannot create hwmon attributes: %d\n", rc);
+	occ_remove_hwmon_attrs(driver);
+	return rc;
+}
+
+static ssize_t show_occ_online(struct device *dev,
+			       struct device_attribute *attr, char *buf)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->occ_online);
+}
+
+static ssize_t store_occ_online(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	unsigned long val;
+	int rc;

drivers/hwmon/occ/occ_sysfs.c line 422

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+	rc = kstrtoul(buf, 10, &val);
+	if (rc)
+		return rc;
+
+	if (val == 1) {
+		if (driver->occ_online)
+			return count;
+
+		driver->dev = hwmon_device_register(dev);

hwmon_device_register() is deprecated. Please consider using devm_hwmon_device_register_with_info() or at least hwmon_device_register_with_info().

Uuh ... and registering a hwmon device based on writing into a sysfs attribute is completely out of the question.

Thanks,
Guenter

Quoted 52 lines
drivers/hwmon/occ/occ_sysfs.c new file +52 -0
+		if (IS_ERR(driver->dev))
+			return PTR_ERR(driver->dev);
+
+		dev_set_drvdata(driver->dev, driver);
+
+		rc = occ_create_hwmon_attrs(driver);
+		if (rc) {
+			hwmon_device_unregister(driver->dev);
+			driver->dev = NULL;
+			return rc;
+		}
+	} else if (val == 0) {
+		if (!driver->occ_online)
+			return count;
+
+		occ_remove_hwmon_attrs(driver);
+		hwmon_device_unregister(driver->dev);
+		driver->dev = NULL;
+	} else
+		return -EINVAL;
+
+	driver->occ_online = val;
+	return count;
+}
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+		   store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  struct occ_sysfs_config *config)
+{
+	struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+					       GFP_KERNEL);
+	int rc;
+
+	if (!hwmon)
+		return ERR_PTR(-ENOMEM);
+
+	hwmon->occ = occ;
+	hwmon->num_caps_fields = config->num_caps_fields;
+	hwmon->caps_names = config->caps_names;
+
+	dev_set_drvdata(dev, hwmon);
+
+	rc = device_create_file(dev, &dev_attr_online);
+	if (rc)
+		return ERR_PTR(rc);
+
+	return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+	if (driver->dev) {
+		occ_remove_hwmon_attrs(driver);
+		hwmon_device_unregister(driver->dev);
+	}
+
+	device_remove_file(driver->dev, &dev_attr_online);
+
+	devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free functions. Something is completely wrong here if you need that call.

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

Quoted 69 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_sysfs_stop);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_sysfs.h new file +52 -0
new file mode 100644
index 0000000..2a8044f
@@ -0,0 +1,52 @@
+/*
+ * 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 "occ.h"
+
+struct sensor_group {
+	char *name;
+	struct sensor_attr_data *sattr;
+	struct attribute_group group;
+};
+
+struct occ_sysfs_config {
+	unsigned int num_caps_fields;
+	char **caps_names;
+};
+
+struct occ_sysfs {
+	struct device *dev;
+	struct occ *occ;
+
+	u16 user_powercap;
+	bool occ_online;
+	struct sensor_group sensor_groups[MAX_OCC_SENSOR_TYPE];
+	unsigned long update_interval;
+	unsigned int num_caps_fields;
+	char **caps_names;
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  struct occ_sysfs_config *config);
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver);
+
+#endif /* __OCC_SYSFS_H__ */
Signature · 1 line
1.9.1
#5 lore raw

PATCH 2/6 hwmon: occ: Add sysfs interface

The hwmon maintainer rejected Eddies OCC driver because of the online sysfs attribute. Any kernel hackers out there know what the architecturally correct way to make the occ hwmon device suspend/resume with the system power might be?

-brad

Quoted 674 lines

Begin forwarded message:

From: Guenter Roeck <[email protected]>
Subject: Re: [PATCH linux 2/6] hwmon: occ: Add sysfs interface
Date: December 30, 2016 at 2:34:04 PM EST
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], "Edward A. James" <[email protected]>

Quoted 501 lines · On Fri, Dec 30, 2016 at 11:56:04AM -0600, [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       |  48 +++++
drivers/hwmon/occ/Makefile    |   2 +-
drivers/hwmon/occ/occ_sysfs.c | 492 ++++++++++++++++++++++++++++++++++++++++++
drivers/hwmon/occ/occ_sysfs.h |  52 +++++
4 files changed, 593 insertions(+), 1 deletion(-)

create mode 100644 drivers/hwmon/occ/occ_sysfs.c
create mode 100644 drivers/hwmon/occ/occ_sysfs.h

Documentation/hwmon/occ +0 -0
index 79d1642..1ee8689 100644
@@ -25,6 +25,54 @@ 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.

Documentation/hwmon/occ +48 -0
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports two sysfs files for each frequency, temperature, and power
+sensor. These are "input" and "label". The input file contains the value of the
+sensor. The label file contains the sensor id. The sensor id is the unique
+internal OCC identifier. Sensor ids may be provided by the OCC specification.
+The names of these files will be in the following format:
+	<sensor type><sensor index>_input
+	<sensor type><sensor index>_label
+Sensor types will be one of "temp", "freq", or "power". The sensor index is
+an index to differentiate different sensor files. For example, a single
+temperature sensor will have two sysfs files: temp1_input and temp1_label.
+
+Caps sensors are exported differently. For each caps sensor, the driver will
+export 6 entries:
+	curr_powercap - current power cap in watts
+	curr_powerreading - current power output in watts
+	norm_powercap - power cap without redundant power
+	max_powercap - maximum power cap that can be set in watts
+	min_powercap - minimum power cap that can be set in watts
+	user_powerlimit - power limit specified by the user in watts
+In addition, the OCC driver for P9 will export a 7th entry:
+	user_powerlimit_source - can be one of two values depending on who set
+		the user_powerlimit. 0x1 - out of band from BMC or host. 0x2 -
+		in band from other source.
+The format for these files is caps<sensor index>_<entry type>. For example,
+caps1_curr_powercap.
+
+The driver also provides a number of sysfs entries through hwmon to better
+control the driver and monitor the OCC.
+	powercap - read or write the OCC user power limit in watts.
+	name - read the name of the driver
+	update_interval - read or write the minimum interval for polling the
+		OCC.
+
+The driver also exports a single sysfs file through the communication protocol
+device (see BMC - Host Communications). The filename is "online" and represents
+the status of the OCC with respect to the driver. The OCC can be in one of two
+states: OCC polling enabled or OCC polling disabled. The purpose of this file
+is to control the behavior of the driver and it's hwmon sysfs entries, not to
+infer any information about the state of the physical OCC. Reading the file
+returns either a 0 (polling disabled) or 1 (polling enabled). Writing 1 to the
+file enables OCC polling in the driver if communications can be established
+with the OCC. Writing a 0 to the driver disables OCC polling.
+

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 +412 -0
new file mode 100644
index 0000000..b0e063da
@@ -0,0 +1,492 @@
+/*
+ * 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/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+
+#include "occ_sysfs.h"
+
+#define MAX_SENSOR_ATTR_LEN	32
+
+#define RESP_RETURN_CMD_INVAL	0x13
+
+struct sensor_attr_data {
+	enum sensor_type type;
+	u32 hwmon_index;
+	u32 attr_id;
+	char name[MAX_SENSOR_ATTR_LEN];
+	struct device_attribute dev_attr;
+};
+
+static ssize_t show_input(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct sensor_attr_data *sdata = container_of(attr,
+						      struct sensor_attr_data,
+						      dev_attr);
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	val = occ_get_sensor_value(driver->occ, sdata->type,
+				   sdata->hwmon_index - 1);
+	if (sdata->type == TEMP)
+		val *= 1000;	/* in millidegree Celsius */
+
+	return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+/* show_label provides the OCC sensor id. The sensor id will be either a
+ * 2-byte (for P8) or 4-byte (for P9) value. The sensor id is a way to
+ * identify what each sensor represents, according to the OCC specification.
+ */
+static ssize_t show_label(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct sensor_attr_data *sdata = container_of(attr,
+						      struct sensor_attr_data,
+						      dev_attr);
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	val = occ_get_sensor_id(driver->occ, sdata->type,
+				sdata->hwmon_index - 1);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_caps(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	int val;
+	struct caps_sensor *sensor;
+	struct sensor_attr_data *sdata = container_of(attr,
+						      struct sensor_attr_data,
+						      dev_attr);
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	sensor = occ_get_sensor(driver->occ, CAPS);
+	if (!sensor) {
+		val = -1;
+		return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+	}
+
+	val = occ_get_caps_value(driver->occ, sensor, sdata->hwmon_index - 1,
+				 sdata->attr_id);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_update_interval(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%lu\n", driver->update_interval);
+}
+
+static ssize_t store_update_interval(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	unsigned long val;
+	int rc;
+
+	rc = kstrtoul(buf, 10, &val);
+	if (rc)
+		return rc;
+
+	driver->update_interval = val;
+	occ_set_update_interval(driver->occ, val);
+
+	return count;
+}
+
+static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_update_interval,
+		   store_update_interval);
+
+static ssize_t show_name(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE - 1, "occ\n");
+}
+
+static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+
+static ssize_t show_user_powercap(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->user_powercap);
+}
+
+static ssize_t store_user_powercap(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	u16 val;
+	int rc;
+
+	rc = kstrtou16(buf, 10, &val);
+	if (rc)
+		return rc;
+
+	dev_dbg(dev, "set user powercap to: %d\n", val);
+	rc = occ_set_user_powercap(driver->occ, val);
+	if (rc) {
+		dev_err(dev, "set user powercap failed: 0x%x\n", rc);
+		if (rc == RESP_RETURN_CMD_INVAL) {
+			dev_err(dev, "set invalid powercap value: %d\n", val);
+			return -EINVAL;
+		}
+
+		return rc;
+	}
+
+	driver->user_powercap = val;
+
+	return count;
+}
+
+static DEVICE_ATTR(user_powercap, S_IWUSR | S_IRUGO, show_user_powercap,
+		   store_user_powercap);
+
+static void deinit_sensor_groups(struct device *dev,
+				 struct sensor_group *sensor_groups)
+{
+	int i;
+
+	for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+		if (sensor_groups[i].group.attrs)
+			devm_kfree(dev, sensor_groups[i].group.attrs);
+		if (sensor_groups[i].sattr)
+			devm_kfree(dev, sensor_groups[i].sattr);
+		sensor_groups[i].group.attrs = NULL;
+		sensor_groups[i].sattr = NULL;
+	}
+}
+
+static void sensor_attr_init(struct sensor_attr_data *sdata,
+			     char *sensor_group_name,
+			     char *attr_name,
+			     ssize_t (*show)(struct device *dev,
+					     struct device_attribute *attr,
+					     char *buf))
+{
+	sysfs_attr_init(&sdata->dev_attr.attr);
+
+	snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+		 sensor_group_name, sdata->hwmon_index, attr_name);
+	sdata->dev_attr.attr.name = sdata->name;
+	sdata->dev_attr.attr.mode = S_IRUGO;
+	sdata->dev_attr.show = show;
+}
+
+static int create_sensor_group(struct occ_sysfs *driver,
+			       enum sensor_type type, int sensor_num)
+{
+	struct device *dev = driver->dev;
+	struct sensor_group *sensor_groups = driver->sensor_groups;
+	struct sensor_attr_data *sdata;
+	int rc, i;
+
+	/* each sensor has 'label' and 'input' attributes */
+	sensor_groups[type].group.attrs =
+		devm_kzalloc(dev, sizeof(struct attribute *) *
+			     sensor_num * 2 + 1, GFP_KERNEL);
+	if (!sensor_groups[type].group.attrs) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	sensor_groups[type].sattr =
+		devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+			     sensor_num * 2, GFP_KERNEL);
+	if (!sensor_groups[type].sattr) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	for (i = 0; i < sensor_num; i++) {
+		sdata = &sensor_groups[type].sattr[i];
+		/* hwmon attributes index starts from 1 */
+		sdata->hwmon_index = i + 1;
+		sdata->type = type;
+		sensor_attr_init(sdata, sensor_groups[type].name, "input",
+				 show_input);
+		sensor_groups[type].group.attrs[i] = &sdata->dev_attr.attr;
+
+		sdata = &sensor_groups[type].sattr[i + sensor_num];
+		sdata->hwmon_index = i + 1;
+		sdata->type = type;
+		sensor_attr_init(sdata, sensor_groups[type].name, "label",
+				 show_label);
+		sensor_groups[type].group.attrs[i + sensor_num] =
+			&sdata->dev_attr.attr;
+	}
+
+	rc = sysfs_create_group(&dev->kobj, &sensor_groups[type].group);
+	if (rc)
+		goto err;
+
+	return 0;
+err:
+	deinit_sensor_groups(dev, sensor_groups);
+	return rc;
+}
+
+static void caps_sensor_attr_init(struct sensor_attr_data *sdata,
+				  char *attr_name, uint32_t hwmon_index,
+				  uint32_t attr_id)
+{
+	sdata->type = CAPS;
+	sdata->hwmon_index = hwmon_index;
+	sdata->attr_id = attr_id;
+
+	snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+		 "caps", sdata->hwmon_index, attr_name);
+
+	sysfs_attr_init(&sdata->dev_attr.attr);
+	sdata->dev_attr.attr.name = sdata->name;
+	sdata->dev_attr.attr.mode = S_IRUGO;
+	sdata->dev_attr.show = show_caps;
+}
+
+static int create_caps_sensor_group(struct occ_sysfs *driver, int sensor_num)
+{
+	struct device *dev = driver->dev;
+	struct sensor_group *sensor_groups = driver->sensor_groups;
+	int field_num = driver->num_caps_fields;
+	struct sensor_attr_data *sdata;
+	int i, j, rc;
+
+	sensor_groups[CAPS].group.attrs =
+		devm_kzalloc(dev, sizeof(struct attribute *) * sensor_num *
+			     field_num + 1, GFP_KERNEL);
+	if (!sensor_groups[CAPS].group.attrs) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	sensor_groups[CAPS].sattr =
+		devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+			     sensor_num * field_num, GFP_KERNEL);
+	if (!sensor_groups[CAPS].sattr) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	for (j = 0; j < sensor_num; ++j) {
+		for (i = 0; i < field_num; ++i) {
+			sdata = &sensor_groups[CAPS].sattr[j * field_num + i];
+			caps_sensor_attr_init(sdata,
+					      driver->caps_names[i], j + 1, i);
+			sensor_groups[CAPS].group.attrs[j * field_num + i] =
+				&sdata->dev_attr.attr;
+		}
+	}
+
+	rc = sysfs_create_group(&dev->kobj, &sensor_groups[CAPS].group);
+	if (rc)
+		goto err;
+
+	return rc;
+err:
+	deinit_sensor_groups(dev, sensor_groups);
+	return rc;
+}
+
+static void occ_remove_hwmon_attrs(struct occ_sysfs *driver)
+{
+	struct device *dev = driver->dev;
+
+	device_remove_file(dev, &dev_attr_user_powercap);
+	device_remove_file(dev, &dev_attr_update_interval);
+	device_remove_file(dev, &dev_attr_name);
+}
+
+static int occ_create_hwmon_attrs(struct occ_sysfs *driver)
+{
+	int i, rc, id, sensor_num;
+	struct device *dev = driver->dev;
+	struct sensor_group *sensor_groups = driver->sensor_groups;
+	struct occ_blocks *resp = NULL;
+
+	occ_get_response_blocks(driver->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(driver->occ);
+	if (rc) {
+		dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+		return rc;
+	}
+	if (!resp->blocks)
+		return -ENOMEM;
+
+	rc = device_create_file(dev, &dev_attr_name);
+	if (rc)
+		goto error;
+
+	rc = device_create_file(dev, &dev_attr_update_interval);
+	if (rc)
+		goto error;
+
+	if (resp->sensor_block_id[CAPS] >= 0) {
+		/* user powercap: only for master OCC */
+		rc = device_create_file(dev, &dev_attr_user_powercap);
+		if (rc)
+			goto error;
+	}
+
+	sensor_groups[FREQ].name = "freq";
+	sensor_groups[TEMP].name = "temp";
+	sensor_groups[POWER].name = "power";
+	sensor_groups[CAPS].name = "caps";
+
+	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;
+		if (i == CAPS)
+			rc = create_caps_sensor_group(driver, sensor_num);
+		else
+			rc = create_sensor_group(driver, i, sensor_num);
+		if (rc)
+			goto error;
+	}
+
+	return 0;
+
+error:
+	dev_err(dev, "cannot create hwmon attributes: %d\n", rc);
+	occ_remove_hwmon_attrs(driver);
+	return rc;
+}
+
+static ssize_t show_occ_online(struct device *dev,
+			       struct device_attribute *attr, char *buf)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->occ_online);
+}
+
+static ssize_t store_occ_online(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct occ_sysfs *driver = dev_get_drvdata(dev);
+	unsigned long val;
+	int rc;

drivers/hwmon/occ/occ_sysfs.c line 422

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+	rc = kstrtoul(buf, 10, &val);
+	if (rc)
+		return rc;
+
+	if (val == 1) {
+		if (driver->occ_online)
+			return count;
+
+		driver->dev = hwmon_device_register(dev);

hwmon_device_register() is deprecated. Please consider using devm_hwmon_device_register_with_info() or at least hwmon_device_register_with_info().

Uuh ... and registering a hwmon device based on writing into a sysfs attribute is completely out of the question.

Thanks,
Guenter

Quoted 52 lines
drivers/hwmon/occ/occ_sysfs.c new file +52 -0
+		if (IS_ERR(driver->dev))
+			return PTR_ERR(driver->dev);
+
+		dev_set_drvdata(driver->dev, driver);
+
+		rc = occ_create_hwmon_attrs(driver);
+		if (rc) {
+			hwmon_device_unregister(driver->dev);
+			driver->dev = NULL;
+			return rc;
+		}
+	} else if (val == 0) {
+		if (!driver->occ_online)
+			return count;
+
+		occ_remove_hwmon_attrs(driver);
+		hwmon_device_unregister(driver->dev);
+		driver->dev = NULL;
+	} else
+		return -EINVAL;
+
+	driver->occ_online = val;
+	return count;
+}
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+		   store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  struct occ_sysfs_config *config)
+{
+	struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+					       GFP_KERNEL);
+	int rc;
+
+	if (!hwmon)
+		return ERR_PTR(-ENOMEM);
+
+	hwmon->occ = occ;
+	hwmon->num_caps_fields = config->num_caps_fields;
+	hwmon->caps_names = config->caps_names;
+
+	dev_set_drvdata(dev, hwmon);
+
+	rc = device_create_file(dev, &dev_attr_online);
+	if (rc)
+		return ERR_PTR(rc);
+
+	return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+	if (driver->dev) {
+		occ_remove_hwmon_attrs(driver);
+		hwmon_device_unregister(driver->dev);
+	}
+
+	device_remove_file(driver->dev, &dev_attr_online);
+
+	devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free functions. Something is completely wrong here if you need that call.

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

Quoted 69 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+	return 0;
+}
+EXPORT_SYMBOL(occ_sysfs_stop);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_sysfs.h new file +52 -0
new file mode 100644
index 0000000..2a8044f
@@ -0,0 +1,52 @@
+/*
+ * 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 "occ.h"
+
+struct sensor_group {
+	char *name;
+	struct sensor_attr_data *sattr;
+	struct attribute_group group;
+};
+
+struct occ_sysfs_config {
+	unsigned int num_caps_fields;
+	char **caps_names;
+};
+
+struct occ_sysfs {
+	struct device *dev;
+	struct occ *occ;
+
+	u16 user_powercap;
+	bool occ_online;
+	struct sensor_group sensor_groups[MAX_OCC_SENSOR_TYPE];
+	unsigned long update_interval;
+	unsigned int num_caps_fields;
+	char **caps_names;
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+				  struct occ_sysfs_config *config);
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver);
+
+#endif /* __OCC_SYSFS_H__ */
Signature · 1 line
1.9.1

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

#6 lore raw

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

Hi,

Just talked to Andrew. Can we just issue a modprobe command from userspace once the system power is on? That should be pretty clean in the driver and will make the "online" attribute unnecessary.

Eddie

Quoted 679 lines · Brad Bishop

The hwmon maintainer rejected Eddies OCC driver because of the online sysfs attribute. Any kernel hackers out there know what the architecturally correct way to make the occ hwmon device suspend/resume with the system power might be?

-brad

Quoted 674 lines

Begin forwarded message:

From: Guenter Roeck <[email protected]>
Subject: Re: [PATCH linux 2/6] hwmon: occ: Add sysfs interface
Date: December 30, 2016 at 2:34:04 PM EST
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], "Edward A. James" <[email protected]>

Quoted 501 lines · On Fri, Dec 30, 2016 at 11:56:04AM -0600, [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       |  48 +++++
drivers/hwmon/occ/Makefile    |   2 +-
drivers/hwmon/occ/occ_sysfs.c | 492 ++++++++++++++++++++++++++++++++++++++++++
drivers/hwmon/occ/occ_sysfs.h |  52 +++++
4 files changed, 593 insertions(+), 1 deletion(-)

create mode 100644 drivers/hwmon/occ/occ_sysfs.c
create mode 100644 drivers/hwmon/occ/occ_sysfs.h

Documentation/hwmon/occ +0 -0
index 79d1642..1ee8689 100644
@@ -25,6 +25,54 @@ 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.

Documentation/hwmon/occ +48 -0
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports two sysfs files for each frequency, temperature, and power
+sensor. These are "input" and "label". The input file contains the value of the
+sensor. The label file contains the sensor id. The sensor id is the unique
+internal OCC identifier. Sensor ids may be provided by the OCC specification.
+The names of these files will be in the following format:
+    <sensor type><sensor index>_input
+    <sensor type><sensor index>_label
+Sensor types will be one of "temp", "freq", or "power". The sensor index is
+an index to differentiate different sensor files. For example, a single
+temperature sensor will have two sysfs files: temp1_input and temp1_label.
+
+Caps sensors are exported differently. For each caps sensor, the driver will
+export 6 entries:
+    curr_powercap - current power cap in watts
+    curr_powerreading - current power output in watts
+    norm_powercap - power cap without redundant power
+    max_powercap - maximum power cap that can be set in watts
+    min_powercap - minimum power cap that can be set in watts
+    user_powerlimit - power limit specified by the user in watts
+In addition, the OCC driver for P9 will export a 7th entry:
+    user_powerlimit_source - can be one of two values depending on who set
+            the user_powerlimit. 0x1 - out of band from BMC or host. 0x2 -
+            in band from other source.
+The format for these files is caps<sensor index>_<entry type>. For example,
+caps1_curr_powercap.
+
+The driver also provides a number of sysfs entries through hwmon to better
+control the driver and monitor the OCC.
+    powercap - read or write the OCC user power limit in watts.
+    name - read the name of the driver
+    update_interval - read or write the minimum interval for polling the
+            OCC.
+
+The driver also exports a single sysfs file through the communication protocol
+device (see BMC - Host Communications). The filename is "online" and represents
+the status of the OCC with respect to the driver. The OCC can be in one of two
+states: OCC polling enabled or OCC polling disabled. The purpose of this file
+is to control the behavior of the driver and it's hwmon sysfs entries, not to
+infer any information about the state of the physical OCC. Reading the file
+returns either a 0 (polling disabled) or 1 (polling enabled). Writing 1 to the
+file enables OCC polling in the driver if communications can be established
+with the OCC. Writing a 0 to the driver disables OCC polling.
+

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 +412 -0
new file mode 100644
index 0000000..b0e063da
@@ -0,0 +1,492 @@
+/*
+ * 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/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+
+#include "occ_sysfs.h"
+
+#define MAX_SENSOR_ATTR_LEN 32
+
+#define RESP_RETURN_CMD_INVAL       0x13
+
+struct sensor_attr_data {
+    enum sensor_type type;
+    u32 hwmon_index;
+    u32 attr_id;
+    char name[MAX_SENSOR_ATTR_LEN];
+    struct device_attribute dev_attr;
+};
+
+static ssize_t show_input(struct device *dev,
+                      struct device_attribute *attr, char *buf)
+{
+    int val;
+    struct sensor_attr_data *sdata = container_of(attr,
+                                                  struct sensor_attr_data,
+                                                  dev_attr);
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    val = occ_get_sensor_value(driver->occ, sdata->type,
+                               sdata->hwmon_index - 1);
+    if (sdata->type == TEMP)
+            val *= 1000;    /* in millidegree Celsius */
+
+    return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+/* show_label provides the OCC sensor id. The sensor id will be either a
+ * 2-byte (for P8) or 4-byte (for P9) value. The sensor id is a way to
+ * identify what each sensor represents, according to the OCC specification.
+ */
+static ssize_t show_label(struct device *dev,
+                      struct device_attribute *attr, char *buf)
+{
+    int val;
+    struct sensor_attr_data *sdata = container_of(attr,
+                                                  struct sensor_attr_data,
+                                                  dev_attr);
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    val = occ_get_sensor_id(driver->occ, sdata->type,
+                            sdata->hwmon_index - 1);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_caps(struct device *dev,
+                     struct device_attribute *attr, char *buf)
+{
+    int val;
+    struct caps_sensor *sensor;
+    struct sensor_attr_data *sdata = container_of(attr,
+                                                  struct sensor_attr_data,
+                                                  dev_attr);
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    sensor = occ_get_sensor(driver->occ, CAPS);
+    if (!sensor) {
+            val = -1;
+            return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+    }
+
+    val = occ_get_caps_value(driver->occ, sensor, sdata->hwmon_index - 1,
+                             sdata->attr_id);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_update_interval(struct device *dev,
+                                struct device_attribute *attr, char *buf)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%lu\n", driver->update_interval);
+}
+
+static ssize_t store_update_interval(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t count)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+    unsigned long val;
+    int rc;
+
+    rc = kstrtoul(buf, 10, &val);
+    if (rc)
+            return rc;
+
+    driver->update_interval = val;
+    occ_set_update_interval(driver->occ, val);
+
+    return count;
+}
+
+static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_update_interval,
+               store_update_interval);
+
+static ssize_t show_name(struct device *dev,
+                     struct device_attribute *attr, char *buf)
+{
+    return snprintf(buf, PAGE_SIZE - 1, "occ\n");
+}
+
+static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+
+static ssize_t show_user_powercap(struct device *dev,
+                              struct device_attribute *attr, char *buf)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->user_powercap);
+}
+
+static ssize_t store_user_powercap(struct device *dev,
+                               struct device_attribute *attr,
+                               const char *buf, size_t count)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+    u16 val;
+    int rc;
+
+    rc = kstrtou16(buf, 10, &val);
+    if (rc)
+            return rc;
+
+    dev_dbg(dev, "set user powercap to: %d\n", val);
+    rc = occ_set_user_powercap(driver->occ, val);
+    if (rc) {
+            dev_err(dev, "set user powercap failed: 0x%x\n", rc);
+            if (rc == RESP_RETURN_CMD_INVAL) {
+                    dev_err(dev, "set invalid powercap value: %d\n", val);
+                    return -EINVAL;
+            }
+
+            return rc;
+    }
+
+    driver->user_powercap = val;
+
+    return count;
+}
+
+static DEVICE_ATTR(user_powercap, S_IWUSR | S_IRUGO, show_user_powercap,
+               store_user_powercap);
+
+static void deinit_sensor_groups(struct device *dev,
+                             struct sensor_group *sensor_groups)
+{
+    int i;
+
+    for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+            if (sensor_groups[i].group.attrs)
+                    devm_kfree(dev, sensor_groups[i].group.attrs);
+            if (sensor_groups[i].sattr)
+                    devm_kfree(dev, sensor_groups[i].sattr);
+            sensor_groups[i].group.attrs = NULL;
+            sensor_groups[i].sattr = NULL;
+    }
+}
+
+static void sensor_attr_init(struct sensor_attr_data *sdata,
+                         char *sensor_group_name,
+                         char *attr_name,
+                         ssize_t (*show)(struct device *dev,
+                                         struct device_attribute *attr,
+                                         char *buf))
+{
+    sysfs_attr_init(&sdata->dev_attr.attr);
+
+    snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+             sensor_group_name, sdata->hwmon_index, attr_name);
+    sdata->dev_attr.attr.name = sdata->name;
+    sdata->dev_attr.attr.mode = S_IRUGO;
+    sdata->dev_attr.show = show;
+}
+
+static int create_sensor_group(struct occ_sysfs *driver,
+                           enum sensor_type type, int sensor_num)
+{
+    struct device *dev = driver->dev;
+    struct sensor_group *sensor_groups = driver->sensor_groups;
+    struct sensor_attr_data *sdata;
+    int rc, i;
+
+    /* each sensor has 'label' and 'input' attributes */
+    sensor_groups[type].group.attrs =
+            devm_kzalloc(dev, sizeof(struct attribute *) *
+                         sensor_num * 2 + 1, GFP_KERNEL);
+    if (!sensor_groups[type].group.attrs) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    sensor_groups[type].sattr =
+            devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+                         sensor_num * 2, GFP_KERNEL);
+    if (!sensor_groups[type].sattr) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    for (i = 0; i < sensor_num; i++) {
+            sdata = &sensor_groups[type].sattr[i];
+            /* hwmon attributes index starts from 1 */
+            sdata->hwmon_index = i + 1;
+            sdata->type = type;
+            sensor_attr_init(sdata, sensor_groups[type].name, "input",
+                             show_input);
+            sensor_groups[type].group.attrs[i] = &sdata->dev_attr.attr;
+
+            sdata = &sensor_groups[type].sattr[i + sensor_num];
+            sdata->hwmon_index = i + 1;
+            sdata->type = type;
+            sensor_attr_init(sdata, sensor_groups[type].name, "label",
+                             show_label);
+            sensor_groups[type].group.attrs[i + sensor_num] =
+                    &sdata->dev_attr.attr;
+    }
+
+    rc = sysfs_create_group(&dev->kobj, &sensor_groups[type].group);
+    if (rc)
+            goto err;
+
+    return 0;
+err:
+    deinit_sensor_groups(dev, sensor_groups);
+    return rc;
+}
+
+static void caps_sensor_attr_init(struct sensor_attr_data *sdata,
+                              char *attr_name, uint32_t hwmon_index,
+                              uint32_t attr_id)
+{
+    sdata->type = CAPS;
+    sdata->hwmon_index = hwmon_index;
+    sdata->attr_id = attr_id;
+
+    snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+             "caps", sdata->hwmon_index, attr_name);
+
+    sysfs_attr_init(&sdata->dev_attr.attr);
+    sdata->dev_attr.attr.name = sdata->name;
+    sdata->dev_attr.attr.mode = S_IRUGO;
+    sdata->dev_attr.show = show_caps;
+}
+
+static int create_caps_sensor_group(struct occ_sysfs *driver, int sensor_num)
+{
+    struct device *dev = driver->dev;
+    struct sensor_group *sensor_groups = driver->sensor_groups;
+    int field_num = driver->num_caps_fields;
+    struct sensor_attr_data *sdata;
+    int i, j, rc;
+
+    sensor_groups[CAPS].group.attrs =
+            devm_kzalloc(dev, sizeof(struct attribute *) * sensor_num *
+                         field_num + 1, GFP_KERNEL);
+    if (!sensor_groups[CAPS].group.attrs) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    sensor_groups[CAPS].sattr =
+            devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+                         sensor_num * field_num, GFP_KERNEL);
+    if (!sensor_groups[CAPS].sattr) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    for (j = 0; j < sensor_num; ++j) {
+            for (i = 0; i < field_num; ++i) {
+                    sdata = &sensor_groups[CAPS].sattr[j * field_num + i];
+                    caps_sensor_attr_init(sdata,
+                                          driver->caps_names[i], j + 1, i);
+                    sensor_groups[CAPS].group.attrs[j * field_num + i] =
+                            &sdata->dev_attr.attr;
+            }
+    }
+
+    rc = sysfs_create_group(&dev->kobj, &sensor_groups[CAPS].group);
+    if (rc)
+            goto err;
+
+    return rc;
+err:
+    deinit_sensor_groups(dev, sensor_groups);
+    return rc;
+}
+
+static void occ_remove_hwmon_attrs(struct occ_sysfs *driver)
+{
+    struct device *dev = driver->dev;
+
+    device_remove_file(dev, &dev_attr_user_powercap);
+    device_remove_file(dev, &dev_attr_update_interval);
+    device_remove_file(dev, &dev_attr_name);
+}
+
+static int occ_create_hwmon_attrs(struct occ_sysfs *driver)
+{
+    int i, rc, id, sensor_num;
+    struct device *dev = driver->dev;
+    struct sensor_group *sensor_groups = driver->sensor_groups;
+    struct occ_blocks *resp = NULL;
+
+    occ_get_response_blocks(driver->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(driver->occ);
+    if (rc) {
+            dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+            return rc;
+    }
+    if (!resp->blocks)
+            return -ENOMEM;
+
+    rc = device_create_file(dev, &dev_attr_name);
+    if (rc)
+            goto error;
+
+    rc = device_create_file(dev, &dev_attr_update_interval);
+    if (rc)
+            goto error;
+
+    if (resp->sensor_block_id[CAPS] >= 0) {
+            /* user powercap: only for master OCC */
+            rc = device_create_file(dev, &dev_attr_user_powercap);
+            if (rc)
+                    goto error;
+    }
+
+    sensor_groups[FREQ].name = "freq";
+    sensor_groups[TEMP].name = "temp";
+    sensor_groups[POWER].name = "power";
+    sensor_groups[CAPS].name = "caps";
+
+    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;
+            if (i == CAPS)
+                    rc = create_caps_sensor_group(driver, sensor_num);
+            else
+                    rc = create_sensor_group(driver, i, sensor_num);
+            if (rc)
+                    goto error;
+    }
+
+    return 0;
+
+error:
+    dev_err(dev, "cannot create hwmon attributes: %d\n", rc);
+    occ_remove_hwmon_attrs(driver);
+    return rc;
+}
+
+static ssize_t show_occ_online(struct device *dev,
+                           struct device_attribute *attr, char *buf)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->occ_online);
+}
+
+static ssize_t store_occ_online(struct device *dev,
+                            struct device_attribute *attr,
+                            const char *buf, size_t count)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+    unsigned long val;
+    int rc;

drivers/hwmon/occ/occ_sysfs.c line 422

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+    rc = kstrtoul(buf, 10, &val);
+    if (rc)
+            return rc;
+
+    if (val == 1) {
+            if (driver->occ_online)
+                    return count;
+
+            driver->dev = hwmon_device_register(dev);

hwmon_device_register() is deprecated. Please consider using devm_hwmon_device_register_with_info() or at least hwmon_device_register_with_info().

Uuh ... and registering a hwmon device based on writing into a sysfs attribute is completely out of the question.

Thanks,
Guenter

Quoted 52 lines
drivers/hwmon/occ/occ_sysfs.c new file +52 -0
+            if (IS_ERR(driver->dev))
+                    return PTR_ERR(driver->dev);
+
+            dev_set_drvdata(driver->dev, driver);
+
+            rc = occ_create_hwmon_attrs(driver);
+            if (rc) {
+                    hwmon_device_unregister(driver->dev);
+                    driver->dev = NULL;
+                    return rc;
+            }
+    } else if (val == 0) {
+            if (!driver->occ_online)
+                    return count;
+
+            occ_remove_hwmon_attrs(driver);
+            hwmon_device_unregister(driver->dev);
+            driver->dev = NULL;
+    } else
+            return -EINVAL;
+
+    driver->occ_online = val;
+    return count;
+}
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+               store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                              struct occ_sysfs_config *config)
+{
+    struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+                                           GFP_KERNEL);
+    int rc;
+
+    if (!hwmon)
+            return ERR_PTR(-ENOMEM);
+
+    hwmon->occ = occ;
+    hwmon->num_caps_fields = config->num_caps_fields;
+    hwmon->caps_names = config->caps_names;
+
+    dev_set_drvdata(dev, hwmon);
+
+    rc = device_create_file(dev, &dev_attr_online);
+    if (rc)
+            return ERR_PTR(rc);
+
+    return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+    if (driver->dev) {
+            occ_remove_hwmon_attrs(driver);
+            hwmon_device_unregister(driver->dev);
+    }
+
+    device_remove_file(driver->dev, &dev_attr_online);
+
+    devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free functions. Something is completely wrong here if you need that call.

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

Quoted 67 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+    return 0;
+}
+EXPORT_SYMBOL(occ_sysfs_stop);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_sysfs.h new file +52 -1
new file mode 100644
index 0000000..2a8044f
@@ -0,0 +1,52 @@
+/*
+ * 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 "occ.h"
+
+struct sensor_group {
+    char *name;
+    struct sensor_attr_data *sattr;
+    struct attribute_group group;
+};
+
+struct occ_sysfs_config {
+    unsigned int num_caps_fields;
+    char **caps_names;
+};
+
+struct occ_sysfs {
+    struct device *dev;
+    struct occ *occ;
+
+    u16 user_powercap;
+    bool occ_online;
+    struct sensor_group sensor_groups[MAX_OCC_SENSOR_TYPE];
+    unsigned long update_interval;
+    unsigned int num_caps_fields;
+    char **caps_names;
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                              struct occ_sysfs_config *config);
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver);
+
+#endif /* __OCC_SYSFS_H__ */
--
Quoted 2 lines

1.9.1

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

#7 lore raw

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

What if the driver is built into the kernel? Is there such a thing as module only drivers?

Quoted 690 lines · Eddie James

Hi,

Just talked to Andrew. Can we just issue a modprobe command from userspace once the system power is on? That should be pretty clean in the driver and will make the "online" attribute unnecessary.

Eddie

Quoted 679 lines · Brad Bishop

The hwmon maintainer rejected Eddies OCC driver because of the online sysfs attribute. Any kernel hackers out there know what the architecturally correct way to make the occ hwmon device suspend/resume with the system power might be?

-brad

Quoted 674 lines

Begin forwarded message:

From: Guenter Roeck <[email protected]>
Subject: Re: [PATCH linux 2/6] hwmon: occ: Add sysfs interface
Date: December 30, 2016 at 2:34:04 PM EST
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], "Edward A. James" <[email protected]>

Quoted 501 lines · On Fri, Dec 30, 2016 at 11:56:04AM -0600, [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       |  48 +++++
drivers/hwmon/occ/Makefile    |   2 +-
drivers/hwmon/occ/occ_sysfs.c | 492 ++++++++++++++++++++++++++++++++++++++++++
drivers/hwmon/occ/occ_sysfs.h |  52 +++++
4 files changed, 593 insertions(+), 1 deletion(-)

create mode 100644 drivers/hwmon/occ/occ_sysfs.c
create mode 100644 drivers/hwmon/occ/occ_sysfs.h

Documentation/hwmon/occ +0 -0
index 79d1642..1ee8689 100644
@@ -25,6 +25,54 @@ 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.

Documentation/hwmon/occ +48 -0
+sysfs Entries
+-------------
+
+The OCC driver uses the hwmon sysfs framework to provide data to userspace.
+
+The driver exports two sysfs files for each frequency, temperature, and power
+sensor. These are "input" and "label". The input file contains the value of the
+sensor. The label file contains the sensor id. The sensor id is the unique
+internal OCC identifier. Sensor ids may be provided by the OCC specification.
+The names of these files will be in the following format:
+    <sensor type><sensor index>_input
+    <sensor type><sensor index>_label
+Sensor types will be one of "temp", "freq", or "power". The sensor index is
+an index to differentiate different sensor files. For example, a single
+temperature sensor will have two sysfs files: temp1_input and temp1_label.
+
+Caps sensors are exported differently. For each caps sensor, the driver will
+export 6 entries:
+    curr_powercap - current power cap in watts
+    curr_powerreading - current power output in watts
+    norm_powercap - power cap without redundant power
+    max_powercap - maximum power cap that can be set in watts
+    min_powercap - minimum power cap that can be set in watts
+    user_powerlimit - power limit specified by the user in watts
+In addition, the OCC driver for P9 will export a 7th entry:
+    user_powerlimit_source - can be one of two values depending on who set
+            the user_powerlimit. 0x1 - out of band from BMC or host. 0x2 -
+            in band from other source.
+The format for these files is caps<sensor index>_<entry type>. For example,
+caps1_curr_powercap.
+
+The driver also provides a number of sysfs entries through hwmon to better
+control the driver and monitor the OCC.
+    powercap - read or write the OCC user power limit in watts.
+    name - read the name of the driver
+    update_interval - read or write the minimum interval for polling the
+            OCC.
+
+The driver also exports a single sysfs file through the communication protocol
+device (see BMC - Host Communications). The filename is "online" and represents
+the status of the OCC with respect to the driver. The OCC can be in one of two
+states: OCC polling enabled or OCC polling disabled. The purpose of this file
+is to control the behavior of the driver and it's hwmon sysfs entries, not to
+infer any information about the state of the physical OCC. Reading the file
+returns either a 0 (polling disabled) or 1 (polling enabled). Writing 1 to the
+file enables OCC polling in the driver if communications can be established
+with the OCC. Writing a 0 to the driver disables OCC polling.
+

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 +412 -0
new file mode 100644
index 0000000..b0e063da
@@ -0,0 +1,492 @@
+/*
+ * 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/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+
+#include "occ_sysfs.h"
+
+#define MAX_SENSOR_ATTR_LEN 32
+
+#define RESP_RETURN_CMD_INVAL       0x13
+
+struct sensor_attr_data {
+    enum sensor_type type;
+    u32 hwmon_index;
+    u32 attr_id;
+    char name[MAX_SENSOR_ATTR_LEN];
+    struct device_attribute dev_attr;
+};
+
+static ssize_t show_input(struct device *dev,
+                      struct device_attribute *attr, char *buf)
+{
+    int val;
+    struct sensor_attr_data *sdata = container_of(attr,
+                                                  struct sensor_attr_data,
+                                                  dev_attr);
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    val = occ_get_sensor_value(driver->occ, sdata->type,
+                               sdata->hwmon_index - 1);
+    if (sdata->type == TEMP)
+            val *= 1000;    /* in millidegree Celsius */
+
+    return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+/* show_label provides the OCC sensor id. The sensor id will be either a
+ * 2-byte (for P8) or 4-byte (for P9) value. The sensor id is a way to
+ * identify what each sensor represents, according to the OCC specification.
+ */
+static ssize_t show_label(struct device *dev,
+                      struct device_attribute *attr, char *buf)
+{
+    int val;
+    struct sensor_attr_data *sdata = container_of(attr,
+                                                  struct sensor_attr_data,
+                                                  dev_attr);
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    val = occ_get_sensor_id(driver->occ, sdata->type,
+                            sdata->hwmon_index - 1);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_caps(struct device *dev,
+                     struct device_attribute *attr, char *buf)
+{
+    int val;
+    struct caps_sensor *sensor;
+    struct sensor_attr_data *sdata = container_of(attr,
+                                                  struct sensor_attr_data,
+                                                  dev_attr);
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    sensor = occ_get_sensor(driver->occ, CAPS);
+    if (!sensor) {
+            val = -1;
+            return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+    }
+
+    val = occ_get_caps_value(driver->occ, sensor, sdata->hwmon_index - 1,
+                             sdata->attr_id);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_update_interval(struct device *dev,
+                                struct device_attribute *attr, char *buf)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%lu\n", driver->update_interval);
+}
+
+static ssize_t store_update_interval(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t count)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+    unsigned long val;
+    int rc;
+
+    rc = kstrtoul(buf, 10, &val);
+    if (rc)
+            return rc;
+
+    driver->update_interval = val;
+    occ_set_update_interval(driver->occ, val);
+
+    return count;
+}
+
+static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_update_interval,
+               store_update_interval);
+
+static ssize_t show_name(struct device *dev,
+                     struct device_attribute *attr, char *buf)
+{
+    return snprintf(buf, PAGE_SIZE - 1, "occ\n");
+}
+
+static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+
+static ssize_t show_user_powercap(struct device *dev,
+                              struct device_attribute *attr, char *buf)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->user_powercap);
+}
+
+static ssize_t store_user_powercap(struct device *dev,
+                               struct device_attribute *attr,
+                               const char *buf, size_t count)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+    u16 val;
+    int rc;
+
+    rc = kstrtou16(buf, 10, &val);
+    if (rc)
+            return rc;
+
+    dev_dbg(dev, "set user powercap to: %d\n", val);
+    rc = occ_set_user_powercap(driver->occ, val);
+    if (rc) {
+            dev_err(dev, "set user powercap failed: 0x%x\n", rc);
+            if (rc == RESP_RETURN_CMD_INVAL) {
+                    dev_err(dev, "set invalid powercap value: %d\n", val);
+                    return -EINVAL;
+            }
+
+            return rc;
+    }
+
+    driver->user_powercap = val;
+
+    return count;
+}
+
+static DEVICE_ATTR(user_powercap, S_IWUSR | S_IRUGO, show_user_powercap,
+               store_user_powercap);
+
+static void deinit_sensor_groups(struct device *dev,
+                             struct sensor_group *sensor_groups)
+{
+    int i;
+
+    for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+            if (sensor_groups[i].group.attrs)
+                    devm_kfree(dev, sensor_groups[i].group.attrs);
+            if (sensor_groups[i].sattr)
+                    devm_kfree(dev, sensor_groups[i].sattr);
+            sensor_groups[i].group.attrs = NULL;
+            sensor_groups[i].sattr = NULL;
+    }
+}
+
+static void sensor_attr_init(struct sensor_attr_data *sdata,
+                         char *sensor_group_name,
+                         char *attr_name,
+                         ssize_t (*show)(struct device *dev,
+                                         struct device_attribute *attr,
+                                         char *buf))
+{
+    sysfs_attr_init(&sdata->dev_attr.attr);
+
+    snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+             sensor_group_name, sdata->hwmon_index, attr_name);
+    sdata->dev_attr.attr.name = sdata->name;
+    sdata->dev_attr.attr.mode = S_IRUGO;
+    sdata->dev_attr.show = show;
+}
+
+static int create_sensor_group(struct occ_sysfs *driver,
+                           enum sensor_type type, int sensor_num)
+{
+    struct device *dev = driver->dev;
+    struct sensor_group *sensor_groups = driver->sensor_groups;
+    struct sensor_attr_data *sdata;
+    int rc, i;
+
+    /* each sensor has 'label' and 'input' attributes */
+    sensor_groups[type].group.attrs =
+            devm_kzalloc(dev, sizeof(struct attribute *) *
+                         sensor_num * 2 + 1, GFP_KERNEL);
+    if (!sensor_groups[type].group.attrs) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    sensor_groups[type].sattr =
+            devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+                         sensor_num * 2, GFP_KERNEL);
+    if (!sensor_groups[type].sattr) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    for (i = 0; i < sensor_num; i++) {
+            sdata = &sensor_groups[type].sattr[i];
+            /* hwmon attributes index starts from 1 */
+            sdata->hwmon_index = i + 1;
+            sdata->type = type;
+            sensor_attr_init(sdata, sensor_groups[type].name, "input",
+                             show_input);
+            sensor_groups[type].group.attrs[i] = &sdata->dev_attr.attr;
+
+            sdata = &sensor_groups[type].sattr[i + sensor_num];
+            sdata->hwmon_index = i + 1;
+            sdata->type = type;
+            sensor_attr_init(sdata, sensor_groups[type].name, "label",
+                             show_label);
+            sensor_groups[type].group.attrs[i + sensor_num] =
+                    &sdata->dev_attr.attr;
+    }
+
+    rc = sysfs_create_group(&dev->kobj, &sensor_groups[type].group);
+    if (rc)
+            goto err;
+
+    return 0;
+err:
+    deinit_sensor_groups(dev, sensor_groups);
+    return rc;
+}
+
+static void caps_sensor_attr_init(struct sensor_attr_data *sdata,
+                              char *attr_name, uint32_t hwmon_index,
+                              uint32_t attr_id)
+{
+    sdata->type = CAPS;
+    sdata->hwmon_index = hwmon_index;
+    sdata->attr_id = attr_id;
+
+    snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+             "caps", sdata->hwmon_index, attr_name);
+
+    sysfs_attr_init(&sdata->dev_attr.attr);
+    sdata->dev_attr.attr.name = sdata->name;
+    sdata->dev_attr.attr.mode = S_IRUGO;
+    sdata->dev_attr.show = show_caps;
+}
+
+static int create_caps_sensor_group(struct occ_sysfs *driver, int sensor_num)
+{
+    struct device *dev = driver->dev;
+    struct sensor_group *sensor_groups = driver->sensor_groups;
+    int field_num = driver->num_caps_fields;
+    struct sensor_attr_data *sdata;
+    int i, j, rc;
+
+    sensor_groups[CAPS].group.attrs =
+            devm_kzalloc(dev, sizeof(struct attribute *) * sensor_num *
+                         field_num + 1, GFP_KERNEL);
+    if (!sensor_groups[CAPS].group.attrs) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    sensor_groups[CAPS].sattr =
+            devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+                         sensor_num * field_num, GFP_KERNEL);
+    if (!sensor_groups[CAPS].sattr) {
+            rc = -ENOMEM;
+            goto err;
+    }
+
+    for (j = 0; j < sensor_num; ++j) {
+            for (i = 0; i < field_num; ++i) {
+                    sdata = &sensor_groups[CAPS].sattr[j * field_num + i];
+                    caps_sensor_attr_init(sdata,
+                                          driver->caps_names[i], j + 1, i);
+                    sensor_groups[CAPS].group.attrs[j * field_num + i] =
+                            &sdata->dev_attr.attr;
+            }
+    }
+
+    rc = sysfs_create_group(&dev->kobj, &sensor_groups[CAPS].group);
+    if (rc)
+            goto err;
+
+    return rc;
+err:
+    deinit_sensor_groups(dev, sensor_groups);
+    return rc;
+}
+
+static void occ_remove_hwmon_attrs(struct occ_sysfs *driver)
+{
+    struct device *dev = driver->dev;
+
+    device_remove_file(dev, &dev_attr_user_powercap);
+    device_remove_file(dev, &dev_attr_update_interval);
+    device_remove_file(dev, &dev_attr_name);
+}
+
+static int occ_create_hwmon_attrs(struct occ_sysfs *driver)
+{
+    int i, rc, id, sensor_num;
+    struct device *dev = driver->dev;
+    struct sensor_group *sensor_groups = driver->sensor_groups;
+    struct occ_blocks *resp = NULL;
+
+    occ_get_response_blocks(driver->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(driver->occ);
+    if (rc) {
+            dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+            return rc;
+    }
+    if (!resp->blocks)
+            return -ENOMEM;
+
+    rc = device_create_file(dev, &dev_attr_name);
+    if (rc)
+            goto error;
+
+    rc = device_create_file(dev, &dev_attr_update_interval);
+    if (rc)
+            goto error;
+
+    if (resp->sensor_block_id[CAPS] >= 0) {
+            /* user powercap: only for master OCC */
+            rc = device_create_file(dev, &dev_attr_user_powercap);
+            if (rc)
+                    goto error;
+    }
+
+    sensor_groups[FREQ].name = "freq";
+    sensor_groups[TEMP].name = "temp";
+    sensor_groups[POWER].name = "power";
+    sensor_groups[CAPS].name = "caps";
+
+    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;
+            if (i == CAPS)
+                    rc = create_caps_sensor_group(driver, sensor_num);
+            else
+                    rc = create_sensor_group(driver, i, sensor_num);
+            if (rc)
+                    goto error;
+    }
+
+    return 0;
+
+error:
+    dev_err(dev, "cannot create hwmon attributes: %d\n", rc);
+    occ_remove_hwmon_attrs(driver);
+    return rc;
+}
+
+static ssize_t show_occ_online(struct device *dev,
+                           struct device_attribute *attr, char *buf)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+    return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->occ_online);
+}
+
+static ssize_t store_occ_online(struct device *dev,
+                            struct device_attribute *attr,
+                            const char *buf, size_t count)
+{
+    struct occ_sysfs *driver = dev_get_drvdata(dev);
+    unsigned long val;
+    int rc;

drivers/hwmon/occ/occ_sysfs.c line 422

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+
+    rc = kstrtoul(buf, 10, &val);
+    if (rc)
+            return rc;
+
+    if (val == 1) {
+            if (driver->occ_online)
+                    return count;
+
+            driver->dev = hwmon_device_register(dev);

hwmon_device_register() is deprecated. Please consider using devm_hwmon_device_register_with_info() or at least hwmon_device_register_with_info().

Uuh ... and registering a hwmon device based on writing into a sysfs attribute is completely out of the question.

Thanks,
Guenter

Quoted 52 lines
drivers/hwmon/occ/occ_sysfs.c new file +52 -0
+            if (IS_ERR(driver->dev))
+                    return PTR_ERR(driver->dev);
+
+            dev_set_drvdata(driver->dev, driver);
+
+            rc = occ_create_hwmon_attrs(driver);
+            if (rc) {
+                    hwmon_device_unregister(driver->dev);
+                    driver->dev = NULL;
+                    return rc;
+            }
+    } else if (val == 0) {
+            if (!driver->occ_online)
+                    return count;
+
+            occ_remove_hwmon_attrs(driver);
+            hwmon_device_unregister(driver->dev);
+            driver->dev = NULL;
+    } else
+            return -EINVAL;
+
+    driver->occ_online = val;
+    return count;
+}
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+               store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                              struct occ_sysfs_config *config)
+{
+    struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+                                           GFP_KERNEL);
+    int rc;
+
+    if (!hwmon)
+            return ERR_PTR(-ENOMEM);
+
+    hwmon->occ = occ;
+    hwmon->num_caps_fields = config->num_caps_fields;
+    hwmon->caps_names = config->caps_names;
+
+    dev_set_drvdata(dev, hwmon);
+
+    rc = device_create_file(dev, &dev_attr_online);
+    if (rc)
+            return ERR_PTR(rc);
+
+    return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+    if (driver->dev) {
+            occ_remove_hwmon_attrs(driver);
+            hwmon_device_unregister(driver->dev);
+    }
+
+    device_remove_file(driver->dev, &dev_attr_online);
+
+    devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free functions. Something is completely wrong here if you need that call.

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

Quoted 67 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+    return 0;
+}
+EXPORT_SYMBOL(occ_sysfs_stop);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("OCC sysfs driver");
+MODULE_LICENSE("GPL");
drivers/hwmon/occ/occ_sysfs.h new file +52 -1
new file mode 100644
index 0000000..2a8044f
@@ -0,0 +1,52 @@
+/*
+ * 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 "occ.h"
+
+struct sensor_group {
+    char *name;
+    struct sensor_attr_data *sattr;
+    struct attribute_group group;
+};
+
+struct occ_sysfs_config {
+    unsigned int num_caps_fields;
+    char **caps_names;
+};
+
+struct occ_sysfs {
+    struct device *dev;
+    struct occ *occ;
+
+    u16 user_powercap;
+    bool occ_online;
+    struct sensor_group sensor_groups[MAX_OCC_SENSOR_TYPE];
+    unsigned long update_interval;
+    unsigned int num_caps_fields;
+    char **caps_names;
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+                              struct occ_sysfs_config *config);
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver);
+
+#endif /* __OCC_SYSFS_H__ */
--
Quoted 2 lines

1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" 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 2/6] hwmon: occ: Add sysfs interface

Quoted 1 line · Brad Bishop

What if the driver is built into the kernel? Is there such a thing as module only drivers?

Our kernel is build with module loading disabled at present.

modprobing the driver when userspace is ready is a neat hack that we could use for now. However, the driver will need a mechanism that tells it to bind to the hardware when it becomes available. The same mechanism would be useful for the fsi driver to initiate a scan and bind it's client drivers.

Cheers,

Joel

#9 lore raw

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

Quoted 11 lines · Joel Stanley
Quoted 1 line · Brad Bishop

What if the driver is built into the kernel? Is there such a thing as module only drivers?

Our kernel is build with module loading disabled at present.

modprobing the driver when userspace is ready is a neat hack that we could use for now. However, the driver will need a mechanism that

Do you happen to know if ‘a mechanism’ is something that needs to be invented or if Linux already has something?

Quoted 7 lines

tells it to bind to the hardware when it becomes available. The same mechanism would be useful for the fsi driver to initiate a scan and bind it's client drivers.

Cheers,

Joel

#10 lore raw

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

Quoted 6 lines · Brad Bishop
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Joel Stanley

On Wed, Jan 4, 2017 at 10:49 AM, Brad Bishop

Quoted 2 lines
Quoted 1 line

<[email protected]> wrote:

What if the driver is built into the kernel?  Is there such a thing as module only drivers?

Quoted 7 lines
Quoted 4 lines

Our kernel is build with module loading disabled at present.

modprobing the driver when userspace is ready is a neat hack that we could use for now. However, the driver will need a mechanism that

Do you happen to know if ‘a mechanism’ is something that needs to be invented or if Linux already has something?

I spoke with Rusty about this and by his response there's no existing "event-bus" mechanism. His suggestions for the problem were:

1. As userspace is coordinating the host power-on, do the modprobe in userspace

2. Push the power-on logic into the kernel so it can trigger hotplug events to probe the drivers, and expose a power-{on,off}/{soft,hard}- reboot interface to userspace.

Andrew

Quoted 8 lines
Quoted 8 lines

tells it to bind to the hardware when it becomes available. The same mechanism would be useful for the fsi driver to initiate a scan and bind it's client drivers.

Cheers,

Joel

#11 lore raw

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

Quoted 20 lines · Andrew Jeffery
Quoted 11 lines · Brad Bishop
Quoted 11 lines
Quoted 2 lines
Quoted 2 lines

On Jan 3, 2017, at 7:06 PM, Joel Stanley <[email protected]> wrote:

On Wed, Jan 4, 2017 at 10:49 AM, Brad Bishop

Quoted 4 lines
Quoted 1 line

<[email protected]> wrote:

What if the driver is built into the kernel?  Is there such a thing as module only drivers?

Our kernel is build with module loading disabled at present.

Quoted 7 lines
Quoted 3 lines

modprobing the driver when userspace is ready is a neat hack that we
could use for now. However, the driver will need a mechanism that

Do you happen to know if ‘a mechanism’ is something that needs to be invented or
if Linux already has something?

Quoted 5 lines

I spoke with Rusty about this and by his response there's no existing "event-bus" mechanism. His suggestions for the problem were:

1. As userspace is coordinating the host power-on, do the modprobe in userspace

After some discussion between Jeremy, Joel, Alistair and myself it was decided that managing the problem from userspace was the way to go.

The rough approach is to describe the devices in the devicetree, but mark them as 'status = "disabled";' so they are not bound to drivers on boot. It is then userspace's job to trigger the binding as necessary, but exactly how it should be done is unknown.

Eddie: Can you investigate what we might need to do to make this work?

Andrew

Quoted 6 lines

2. Push the power-on logic into the kernel so it can trigger hotplug events to probe the drivers, and expose a power-{on,off}/{soft,hard}- reboot interface to userspace.

Andrew

#12 lore raw

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

Will do Andrew.

Just had some discussion with Brad and Patrick. I think we can use the "bind/unbind" sysfs api to cause the occ i2c device to probe when we want it to. Will investigate that today.

Thanks,
Eddie

Quoted 45 lines · Andrew Jeffery
Quoted 20 lines · Andrew Jeffery
Quoted 11 lines · Brad Bishop
Quoted 11 lines
Quoted 2 lines
Quoted 2 lines

On Jan 3, 2017, at 7:06 PM, Joel Stanley <[email protected]> wrote:

On Wed, Jan 4, 2017 at 10:49 AM, Brad Bishop

Quoted 4 lines
Quoted 1 line

<[email protected]> wrote:

What if the driver is built into the kernel? Is there such a thing as module only drivers?

Our kernel is build with module loading disabled at present.

Quoted 7 lines
Quoted 3 lines

modprobing the driver when userspace is ready is a neat hack that we
could use for now. However, the driver will need a mechanism that

Do you happen to know if ‘a mechanism’ is something that needs to be invented or
if Linux already has something?

Quoted 5 lines

I spoke with Rusty about this and by his response there's no existing "event-bus" mechanism. His suggestions for the problem were:

1. As userspace is coordinating the host power-on, do the modprobe in userspace

After some discussion between Jeremy, Joel, Alistair and myself it was decided that managing the problem from userspace was the way to go.

The rough approach is to describe the devices in the devicetree, but mark them as 'status = "disabled";' so they are not bound to drivers on boot. It is then userspace's job to trigger the binding as necessary, but exactly how it should be done is unknown.

Eddie: Can you investigate what we might need to do to make this work?

Andrew

Quoted 6 lines

2. Push the power-on logic into the kernel so it can trigger hotplug events to probe the drivers, and expose a power-{on,off}/{soft,hard}- reboot interface to userspace.

Andrew

#13 lore raw

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

In reply to Guenter Roeck

Hi,

Thanks for your comments Guenter. Sorry to switch email addresses, but gmail is rejecting my messages to this thread.

Guenter Roeck <[email protected]> wrote on 12/30/2016 01:34:04 PM:

Quoted 25 lines · On Fri, Dec 30, 2016 at 11:56:04AM -0600, [email protected] wrote:
Quoted 5 lines

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

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

Quoted 7 lines
---
 Documentation/hwmon/occ       |  48 +++++
 drivers/hwmon/occ/Makefile    |   2 +-
 drivers/hwmon/occ/occ_sysfs.c | 492 +++++++++++++++++++++++++++++

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

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

Quoted 10 lines
 drivers/hwmon/occ/occ_sysfs.h |  52 +++++
 4 files changed, 593 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/occ/occ_sysfs.c
 create mode 100644 drivers/hwmon/occ/occ_sysfs.h
Documentation/hwmon/occ +0 -0
index 79d1642..1ee8689 100644
@@ -25,6 +25,54 @@ Currently, all versions of the OCC support four

types of sensor data: power,

Quoted 8 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,

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

userspace.

Quoted 6 lines

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

Quoted 2 lines
Documentation/hwmon/occ +2 -0
+
+The driver exports two sysfs files for each frequency,

temperature, and power

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+sensor. These are "input" and "label". The input file contains

the value of the

Quoted 1 line
Documentation/hwmon/occ +1 -0
+sensor. The label file contains the sensor id. The sensor id is the

unique

Quoted 6 lines

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+internal OCC identifier. Sensor ids may be provided by the OCC

specification.

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

Quoted 4 lines
Documentation/hwmon/occ +4 -0
+The names of these files will be in the following format:
+   <sensor type><sensor index>_input
+   <sensor type><sensor index>_label
+Sensor types will be one of "temp", "freq", or "power". The sensor

index is

Quoted 1 line

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+an index to differentiate different sensor files. For example, a

single

Quoted 1 line

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+temperature sensor will have two sysfs files: temp1_input and

temp1_label.

Quoted 12 lines

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

Quoted 2 lines
Documentation/hwmon/occ +2 -0
+
+Caps sensors are exported differently. For each caps sensor, the

driver will

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

Quoted 9 lines
Documentation/hwmon/occ +9 -0
+export 6 entries:
+   curr_powercap - current power cap in watts
+   curr_powerreading - current power output in watts
+   norm_powercap - power cap without redundant power
+   max_powercap - maximum power cap that can be set in watts
+   min_powercap - minimum power cap that can be set in watts
+   user_powerlimit - power limit specified by the user in watts
+In addition, the OCC driver for P9 will export a 7th entry:
+   user_powerlimit_source - can be one of two values depending on who

set

Quoted 3 lines

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

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+      the user_powerlimit. 0x1 - out of band from BMC or host. 0x2 -
+      in band from other source.
+The format for these files is caps<sensor index>_<entry type>. For

example,

Quoted 3 lines

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

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+caps1_curr_powercap.
+
+The driver also provides a number of sysfs entries through hwmon to

better

Quoted 4 lines

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

Quoted 4 lines
Documentation/hwmon/occ +4 -0
+control the driver and monitor the OCC.
+   powercap - read or write the OCC user power limit in watts.
+   name - read the name of the driver
+   update_interval - read or write the minimum interval for polling

the

Quoted 4 lines

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

Quoted 3 lines
Documentation/hwmon/occ +3 -0
+      OCC.
+
+The driver also exports a single sysfs file through the

communication protocol

Quoted 9 lines

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+device (see BMC - Host Communications). The filename is "online"

and represents

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+the status of the OCC with respect to the driver. The OCC can be

in one of two

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+states: OCC polling enabled or OCC polling disabled. The purpose

of this file

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+is to control the behavior of the driver and it's hwmon sysfs

entries, not to

Quoted 1 line
Documentation/hwmon/occ +1 -0
+infer any information about the state of the physical OCC. Reading the

file

Quoted 3 lines

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

Quoted 1 line
Documentation/hwmon/occ +1 -0
+returns either a 0 (polling disabled) or 1 (polling enabled).

Writing 1 to the

Quoted 1 line
Documentation/hwmon/occ +1 -0
+file enables OCC polling in the driver if communications can be

established

Quoted 5 lines
Quoted 5 lines
Documentation/hwmon/occ +2 -0
+with the OCC. Writing a 0 to the driver disables OCC polling.
+
 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 15 lines

drivers/hwmon/occ/occ_sysfs.c line 4

Quoted 9 lines

new file mode 100644
index 0000000..b0e063da

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

implementing the OCC

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.c new file +5 -0
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_sysfs.c line 10

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

by

Quoted 45 lines
Quoted 45 lines
drivers/hwmon/occ/occ_sysfs.c new file +45 -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/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/jiffies.h>
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+
+#include "occ_sysfs.h"
+
+#define MAX_SENSOR_ATTR_LEN   32
+
+#define RESP_RETURN_CMD_INVAL   0x13
+
+struct sensor_attr_data {
+   enum sensor_type type;
+   u32 hwmon_index;
+   u32 attr_id;
+   char name[MAX_SENSOR_ATTR_LEN];
+   struct device_attribute dev_attr;
+};
+
+static ssize_t show_input(struct device *dev,
+           struct device_attribute *attr, char *buf)
+{
+   int val;
+   struct sensor_attr_data *sdata = container_of(attr,
+                        struct sensor_attr_data,
+                        dev_attr);
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+   val = occ_get_sensor_value(driver->occ, sdata->type,
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+               sdata->hwmon_index - 1);
+   if (sdata->type == TEMP)
+      val *= 1000;   /* in millidegree Celsius */
+
+   return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+/* show_label provides the OCC sensor id. The sensor id will be either

a

Quoted 28 lines

drivers/hwmon/occ/occ_sysfs.c line 65

Quoted 2 lines
drivers/hwmon/occ/occ_sysfs.c new file +2 -0
+ * 2-byte (for P8) or 4-byte (for P9) value. The sensor id is a way to
+ * identify what each sensor represents, according to the OCC

specification.

Quoted 25 lines
drivers/hwmon/occ/occ_sysfs.c new file +25 -0
+ */
+static ssize_t show_label(struct device *dev,
+           struct device_attribute *attr, char *buf)
+{
+   int val;
+   struct sensor_attr_data *sdata = container_of(attr,
+                        struct sensor_attr_data,
+                        dev_attr);
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+   val = occ_get_sensor_id(driver->occ, sdata->type,
+            sdata->hwmon_index - 1);
+
+   return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_caps(struct device *dev,
+          struct device_attribute *attr, char *buf)
+{
+   int val;
+   struct caps_sensor *sensor;
+   struct sensor_attr_data *sdata = container_of(attr,
+                        struct sensor_attr_data,
+                        dev_attr);
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+   sensor = occ_get_sensor(driver->occ, CAPS);
+   if (!sensor) {
+      val = -1;
+      return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+   }
+
+   val = occ_get_caps_value(driver->occ, sensor, sdata->hwmon_index -

1,

Quoted 11 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 11 lines
drivers/hwmon/occ/occ_sysfs.c new file +11 -0
+             sdata->attr_id);
+
+   return snprintf(buf, PAGE_SIZE - 1, "%d\n", val);
+}
+
+static ssize_t show_update_interval(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+   return snprintf(buf, PAGE_SIZE - 1, "%lu\n", driver->

update_interval);

Quoted 62 lines
Quoted 11 lines
drivers/hwmon/occ/occ_sysfs.c new file +11 -0
+}
+
+static ssize_t store_update_interval(struct device *dev,
+                 struct device_attribute *attr,
+                 const char *buf, size_t count)
+{
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+   unsigned long val;
+   int rc;
+
+   rc = kstrtoul(buf, 10, &val);

drivers/hwmon/occ/occ_sysfs.c

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+   if (rc)
+      return rc;
+
+   driver->update_interval = val;
+   occ_set_update_interval(driver->occ, val);
+
+   return count;
+}
+
+static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,

show_update_interval,

Quoted 40 lines
drivers/hwmon/occ/occ_sysfs.c new file +40 -0
+         store_update_interval);
+
+static ssize_t show_name(struct device *dev,
+          struct device_attribute *attr, char *buf)
+{
+   return snprintf(buf, PAGE_SIZE - 1, "occ\n");
+}
+
+static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
+
+static ssize_t show_user_powercap(struct device *dev,
+              struct device_attribute *attr, char *buf)
+{
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+   return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->user_powercap);
+}
+
+static ssize_t store_user_powercap(struct device *dev,
+               struct device_attribute *attr,
+               const char *buf, size_t count)
+{
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+   u16 val;
+   int rc;
+
+   rc = kstrtou16(buf, 10, &val);
+   if (rc)
+      return rc;
+
+   dev_dbg(dev, "set user powercap to: %d\n", val);
+   rc = occ_set_user_powercap(driver->occ, val);
+   if (rc) {
+      dev_err(dev, "set user powercap failed: 0x%x\n", rc);
+      if (rc == RESP_RETURN_CMD_INVAL) {
+         dev_err(dev, "set invalid powercap value: %d\n", val);
+         return -EINVAL;
+      }
+
+      return rc;
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+   }
+
+   driver->user_powercap = val;
+
+   return count;
+}
+
+static DEVICE_ATTR(user_powercap, S_IWUSR | S_IRUGO,

show_user_powercap,

Quoted 241 lines
Quoted 94 lines
drivers/hwmon/occ/occ_sysfs.c new file +94 -0
+         store_user_powercap);
+
+static void deinit_sensor_groups(struct device *dev,
+             struct sensor_group *sensor_groups)
+{
+   int i;
+
+   for (i = 0; i < MAX_OCC_SENSOR_TYPE; i++) {
+      if (sensor_groups[i].group.attrs)
+         devm_kfree(dev, sensor_groups[i].group.attrs);
+      if (sensor_groups[i].sattr)
+         devm_kfree(dev, sensor_groups[i].sattr);
+      sensor_groups[i].group.attrs = NULL;
+      sensor_groups[i].sattr = NULL;
+   }
+}
+
+static void sensor_attr_init(struct sensor_attr_data *sdata,
+              char *sensor_group_name,
+              char *attr_name,
+              ssize_t (*show)(struct device *dev,
+                    struct device_attribute *attr,
+                    char *buf))
+{
+   sysfs_attr_init(&sdata->dev_attr.attr);
+
+   snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+       sensor_group_name, sdata->hwmon_index, attr_name);
+   sdata->dev_attr.attr.name = sdata->name;
+   sdata->dev_attr.attr.mode = S_IRUGO;
+   sdata->dev_attr.show = show;
+}
+
+static int create_sensor_group(struct occ_sysfs *driver,
+                enum sensor_type type, int sensor_num)
+{
+   struct device *dev = driver->dev;
+   struct sensor_group *sensor_groups = driver->sensor_groups;
+   struct sensor_attr_data *sdata;
+   int rc, i;
+
+   /* each sensor has 'label' and 'input' attributes */
+   sensor_groups[type].group.attrs =
+      devm_kzalloc(dev, sizeof(struct attribute *) *
+              sensor_num * 2 + 1, GFP_KERNEL);
+   if (!sensor_groups[type].group.attrs) {
+      rc = -ENOMEM;
+      goto err;
+   }
+
+   sensor_groups[type].sattr =
+      devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+              sensor_num * 2, GFP_KERNEL);
+   if (!sensor_groups[type].sattr) {
+      rc = -ENOMEM;
+      goto err;
+   }
+
+   for (i = 0; i < sensor_num; i++) {
+      sdata = &sensor_groups[type].sattr[i];
+      /* hwmon attributes index starts from 1 */
+      sdata->hwmon_index = i + 1;
+      sdata->type = type;
+      sensor_attr_init(sdata, sensor_groups[type].name, "input",
+             show_input);
+      sensor_groups[type].group.attrs[i] = &sdata->dev_attr.attr;
+
+      sdata = &sensor_groups[type].sattr[i + sensor_num];
+      sdata->hwmon_index = i + 1;
+      sdata->type = type;
+      sensor_attr_init(sdata, sensor_groups[type].name, "label",
+             show_label);
+      sensor_groups[type].group.attrs[i + sensor_num] =
+         &sdata->dev_attr.attr;
+   }
+
+   rc = sysfs_create_group(&dev->kobj, &sensor_groups[type].group);
+   if (rc)
+      goto err;
+
+   return 0;
+err:
+   deinit_sensor_groups(dev, sensor_groups);
+   return rc;
+}
+
+static void caps_sensor_attr_init(struct sensor_attr_data *sdata,
+              char *attr_name, uint32_t hwmon_index,
+              uint32_t attr_id)
+{
+   sdata->type = CAPS;
+   sdata->hwmon_index = hwmon_index;
+   sdata->attr_id = attr_id;
+

drivers/hwmon/occ/occ_sysfs.c

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+   snprintf(sdata->name, MAX_SENSOR_ATTR_LEN, "%s%d_%s",
+       "caps", sdata->hwmon_index, attr_name);
+
+   sysfs_attr_init(&sdata->dev_attr.attr);
+   sdata->dev_attr.attr.name = sdata->name;
+   sdata->dev_attr.attr.mode = S_IRUGO;
+   sdata->dev_attr.show = show_caps;
+}
+
+static int create_caps_sensor_group(struct occ_sysfs *driver, int

sensor_num)

Quoted 136 lines
drivers/hwmon/occ/occ_sysfs.c new file +136 -0
+{
+   struct device *dev = driver->dev;
+   struct sensor_group *sensor_groups = driver->sensor_groups;
+   int field_num = driver->num_caps_fields;
+   struct sensor_attr_data *sdata;
+   int i, j, rc;
+
+   sensor_groups[CAPS].group.attrs =
+      devm_kzalloc(dev, sizeof(struct attribute *) * sensor_num *
+              field_num + 1, GFP_KERNEL);
+   if (!sensor_groups[CAPS].group.attrs) {
+      rc = -ENOMEM;
+      goto err;
+   }
+
+   sensor_groups[CAPS].sattr =
+      devm_kzalloc(dev, sizeof(struct sensor_attr_data) *
+              sensor_num * field_num, GFP_KERNEL);
+   if (!sensor_groups[CAPS].sattr) {
+      rc = -ENOMEM;
+      goto err;
+   }
+
+   for (j = 0; j < sensor_num; ++j) {
+      for (i = 0; i < field_num; ++i) {
+         sdata = &sensor_groups[CAPS].sattr[j * field_num + i];
+         caps_sensor_attr_init(sdata,
+                     driver->caps_names[i], j + 1, i);
+         sensor_groups[CAPS].group.attrs[j * field_num + i] =
+            &sdata->dev_attr.attr;
+      }
+   }
+
+   rc = sysfs_create_group(&dev->kobj, &sensor_groups[CAPS].group);
+   if (rc)
+      goto err;
+
+   return rc;
+err:
+   deinit_sensor_groups(dev, sensor_groups);
+   return rc;
+}
+
+static void occ_remove_hwmon_attrs(struct occ_sysfs *driver)
+{
+   struct device *dev = driver->dev;
+
+   device_remove_file(dev, &dev_attr_user_powercap);
+   device_remove_file(dev, &dev_attr_update_interval);
+   device_remove_file(dev, &dev_attr_name);
+}
+
+static int occ_create_hwmon_attrs(struct occ_sysfs *driver)
+{
+   int i, rc, id, sensor_num;
+   struct device *dev = driver->dev;
+   struct sensor_group *sensor_groups = driver->sensor_groups;
+   struct occ_blocks *resp = NULL;
+
+   occ_get_response_blocks(driver->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(driver->occ);
+   if (rc) {
+      dev_err(dev, "cannot get occ sensor data: %d\n", rc);
+      return rc;
+   }
+   if (!resp->blocks)
+      return -ENOMEM;
+
+   rc = device_create_file(dev, &dev_attr_name);
+   if (rc)
+      goto error;
+
+   rc = device_create_file(dev, &dev_attr_update_interval);
+   if (rc)
+      goto error;
+
+   if (resp->sensor_block_id[CAPS] >= 0) {
+      /* user powercap: only for master OCC */
+      rc = device_create_file(dev, &dev_attr_user_powercap);
+      if (rc)
+         goto error;
+   }
+
+   sensor_groups[FREQ].name = "freq";
+   sensor_groups[TEMP].name = "temp";
+   sensor_groups[POWER].name = "power";
+   sensor_groups[CAPS].name = "caps";
+
+   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;
+      if (i == CAPS)
+         rc = create_caps_sensor_group(driver, sensor_num);
+      else
+         rc = create_sensor_group(driver, i, sensor_num);
+      if (rc)
+         goto error;
+   }
+
+   return 0;
+
+error:
+   dev_err(dev, "cannot create hwmon attributes: %d\n", rc);
+   occ_remove_hwmon_attrs(driver);
+   return rc;
+}
+
+static ssize_t show_occ_online(struct device *dev,
+                struct device_attribute *attr, char *buf)
+{
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+
+   return snprintf(buf, PAGE_SIZE - 1, "%u\n", driver->occ_online);
+}
+
+static ssize_t store_occ_online(struct device *dev,
+            struct device_attribute *attr,
+            const char *buf, size_t count)
+{
+   struct occ_sysfs *driver = dev_get_drvdata(dev);
+   unsigned long val;
+   int rc;
+
+   rc = kstrtoul(buf, 10, &val);
+   if (rc)
+      return rc;
+
+   if (val == 1) {
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 422

Quoted 4 lines
drivers/hwmon/occ/occ_sysfs.c new file +4 -0
+      if (driver->occ_online)
+         return count;
+
+      driver->dev = hwmon_device_register(dev);

hwmon_device_register() is deprecated. Please consider using devm_hwmon_device_register_with_info() or at least hwmon_device_register_with_info().

OK, I'll address this.

Quoted 2 lines

Uuh ... and registering a hwmon device based on writing into a

sysfsattribute

Quoted 29 lines

is completely out of the question.

Thanks,
Guenter

Quoted 24 lines
drivers/hwmon/occ/occ_sysfs.c new file +24 -0
+      if (IS_ERR(driver->dev))
+         return PTR_ERR(driver->dev);
+
+      dev_set_drvdata(driver->dev, driver);
+
+      rc = occ_create_hwmon_attrs(driver);
+      if (rc) {
+         hwmon_device_unregister(driver->dev);
+         driver->dev = NULL;
+         return rc;
+      }
+   } else if (val == 0) {
+      if (!driver->occ_online)
+         return count;
+
+      occ_remove_hwmon_attrs(driver);
+      hwmon_device_unregister(driver->dev);
+      driver->dev = NULL;
+   } else
+      return -EINVAL;
+
+   driver->occ_online = val;
+   return count;
+}
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c

Quoted 8 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+         store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+              struct occ_sysfs_config *config)
+{
+   struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct

occ_sysfs),

Quoted 24 lines
Quoted 24 lines
drivers/hwmon/occ/occ_sysfs.c new file +24 -0
+                      GFP_KERNEL);
+   int rc;
+
+   if (!hwmon)
+      return ERR_PTR(-ENOMEM);
+
+   hwmon->occ = occ;
+   hwmon->num_caps_fields = config->num_caps_fields;
+   hwmon->caps_names = config->caps_names;
+
+   dev_set_drvdata(dev, hwmon);
+
+   rc = device_create_file(dev, &dev_attr_online);
+   if (rc)
+      return ERR_PTR(rc);
+
+   return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+   if (driver->dev) {
+      occ_remove_hwmon_attrs(driver);
Quoted 8 lines

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 6 lines
drivers/hwmon/occ/occ_sysfs.c new file +6 -0
+      hwmon_device_unregister(driver->dev);
+   }
+
+   device_remove_file(driver->dev, &dev_attr_online);
+
+   devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free

functions.

Quoted 5 lines

Something is completely wrong here if you need that call.

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

We had some trouble designing this driver because the BMC only has access to the OCC once the processor is powered on. This will happen sometime after the BMC boots (this driver runs only on the BMC). With no access to the OCC, we don't know what sensors are present on the system without a large static enumeration. Also any sysfs files created before we have OCC access won't be able to return any data.

Instead of the "online" attribute, what do you think about using the "bind"/"unbind" API to probe the device from user space once the system is powered on? All the hwmon registration would take place in the probe function, it would just occur some time after boot.

Thanks again,
Eddie

Quoted 10 lines

Review

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +8 -0
+
+   return 0;
+}
+EXPORT_SYMBOL(occ_sysfs_stop);
+
+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 15 lines

drivers/hwmon/occ/occ_sysfs.h line 4

Quoted 9 lines

new file mode 100644
index 0000000..2a8044f

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

for the OCC

Quoted 5 lines
drivers/hwmon/occ/occ_sysfs.h new file +5 -0
+ * hwmon sysfs entries.
+ *
+ * Copyright 2016 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or

modify

Quoted 1 line

drivers/hwmon/occ/occ_sysfs.h line 10

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

by

Quoted 46 lines
Quoted 45 lines
drivers/hwmon/occ/occ_sysfs.h new file +42 -1
+ * 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 "occ.h"
+
+struct sensor_group {
+   char *name;
+   struct sensor_attr_data *sattr;
+   struct attribute_group group;
+};
+
+struct occ_sysfs_config {
+   unsigned int num_caps_fields;
+   char **caps_names;
+};
+
+struct occ_sysfs {
+   struct device *dev;
+   struct occ *occ;
+
+   u16 user_powercap;
+   bool occ_online;
+   struct sensor_group sensor_groups[MAX_OCC_SENSOR_TYPE];
+   unsigned long update_interval;
+   unsigned int num_caps_fields;
+   char **caps_names;
+};
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+              struct occ_sysfs_config *config);
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver);
+
+#endif /* __OCC_SYSFS_H__ */
--

1.9.1

maintainer
#14 lore raw

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

On 01/06/2017 02:17 PM, Edward James wrote:

[ ... ]

Quoted 54 lines
Quoted 43 lines
Quoted 29 lines
drivers/hwmon/occ/occ_sysfs.c new file +29 -0
+}
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+         store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+              struct occ_sysfs_config *config)
+{
+   struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+                      GFP_KERNEL);
+   int rc;
+
+   if (!hwmon)
+      return ERR_PTR(-ENOMEM);
+
+   hwmon->occ = occ;
+   hwmon->num_caps_fields = config->num_caps_fields;
+   hwmon->caps_names = config->caps_names;
+
+   dev_set_drvdata(dev, hwmon);
+
+   rc = device_create_file(dev, &dev_attr_online);
+   if (rc)
+      return ERR_PTR(rc);
+
+   return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+   if (driver->dev) {
+      occ_remove_hwmon_attrs(driver);
+      hwmon_device_unregister(driver->dev);
+   }
+
+   device_remove_file(driver->dev, &dev_attr_online);
+
+   devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free functions. Something is completely wrong here if you need that call.

Quoted 3 lines

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

We had some trouble designing this driver because the BMC only has access to the OCC once the processor is powered on. This will happen sometime after the BMC boots (this driver runs only on the BMC). With no access to the OCC, we don't know what sensors are present on the system without a large static enumeration. Also any sysfs files created before we have OCC access won't be able to return any data.

Quoted 5 lines

Instead of the "online" attribute, what do you think about using the "bind"/"unbind" API to probe the device from user space once the system is powered on? All the hwmon registration would take place in the probe function, it would just occur some time after boot.

A more common approach would be to have a platform driver. That platform driver would need a means to detect if the OCC is up and running, and instantiate everything else once it is.

A trigger from user space is problematic because there is no guarantee that the OCC is really up (or that it even exists).

An alternative might be to have the hwmon driver poll for the OCC, but that would be a bit more difficult and might require a kernel thread or maybe asynchronous probing.

Guenter

#15 lore raw

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

Quoted 64 lines · Guenter Roeck

On 01/06/2017 02:17 PM, Edward James wrote:

[ ... ]

Quoted 54 lines
Quoted 43 lines
Quoted 29 lines
drivers/hwmon/occ/occ_sysfs.c new file +29 -0
+}
+
+static DEVICE_ATTR(online, S_IWUSR | S_IRUGO, show_occ_online,
+         store_occ_online);
+
+struct occ_sysfs *occ_sysfs_start(struct device *dev, struct occ *occ,
+              struct occ_sysfs_config *config)
+{
+   struct occ_sysfs *hwmon = devm_kzalloc(dev, sizeof(struct occ_sysfs),
+                      GFP_KERNEL);
+   int rc;
+
+   if (!hwmon)
+      return ERR_PTR(-ENOMEM);
+
+   hwmon->occ = occ;
+   hwmon->num_caps_fields = config->num_caps_fields;
+   hwmon->caps_names = config->caps_names;
+
+   dev_set_drvdata(dev, hwmon);
+
+   rc = device_create_file(dev, &dev_attr_online);
+   if (rc)
+      return ERR_PTR(rc);
+
+   return hwmon;
+}
+EXPORT_SYMBOL(occ_sysfs_start);
+

drivers/hwmon/occ/occ_sysfs.c line 484

Quoted 10 lines
drivers/hwmon/occ/occ_sysfs.c new file +10 -0
+int occ_sysfs_stop(struct device *dev, struct occ_sysfs *driver)
+{
+   if (driver->dev) {
+      occ_remove_hwmon_attrs(driver);
+      hwmon_device_unregister(driver->dev);
+   }
+
+   device_remove_file(driver->dev, &dev_attr_online);
+
+   devm_kfree(dev, driver);

Thw point of using devm_ functions is not to require remove/free functions. Something is completely wrong here if you need that call.

Quoted 3 lines

Overall, this is architectually completely wrong. One does not register or instantiate drivers based on writing into sysfs attributes. Please reconsider your approach.

We had some trouble designing this driver because the BMC only has access to the OCC once the processor is powered on. This will happen sometime after the BMC boots (this driver runs only on the BMC). With no access to the OCC, we don't know what sensors are present on the system without a large static enumeration. Also any sysfs files created before we have OCC access won't be able to return any data.

Quoted 5 lines

Instead of the "online" attribute, what do you think about using the "bind"/"unbind" API to probe the device from user space once the system is powered on? All the hwmon registration would take place in the probe function, it would just occur some time after boot.

Quoted 6 lines

A more common approach would be to have a platform driver. That platform driver would need a means to detect if the OCC is up and running, and instantiate everything else once it is.

A trigger from user space is problematic because there is no guarantee that the OCC is really up (or that it even exists).

This is true in general, but for the BMC case we have more information:
The host CPU power supply is controlled by several GPIOs from userspace. Once we receive the "power-good" signal for the host CPU we can bind the OCC driver and trigger the probe.

Alternatively, in the style of your first para, we could push the host CPU state management into the kernel and expose a boot/reboot/power-off API to userspace. That would give us a place to hook calls for configuring and cleaning up any host-dependent drivers on the BMC.

The solution to the host-power-state problem is also applicable to the OpenFSI patches that were recently sent out:

https://lkml.org/lkml/2016/12/6/732

The OpenFSI infra needs to re-scan for CFAMs when the host is powered up.

Quoted 4 lines

An alternative might be to have the hwmon driver poll for the OCC, but that would be a bit more difficult and might require a kernel thread or maybe asynchronous probing.

This was our thought as a fallback solution.

Andrew

Quoted 3 lines

Guenter

#16 lore raw

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

Quoted 6 lines · Andrew Jeffery

Alternatively, in the style of your first para, we could push the host
CPU state management into the kernel and expose a boot/reboot/power- off
API to userspace. That would give us a place to hook calls for configuring and cleaning up any host-dependent drivers on the BMC.

That's nasty. Each machine has a subtly different way of controller host power, that would mean a different kernel driver for each of them, which we are trying to avoid.

This is userspace policy.

Cheers,
Ben.

#17 lore raw

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

In reply to Guenter Roeck
Quoted 10 lines · Guenter Roeck
Quoted 5 lines

Instead of the "online" attribute, what do you think about using the "bind"/"unbind" API to probe the device from user space once the system is powered on? All the hwmon registration would take place in the probe function, it would just occur some time after boot.

A more common approach would be to have a platform driver. That platform driver would need a means to detect if the OCC is up and running, and instantiate everything else once it is.

Quoted 6 lines

A trigger from user space is problematic because there is no guarantee that the OCC is really up (or that it even exists).

An alternative might be to have the hwmon driver poll for the OCC, but that would be a bit more difficult and might require a kernel thread or maybe asynchronous probing.

Hi Guenter !

I'm not sure I agree with you here ;-)

I'm don't know how much background you got (I missed the beginning of the discussion) but basically this driver runs on the BMC (system controller) of the POWER9 server, the OCC is inside the POWER9 chip itself.

So the presence/power state of the OCC doesn't depend on the BMC
platform kernel code. The BMC userspace controls power up and down to the POWER9, and thus it's the one to know whether the remote is up.

If we were to create a "platform driver", all it would do is get input from userspace exactly like that sysfs file :-)

So if you don't like the sysfs file that registers/de-registers, which I sort-of understand, it's a bit of a hack (though a rather efficient one), I think the bind/unbind approach makes sense. However, I wonder whether the simplest and most efficient (remember this BMC has a really slow CPU) is an "online" file sysfs file, though rather than registering/de-registering the hwmon it would simply make it stop accessing the HW (and return some known "offline" values).

Cheers,
Ben.

maintainer
#18 lore raw

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

Quoted 33 lines · Benjamin Herrenschmidt
Quoted 10 lines · Guenter Roeck
Quoted 5 lines

Instead of the "online" attribute, what do you think about using the "bind"/"unbind" API to probe the device from user space once the system is powered on? All the hwmon registration would take place in the probe function, it would just occur some time after boot.

A more common approach would be to have a platform driver. That platform driver would need a means to detect if the OCC is up and running, and instantiate everything else once it is.

Quoted 6 lines

A trigger from user space is problematic because there is no guarantee that the OCC is really up (or that it even exists).

An alternative might be to have the hwmon driver poll for the OCC, but that would be a bit more difficult and might require a kernel thread or maybe asynchronous probing.

Hi Guenter !

I'm not sure I agree with you here ;-)

I'm don't know how much background you got (I missed the beginning of the discussion) but basically this driver runs on the BMC (system controller) of the POWER9 server, the OCC is inside the POWER9 chip itself.

So the presence/power state of the OCC doesn't depend on the BMC
platform kernel code. The BMC userspace controls power up and down to the POWER9, and thus it's the one to know whether the remote is up.

If we were to create a "platform driver", all it would do is get input from userspace exactly like that sysfs file :-)

Quoted 8 lines

So if you don't like the sysfs file that registers/de-registers, which I sort-of understand, it's a bit of a hack (though a rather efficient one), I think the bind/unbind approach makes sense. However, I wonder whether the simplest and most efficient (remember this BMC has a really slow CPU) is an "online" file sysfs file, though rather than registering/de-registering the hwmon it would simply make it stop accessing the HW (and return some known "offline" values).

I don't like that too much either; it still looks like a hack. How about using bind/unbind then ?

Guenter

#19 lore raw

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

Guenter Roeck <[email protected]> wrote on 01/10/2017 11:51:12 AM:

Quoted 11 lines

From: Guenter Roeck <[email protected]>
To: Benjamin Herrenschmidt <[email protected]>
Cc: Edward James/Austin/IBM@IBMUS, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], linux- [email protected], [email protected], linux- [email protected], [email protected], [email protected], [email protected]
Date: 01/10/2017 11:51 AM
Subject: Re: [PATCH linux 2/6] hwmon: occ: Add sysfs interface

Quoted 3 lines
Quoted 2 lines · Benjamin Herrenschmidt
Quoted 1 line · Guenter Roeck
Quoted 1 line

Instead of the "online" attribute, what do you think about using

the

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

"bind"/"unbind" API to probe the device from user space once the

system

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

is powered on? All the hwmon registration would take place in the

probe

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

function, it would just occur some time after boot.

A more common approach would be to have a platform driver. That

platform

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

driver would need a means to detect if the OCC is up and running, and instantiate everything else once it is.

A trigger from user space is problematic because there is no

guarantee

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

that the OCC is really up (or that it even exists).

An alternative might be to have the hwmon driver poll for the OCC, but that would be a bit more difficult and might require a kernel

thread

Quoted 27 lines
Quoted 6 lines
Quoted 1 line

or maybe asynchronous probing.

Hi Guenter !

I'm not sure I agree with you here ;-)

Quoted 7 lines

I'm don't know how much background you got (I missed the beginning of the discussion) but basically this driver runs on the BMC (system controller) of the POWER9 server, the OCC is inside the POWER9 chip itself.

So the presence/power state of the OCC doesn't depend on the BMC platform kernel code. The BMC userspace controls power up and down

to the POWER9, and thus it's the one to know whether the remote is up.

Quoted 4 lines

If we were to create a "platform driver", all it would do is get input from userspace exactly like that sysfs file :-)

Quoted 8 lines

So if you don't like the sysfs file that registers/de-registers, which I sort-of understand, it's a bit of a hack (though a rather efficient one), I think the bind/unbind approach makes sense. However, I wonder whether the simplest and most efficient (remember this BMC has a really slow CPU) is an "online" file sysfs file, though rather than registering/de-registering the hwmon it would simply make it stop accessing the HW (and return some known "offline" values).

Quoted 2 lines

I don't like that too much either; it still looks like a hack. How about using bind/unbind then ?

Bind/unbind works very well in testing. Only thing we have to figure out is how to stop the driver loading during BMC boot... but I don't think the solution will be in this patch set. I'll put together a v2 for tomorrow.

Thanks,
Eddie

Quoted 3 lines

Guenter

#20 lore raw

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

In reply to [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 | 73 ++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_scom_i2c.h | 26 ++++++++++++++
 2 files changed, 99 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 +73 -0
new file mode 100644
index 0000000..a922f83
@@ -0,0 +1,73 @@
+/*
+ * 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 "scom.h"
+#include "occ_scom_i2c.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 <eajames-r/[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 · 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
#21 lore raw

PATCH 4/6 hwmon: occ: Add callbacks for parsing P8 OCC datastructures

In reply to [email protected]

From: "Edward A. James" <eajames-r/[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 | 217 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/occ_p8.h |  30 +++++++
 3 files changed, 256 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 1ee8689..a6b3dd6 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 +217 -0

217 lines not on this page · raw message

drivers/hwmon/occ/occ_p8.h new file +30 -0
new file mode 100644
index 0000000..b44cdd4
@@ -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;
+
+struct occ *p8_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+int p8_occ_stop(struct occ *occ);
+
+#endif /* __OCC_P8_H__ */
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
#22 lore raw

PATCH 5/6 hwmon: occ: Add hwmon implementation for the P8 OCC

In reply to [email protected]

From: "Edward A. James" <eajames-r/[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.

---
 .../devicetree/bindings/i2c/i2c-ibm-occ.txt        |  13 ++
 drivers/hwmon/occ/Kconfig                          |  14 ++
 drivers/hwmon/occ/Makefile                         |   1 +
 drivers/hwmon/occ/p8_occ_i2c.c                     | 141 +++++++++++++++++++++
 4 files changed, 169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/i2c-ibm-occ.txt
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
Documentation/devicetree/bindings/i2c/i2c-ibm-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 +141 -0

141 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
#23 lore raw

Re: [PATCH linux 5/6] hwmon: occ: Add hwmon implementation for the P8 OCC

reproduce:

Hi Edward,

[auto build test ERROR on hwmon/hwmon-next]
[also build test ERROR on v4.10-rc1 next-20161224]
[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/20161231-021324
base:   https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next
config: x86_64-randconfig-n0-01010656 (attached as .config)
compiler: gcc-4.8 (Debian 4.8.4-1) 4.8.4
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

>> ERROR: "occ_sysfs_start" [drivers/hwmon/occ/p8_occ_i2c.ko] undefined!
>> ERROR: "occ_sysfs_stop" [drivers/hwmon/occ/p8_occ_i2c.ko] undefined!
>> ERROR: "occ_stop" [drivers/hwmon/occ/occ_p8.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

maintainer
#24 lore raw

Re: [PATCH linux 5/6] hwmon: occ: Add hwmon implementation for the P8 OCC

In reply to [email protected]
Quoted 11 lines · On Fri, Dec 30, 2016 at 11:56:07AM -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.

---
 .../devicetree/bindings/i2c/i2c-ibm-occ.txt        |  13 ++

bindings/i2c/ is generally for host controllers. bindings/hwmon perhaps.

Quoted 205 lines
 drivers/hwmon/occ/Kconfig                          |  14 ++
 drivers/hwmon/occ/Makefile                         |   1 +
 drivers/hwmon/occ/p8_occ_i2c.c                     | 141 +++++++++++++++++++++
 4 files changed, 169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/i2c-ibm-occ.txt
 create mode 100644 drivers/hwmon/occ/p8_occ_i2c.c
Documentation/devicetree/bindings/i2c/i2c-ibm-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 +141 -0
new file mode 100644
index 0000000..0c65894
@@ -0,0 +1,141 @@
+/*
+ * p8_occ_i2c.c - hwmon OCC driver
+ *
+ * This file contains the i2c layer for accessing the P8 OCC over i2c bus.
+ *
+ * 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/err.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/err.h>
+#include <linux/of.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+
+#include "scom.h"
+#include "occ_scom_i2c.h"
+#include "occ_p8.h"
+#include "occ_sysfs.h"
+
+#define P8_OCC_I2C_NAME	"p8-occ-i2c"
+
+static char *caps_sensor_names[] = {
+	"curr_powercap",
+	"curr_powerreading",
+	"norm_powercap",
+	"max_powercap",
+	"min_powercap",
+	"user_powerlimit"
+};
+
+int p8_i2c_getscom(void *bus, u32 address, u64 *data)
+{
+	/* P8 i2c slave requires address to be shifted by 1 */
+	address = address << 1;
+
+	return occ_i2c_getscom(bus, address, data);
+}
+
+int p8_i2c_putscom(void *bus, u32 address, u32 data0, u32 data1)
+{
+	/* P8 i2c slave requires address to be shifted by 1 */
+	address = address << 1;
+
+	return occ_i2c_putscom(bus, address, data0, data1);
+}
+
+static struct occ_bus_ops p8_bus_ops = {
+	.getscom = p8_i2c_getscom,
+	.putscom = p8_i2c_putscom,
+};
+
+static struct occ_sysfs_config p8_sysfs_config = {
+	.num_caps_fields = ARRAY_SIZE(caps_sensor_names),
+	.caps_names = caps_sensor_names,
+};
+
+static int p8_occ_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct occ *occ;
+	struct occ_sysfs *hwmon;
+
+	occ = p8_occ_start(&client->dev, client, &p8_bus_ops);
+	if (IS_ERR(occ))
+		return PTR_ERR(occ);
+
+	hwmon = occ_sysfs_start(&client->dev, occ, &p8_sysfs_config);
+	if (IS_ERR(hwmon))
+		return PTR_ERR(hwmon);
+
+	i2c_set_clientdata(client, hwmon);
+
+	return 0;
+}
+
+static int p8_occ_remove(struct i2c_client *client)
+{
+	int rc;
+	struct occ_sysfs *hwmon = i2c_get_clientdata(client);
+	struct occ *occ = hwmon->occ;
+
+	rc = occ_sysfs_stop(&client->dev, hwmon);
+
+	rc = rc || p8_occ_stop(occ);
+
+	return rc;
+}
+
+/* used by old-style board info. */
+static const struct i2c_device_id occ_ids[] = {
+	{ P8_OCC_I2C_NAME, 0 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, occ_ids);
+
+/* used by device table */
+static const struct of_device_id occ_of_match[] = {
+	{ .compatible = "ibm,p8-occ-i2c" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, occ_of_match);
+
+/*
+ * i2c-core uses i2c-detect() to detect device in below address list.
+ * If exists, address will be assigned to client.
+ * It is also possible to read address from device table.
+ */
+static const unsigned short normal_i2c[] = {0x50, 0x51, I2C_CLIENT_END };
+
+static struct i2c_driver p8_occ_driver = {
+	.class = I2C_CLASS_HWMON,
+	.driver = {
+		.name = P8_OCC_I2C_NAME,
+		.pm = NULL,
+		.of_match_table = occ_of_match,
+	},
+	.probe = p8_occ_probe,
+	.remove = p8_occ_remove,
+	.id_table = occ_ids,
+	.address_list = normal_i2c,
+};
+
+module_i2c_driver(p8_occ_driver);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("BMC P8 OCC hwmon driver");
+MODULE_LICENSE("GPL");
Signature · 1 line
1.9.1
#25 lore raw

PATCH 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/p9_occ.c | 243 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/hwmon/occ/p9_occ.h |  30 ++++++
 3 files changed, 276 insertions(+)
 create mode 100644 drivers/hwmon/occ/p9_occ.c
 create mode 100644 drivers/hwmon/occ/p9_occ.h
Documentation/hwmon/occ +3 -0
index a6b3dd6..0f17c2f 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/p9_occ.c new file +243 -0

243 lines not on this page · raw message

drivers/hwmon/occ/p9_occ.h new file +30 -0
new file mode 100644
index 0000000..8130873
@@ -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;
+
+struct occ *p9_occ_start(struct device *dev, void *bus,
+			 struct occ_bus_ops *bus_ops);
+int p9_occ_stop(struct occ *occ);
+
+#endif /* __OCC_P9_H__ */
Signature · 1 line
1.9.1