Skip to contentSkip to conversation

scsi: mpt3sas: add hwmon support

PATCH RESEND Superseded Louis Sautier 3 participants 15 messages Jun 9 – Jun 30

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover scsi: mpt3sas: add hwmon support

Expose the IOC and board temperature sensors of LSI / Broadcom / Avago SAS HBAs that bind to mpt3sas through the hwmon interface. The data lives in MPI IO Unit Page 7.

The same fields are exposed by Broadcom's userspace tooling through the /dev/mpt[23]ctl ioctl path (typically root-only): IOCTemperature and BoardTemperature in lsiutil; ROC and Controller in storcli. With this driver, sensors(1) shows them unprivileged:

  $ sensors mpt3sas-pci-0200
  mpt3sas-pci-0200
  Adapter: PCI adapter
  IOC:          +42.0°C

v2 -> v3:
v2: https://lore.kernel.org/r/[email protected]
* Removed stale Documentation/hwmon/mpt3sas.rst reference from Kconfig help text.

v1 -> v2:
v1: https://lore.kernel.org/r/[email protected]
* Dropped misleading Documentation/hwmon/mpt3sas.rst.
* Dropped inaccurate concurrency-wait figure from Testing; corrected empirical data is in the on-list discussion.

Testing
-------

Validated across three Broadcom SAS chip generations. None of the cards had a board sensor present, so the testing only covers the IOC channel:

* LSI 9500-8i / SAS3816, SAS-3:
- hwmon device registers as "mpt3sas" with only temp1 (IOC) exposed
- IOC reading matches storcli /c0 show temperature and lsiutil -p1 -a 25,2,0,0
- rmmod / modprobe cycle goes through the explicit unregister/register paths cleanly

* LSI 9305-24i / SAS3224, SAS-3: same behaviour.

* LSI 9211-4i / SAS2004, SAS-2: firmware reports both *TemperatureUnits = NOT_PRESENT, no hwmon device registered (graceful-skip path).

Not verified (no available hardware):
* Path with both IOC and board sensors present.
* Fahrenheit-units conversion.
* Sub-zero readings (signed-cast path).

Louis Sautier (2):
scsi: mpt3sas: add IO Unit Page 7 config accessor
scsi: mpt3sas: add hwmon support

 drivers/scsi/mpt3sas/Kconfig          |   9 ++
 drivers/scsi/mpt3sas/Makefile         |   2 +
 drivers/scsi/mpt3sas/mpt3sas_base.h   |  19 +++
 drivers/scsi/mpt3sas/mpt3sas_config.c |  36 +++++
 drivers/scsi/mpt3sas/mpt3sas_hwmon.c  | 200 ++++++++++++++++++++++++++
 drivers/scsi/mpt3sas/mpt3sas_scsih.c  |   6 +
 6 files changed, 272 insertions(+)
 create mode 100644 drivers/scsi/mpt3sas/mpt3sas_hwmon.c

base-commit: 5d6919055dec134de3c40167a490f33c74c12581

Signature · 1 line
2.54.0
#1 lore raw

PATCH RESEND v3 1/2 scsi: mpt3sas: add IO Unit Page 7 config accessor

Add mpt3sas_config_get_iounit_pg7(), mirroring the existing iounit page accessors. Used by the hwmon driver added in the following patch to read the IOC and board temperatures.

Assisted-by Claude:claude-opus-4-7
---
 drivers/scsi/mpt3sas/mpt3sas_base.h   |  2 ++
 drivers/scsi/mpt3sas/mpt3sas_config.c | 36 +++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
drivers/scsi/mpt3sas/mpt3sas_base.h +2 -0
index d4597d058705..c655742d0dde 100644
@@ -1904,6 +1904,8 @@ int mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc,
 	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage3_t *config_page, u16 sz);
 int mpt3sas_config_set_iounit_pg1(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
 	*mpi_reply, Mpi2IOUnitPage1_t *config_page);
+int mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,
+	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page);
 int mpt3sas_config_get_iounit_pg8(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
 	*mpi_reply, Mpi2IOUnitPage8_t *config_page);
 int mpt3sas_config_get_sas_iounit_pg1(struct MPT3SAS_ADAPTER *ioc,
drivers/scsi/mpt3sas/mpt3sas_config.c +36 -0
index 45ac853e1289..ef07825046bc 100644
@@ -991,6 +991,42 @@ mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc,
 	return r;
 }
 
+/**
+ * mpt3sas_config_get_iounit_pg7 - obtain iounit page 7
+ * @ioc: per adapter object
+ * @mpi_reply: reply mf payload returned from firmware
+ * @config_page: contents of the config page
+ * Context: sleep.
+ *
+ * Return: 0 for success, non-zero for failure.
+ */
+int
+mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,
+	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page)
+{
+	Mpi2ConfigRequest_t mpi_request;
+	int r;
+
+	memset(&mpi_request, 0, sizeof(Mpi2ConfigRequest_t));
+	mpi_request.Function = MPI2_FUNCTION_CONFIG;
+	mpi_request.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
+	mpi_request.Header.PageType = MPI2_CONFIG_PAGETYPE_IO_UNIT;
+	mpi_request.Header.PageNumber = 7;
+	mpi_request.Header.PageVersion = MPI2_IOUNITPAGE7_PAGEVERSION;
+	ioc->build_zero_len_sge_mpi(ioc, &mpi_request.PageBufferSGE);
+	r = _config_request(ioc, &mpi_request, mpi_reply,
+	    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);
+	if (r)
+		goto out;
+
+	mpi_request.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
+	r = _config_request(ioc, &mpi_request, mpi_reply,
+	    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, config_page,
+	    sizeof(*config_page));
+ out:
+	return r;
+}
+
 /**
  * mpt3sas_config_get_iounit_pg8 - obtain iounit page 8
  * @ioc: per adapter object
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v3 RESEND 1/2] scsi: mpt3sas: add IO Unit Page 7 config accessor

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Add mpt3sas_config_get_iounit_pg7(), mirroring the existing iounit page accessors. Used by the hwmon driver added in the following patch to read the IOC and board temperatures.

Assisted-by Claude:claude-opus-4-7

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH v3 RESEND 1/2] scsi: mpt3sas: add IO Unit Page 7 config accessor

In reply to Louis Sautier
Quoted 33 lines · Louis Sautier

Add mpt3sas_config_get_iounit_pg7(), mirroring the existing iounit page accessors. Used by the hwmon driver added in the following patch to read the IOC and board temperatures.

Assisted-by Claude:claude-opus-4-7
---
 drivers/scsi/mpt3sas/mpt3sas_base.h   |  2 ++
 drivers/scsi/mpt3sas/mpt3sas_config.c | 36 +++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
drivers/scsi/mpt3sas/mpt3sas_base.h +2 -0
index d4597d058705..c655742d0dde 100644
@@ -1904,6 +1904,8 @@ int mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc,
 	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage3_t *config_page, u16 sz);
 int mpt3sas_config_set_iounit_pg1(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
 	*mpi_reply, Mpi2IOUnitPage1_t *config_page);
+int mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,
+	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page);
 int mpt3sas_config_get_iounit_pg8(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
 	*mpi_reply, Mpi2IOUnitPage8_t *config_page);
 int mpt3sas_config_get_sas_iounit_pg1(struct MPT3SAS_ADAPTER *ioc,
drivers/scsi/mpt3sas/mpt3sas_config.c +1 -0
index 45ac853e1289..ef07825046bc 100644
@@ -991,6 +991,42 @@ mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc,
 	return r;
 }
 
+/**

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1004

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +10 -0
+ * mpt3sas_config_get_iounit_pg7 - obtain iounit page 7
+ * @ioc: per adapter object
+ * @mpi_reply: reply mf payload returned from firmware
+ * @config_page: contents of the config page
+ * Context: sleep.
+ *
+ * Return: 0 for success, non-zero for failure.
+ */
+int
+mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,

Please do not break the line after "int"

Quoted 4 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +4 -0
+	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page)
+{
+	Mpi2ConfigRequest_t mpi_request;
+	int r;

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1018

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +10 -0
+
+	memset(&mpi_request, 0, sizeof(Mpi2ConfigRequest_t));
+	mpi_request.Function = MPI2_FUNCTION_CONFIG;
+	mpi_request.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
+	mpi_request.Header.PageType = MPI2_CONFIG_PAGETYPE_IO_UNIT;
+	mpi_request.Header.PageNumber = 7;
+	mpi_request.Header.PageVersion = MPI2_IOUNITPAGE7_PAGEVERSION;
+	ioc->build_zero_len_sge_mpi(ioc, &mpi_request.PageBufferSGE);
+	r = _config_request(ioc, &mpi_request, mpi_reply,
+	    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);
	r = _config_request(ioc, &mpi_request, mpi_reply,
			    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);

is a lot nicer to read.

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1025

Quoted 7 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +7 -0
+	if (r)
+		goto out;
+
+	mpi_request.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
+	r = _config_request(ioc, &mpi_request, mpi_reply,
+	    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, config_page,
+	    sizeof(*config_page));

Same here, please align the arguments.

	r = _config_request(ioc, &mpi_request, mpi_reply,
			    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT,
			    config_page, sizeof(*config_page));

With that, looks OK to me.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#4 lore raw

Re: [PATCH v3 RESEND 1/2] scsi: mpt3sas: add IO Unit Page 7 config accessor

Quoted 4 lines · Damien Le Moal

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1004

Quoted 2 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +2 -0
+int
+mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,

Please do not break the line after "int"

Hi and thanks for the review.

Sure, I can change this. Can you confirm we want to diverge from the convention used by neighbouring functions such as mpt3sas_config_get_iounit_pg8?

Quoted 18 lines
Quoted 4 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +4 -0
+	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page)
+{
+	Mpi2ConfigRequest_t mpi_request;
+	int r;

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1018

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +10 -0
+
+	memset(&mpi_request, 0, sizeof(Mpi2ConfigRequest_t));
+	mpi_request.Function = MPI2_FUNCTION_CONFIG;
+	mpi_request.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
+	mpi_request.Header.PageType = MPI2_CONFIG_PAGETYPE_IO_UNIT;
+	mpi_request.Header.PageNumber = 7;
+	mpi_request.Header.PageVersion = MPI2_IOUNITPAGE7_PAGEVERSION;
+	ioc->build_zero_len_sge_mpi(ioc, &mpi_request.PageBufferSGE);
+	r = _config_request(ioc, &mpi_request, mpi_reply,
+	    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);
	r = _config_request(ioc, &mpi_request, mpi_reply,
			    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);
Quoted 1 line

is a lot nicer to read.

Do I also align the signature like so in both the source file and the header?

int mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,
Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page)

#5 lore raw

Re: [PATCH v3 RESEND 1/2] scsi: mpt3sas: add IO Unit Page 7 config accessor

Quoted 11 lines · Louis Sautier
Quoted 4 lines · Damien Le Moal

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1004

Quoted 2 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +2 -0
+int
+mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,

Please do not break the line after "int"

Hi and thanks for the review.

Sure, I can change this. Can you confirm we want to diverge from the convention used by neighbouring functions such as mpt3sas_config_get_iounit_pg8?

My comment is based on the standard kernel coding style, which we should follow.

Quoted 19 lines
Quoted 19 lines
Quoted 5 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +4 -0
+	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page)
+{
+	Mpi2ConfigRequest_t mpi_request;
+	int r;

drivers/scsi/mpt3sas/mpt3sas_config.c mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc, · line 1018

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_config.c +10 -0
+
+	memset(&mpi_request, 0, sizeof(Mpi2ConfigRequest_t));
+	mpi_request.Function = MPI2_FUNCTION_CONFIG;
+	mpi_request.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
+	mpi_request.Header.PageType = MPI2_CONFIG_PAGETYPE_IO_UNIT;
+	mpi_request.Header.PageNumber = 7;
+	mpi_request.Header.PageVersion = MPI2_IOUNITPAGE7_PAGEVERSION;
+	ioc->build_zero_len_sge_mpi(ioc, &mpi_request.PageBufferSGE);
+	r = _config_request(ioc, &mpi_request, mpi_reply,
+	    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);
	r = _config_request(ioc, &mpi_request, mpi_reply,
			    MPT3_CONFIG_PAGE_DEFAULT_TIMEOUT, NULL, 0);
Quoted 4 lines
Quoted 1 line

is a lot nicer to read.

Do I also align the signature like so in both the source file and the header?

I think so, as in my opinion, that makes for a nicer reading.

Quoted 4 lines

int mpt3sas_config_get_iounit_pg7(struct MPT3SAS_ADAPTER *ioc,
Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage7_t *config_page)

Signature · 2 lines
Damien Le Moal
Western Digital Research
#6 lore raw

PATCH RESEND v3 2/2 scsi: mpt3sas: add hwmon support

In reply to Louis Sautier

Expose the IOC and board temperature sensors of LSI / Broadcom SAS HBAs through hwmon. Readings come from MPI IO Unit Page 7 via the accessor added in the preceding patch.

The same fields are exposed by Broadcom's userspace tooling through the /dev/mpt[23]ctl ioctl path (typically root-only):
IOCTemperature and BoardTemperature in lsiutil; ROC and Controller in storcli. With this driver, sensors(1) shows them unprivileged:

  $ sensors mpt3sas-pci-0200
  mpt3sas-pci-0200
  Adapter: PCI adapter
  IOC:          +42.0°C

Each channel is gated independently by its *TemperatureUnits field through is_visible(); cards that populate only one sensor expose only one input file, and cards that populate neither do not register an hwmon device.

Built into mpt3sas.ko under a new CONFIG_SCSI_MPT3SAS_HWMON Kconfig option.

Assisted-by Claude:claude-opus-4-7
---
 drivers/scsi/mpt3sas/Kconfig         |   9 ++
 drivers/scsi/mpt3sas/Makefile        |   2 +
 drivers/scsi/mpt3sas/mpt3sas_base.h  |  17 +++
 drivers/scsi/mpt3sas/mpt3sas_hwmon.c | 200 +++++++++++++++++++++++++++
 drivers/scsi/mpt3sas/mpt3sas_scsih.c |   6 +
 5 files changed, 234 insertions(+)
 create mode 100644 drivers/scsi/mpt3sas/mpt3sas_hwmon.c
drivers/scsi/mpt3sas/Kconfig +9 -0
index c299f7e078fb..a2e1e112b7a3 100644
@@ -73,6 +73,15 @@ config SCSI_MPT3SAS_MAX_SGE
 	can be 256. However, it may decreased down to 16.  Decreasing this
 	parameter will reduce memory requirements on a per controller instance.
 
+config SCSI_MPT3SAS_HWMON
+	bool "LSI MPT Fusion SAS hwmon support"
+	depends on SCSI_MPT3SAS && HWMON
+	depends on !(SCSI_MPT3SAS=y && HWMON=m)
+	help
+	Say Y here to expose the IOC and board temperature sensors of
+	LSI / Broadcom SAS HBAs (such as the 9300, 9400, and 9500 series)
+	through hwmon.
+
 config SCSI_MPT2SAS
 	tristate "Legacy MPT2SAS config option"
 	default n
drivers/scsi/mpt3sas/Makefile +2 -0
index e76d994dbed3..9a2f3ce4158a 100644
@@ -9,3 +9,5 @@ mpt3sas-y +=  mpt3sas_base.o     \
 		mpt3sas_trigger_diag.o \
 		mpt3sas_warpdrive.o \
 		mpt3sas_debugfs.o \
+
+mpt3sas-$(CONFIG_SCSI_MPT3SAS_HWMON) += mpt3sas_hwmon.o
drivers/scsi/mpt3sas/mpt3sas_base.h +17 -0
index c655742d0dde..63252f30343b 100644
@@ -1629,6 +1629,7 @@ struct MPT3SAS_ADAPTER {
 	u8		is_aero_ioc;
 	struct dentry	*debugfs_root;
 	struct dentry	*ioc_dump;
+	struct mpt3sas_hwmon *hwmon;
 	PUT_SMID_IO_FP_HIP put_smid_scsi_io;
 	PUT_SMID_IO_FP_HIP put_smid_fast_path;
 	PUT_SMID_IO_FP_HIP put_smid_hi_priority;
@@ -2049,6 +2050,22 @@ void mpt3sas_destroy_debugfs(struct MPT3SAS_ADAPTER *ioc);
 void mpt3sas_init_debugfs(void);
 void mpt3sas_exit_debugfs(void);
 
+#if IS_ENABLED(CONFIG_SCSI_MPT3SAS_HWMON)
+int mpt3sas_hwmon_register(struct MPT3SAS_ADAPTER *ioc);
+void mpt3sas_hwmon_unregister(struct MPT3SAS_ADAPTER *ioc);
+#else
+static inline int
+mpt3sas_hwmon_register(struct MPT3SAS_ADAPTER *ioc)
+{
+	return 0;
+}
+
+static inline void
+mpt3sas_hwmon_unregister(struct MPT3SAS_ADAPTER *ioc)
+{
+}
+#endif
+
 /**
  * _scsih_is_pcie_scsi_device - determines if device is an pcie scsi device
  * @device_info: bitfield providing information about the device.
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +200 -0

200 lines not on this page · raw message

drivers/scsi/mpt3sas/mpt3sas_scsih.c +6 -0
index 12caffeed3a0..dea78688cc9b 100644
@@ -12562,6 +12562,7 @@ static void scsih_remove(struct pci_dev *pdev)
 	/* release all the volumes */
 	_scsih_ir_shutdown(ioc);
 	mpt3sas_destroy_debugfs(ioc);
+	mpt3sas_hwmon_unregister(ioc);
 	sas_remove_host(shost);
 	list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
 	    list) {
@@ -13651,6 +13652,11 @@ _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	}
 
 	scsi_scan_host(shost);
+
+	if (mpt3sas_hwmon_register(ioc))
+		ioc_warn(ioc,
+			 "hwmon registration failed; temperatures not exposed\n");
+
 	mpt3sas_setup_debugfs(ioc);
 	return 0;
 out_add_shost_fail:
Signature · 1 line
2.54.0
#7 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Expose the IOC and board temperature sensors of LSI / Broadcom SAS HBAs through hwmon. Readings come from MPI IO Unit Page 7 via the accessor added in the preceding patch.

The same fields are exposed by Broadcom's userspace tooling through the /dev/mpt[23]ctl ioctl path (typically root-only):
IOCTemperature and BoardTemperature in lsiutil; ROC and Controller in storcli. With this driver, sensors(1) shows them unprivileged:

  $ sensors mpt3sas-pci-0200
  mpt3sas-pci-0200
  Adapter: PCI adapter
  IOC:          +42.0°C

Each channel is gated independently by its *TemperatureUnits field through is_visible(); cards that populate only one sensor expose only one input file, and cards that populate neither do not register an hwmon device.

Quoted 5 lines

Built into mpt3sas.ko under a new CONFIG_SCSI_MPT3SAS_HWMON Kconfig option.

Assisted-by Claude:claude-opus-4-7

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw
In reply to Louis Sautier
Quoted 24 lines · Louis Sautier

Expose the IOC and board temperature sensors of LSI / Broadcom SAS HBAs through hwmon. Readings come from MPI IO Unit Page 7 via the accessor added in the preceding patch.

The same fields are exposed by Broadcom's userspace tooling through the /dev/mpt[23]ctl ioctl path (typically root-only):
IOCTemperature and BoardTemperature in lsiutil; ROC and Controller in storcli. With this driver, sensors(1) shows them unprivileged:

  $ sensors mpt3sas-pci-0200
  mpt3sas-pci-0200
  Adapter: PCI adapter
  IOC:          +42.0°C

Each channel is gated independently by its *TemperatureUnits field through is_visible(); cards that populate only one sensor expose only one input file, and cards that populate neither do not register an hwmon device.

Built into mpt3sas.ko under a new CONFIG_SCSI_MPT3SAS_HWMON Kconfig option.

Assisted-by Claude:claude-opus-4-7

[...]

drivers/scsi/mpt3sas/Kconfig config SCSI_MPT3SAS_MAX_SGE · line 83

Quoted 8 lines
drivers/scsi/mpt3sas/Kconfig +8 -0
+config SCSI_MPT3SAS_HWMON
+	bool "LSI MPT Fusion SAS hwmon support"
+	depends on SCSI_MPT3SAS && HWMON
+	depends on !(SCSI_MPT3SAS=y && HWMON=m)
+	help
+	Say Y here to expose the IOC and board temperature sensors of
+	LSI / Broadcom SAS HBAs (such as the 9300, 9400, and 9500 series)
+	through hwmon.

Why do you need this ?

Quoted 9 lines
drivers/scsi/mpt3sas/Kconfig +1 -0
+
 config SCSI_MPT2SAS
 	tristate "Legacy MPT2SAS config option"
 	default n
drivers/scsi/mpt3sas/Makefile +0 -0
index e76d994dbed3..9a2f3ce4158a 100644

drivers/scsi/mpt3sas/Makefile mpt3sas-y += mpt3sas_base.o \ · line 13

Quoted 5 lines
drivers/scsi/mpt3sas/Makefile +2 -0
@@ -9,3 +9,5 @@ mpt3sas-y +=  mpt3sas_base.o     \
 		mpt3sas_trigger_diag.o \
 		mpt3sas_warpdrive.o \
 		mpt3sas_debugfs.o \
+
+mpt3sas-$(CONFIG_SCSI_MPT3SAS_HWMON) += mpt3sas_hwmon.o

mpt3sas-$(CONFIG_HWMON) += mpt3sas_hwmon.o

drivers/scsi/mpt3sas/mpt3sas_base.h struct MPT3SAS_ADAPTER { · line 1632

Quoted 9 lines
drivers/scsi/mpt3sas/mpt3sas_base.h +1 -0
index c655742d0dde..63252f30343b 100644
@@ -1629,6 +1629,7 @@ struct MPT3SAS_ADAPTER {
 	u8		is_aero_ioc;
 	struct dentry	*debugfs_root;
 	struct dentry	*ioc_dump;
+	struct mpt3sas_hwmon *hwmon;

This should be conditionally defined with "#ifdef CONFIG_HWMON". Then you can simply drop the config entry you added.

drivers/scsi/mpt3sas/mpt3sas_base.h void mpt3sas_destroy_debugfs(struct MPT3SAS_ADAPTER *ioc); · line 2053

Quoted 8 lines
drivers/scsi/mpt3sas/mpt3sas_base.h +1 -0
 	PUT_SMID_IO_FP_HIP put_smid_scsi_io;
 	PUT_SMID_IO_FP_HIP put_smid_fast_path;
 	PUT_SMID_IO_FP_HIP put_smid_hi_priority;
@@ -2049,6 +2050,22 @@ void mpt3sas_destroy_debugfs(struct MPT3SAS_ADAPTER *ioc);
 void mpt3sas_init_debugfs(void);
 void mpt3sas_exit_debugfs(void);
 
+#if IS_ENABLED(CONFIG_SCSI_MPT3SAS_HWMON)

#if IS_ENABLED(CONFIG_HWMON)

drivers/scsi/mpt3sas/mpt3sas_base.h void mpt3sas_destroy_debugfs(struct MPT3SAS_ADAPTER *ioc); · line 2058

Quoted 5 lines
drivers/scsi/mpt3sas/mpt3sas_base.h +5 -0
+int mpt3sas_hwmon_register(struct MPT3SAS_ADAPTER *ioc);
+void mpt3sas_hwmon_unregister(struct MPT3SAS_ADAPTER *ioc);
+#else
+static inline int
+mpt3sas_hwmon_register(struct MPT3SAS_ADAPTER *ioc)

No line break after "int" please.

drivers/scsi/mpt3sas/mpt3sas_base.h void mpt3sas_destroy_debugfs(struct MPT3SAS_ADAPTER *ioc); · line 2063

Quoted 5 lines
drivers/scsi/mpt3sas/mpt3sas_base.h +5 -0
+{
+	return 0;
+}
+
+static inline void

No line break after void please.

Quoted 36 lines
drivers/scsi/mpt3sas/mpt3sas_base.h +5 -0
+mpt3sas_hwmon_unregister(struct MPT3SAS_ADAPTER *ioc)
+{
+}
+#endif
+
 /**
  * _scsih_is_pcie_scsi_device - determines if device is an pcie scsi device
  * @device_info: bitfield providing information about the device.
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +22 -0
new file mode 100644
index 000000000000..26227a992f35
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hardware monitoring (hwmon) support for the LSI / Broadcom mpt3sas
+ * SAS HBA driver. Exposes the IOC and board temperature sensors by
+ * reading MPI IO Unit Page 7.
+ */
+
+#include <linux/err.h>
+#include <linux/hwmon.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+
+#include "mpt3sas_base.h"
+
+struct mpt3sas_hwmon {
+	struct MPT3SAS_ADAPTER *ioc;
+	struct device *hwmon_dev;
+	bool ioc_present;
+	bool board_present;
+};
+
+/*

drivers/scsi/mpt3sas/mpt3sas_hwmon.c line 32

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +10 -0
+ * Convert a (raw, units) reading to millidegrees Celsius.
+ * Returns -ENODATA when the sensor reports "not present" or
+ * unknown units. Temperature values are interpreted as signed
+ * two's-complement integers.
+ *
+ * The MPI2_IOUNITPAGE7_IOC_TEMP_* and MPI2_IOUNITPAGE7_BOARD_TEMP_*
+ * defines in mpi2_cnfg.h share the same values; the IOC ones are
+ * used for both channels.
+ */
+static int

Remove the line break here please.

Quoted 6 lines
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +6 -0
+_hwmon_to_mdegc(s16 raw, u8 units, long *out)
+{
+	switch (units) {
+	case MPI2_IOUNITPAGE7_IOC_TEMP_CELSIUS:
+		*out = (long)raw * 1000;
+		return 0;

drivers/scsi/mpt3sas/mpt3sas_hwmon.c line 48

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +10 -0
+	case MPI2_IOUNITPAGE7_IOC_TEMP_FAHRENHEIT:
+		/* (F - 32) * 5 / 9, expressed in milli-units */
+		*out = ((long)raw - 32) * 5000 / 9;
+		return 0;
+	default:
+		return -ENODATA;
+	}
+}
+
+static umode_t

And here too.

Quoted 7 lines
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +7 -0
+_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
+		  u32 attr, int channel)
+{
+	const struct mpt3sas_hwmon *h = drvdata;
+
+	if (type != hwmon_temp)
+		return 0;

drivers/scsi/mpt3sas/mpt3sas_hwmon.c line 65

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +10 -0
+	if (attr != hwmon_temp_input && attr != hwmon_temp_label)
+		return 0;
+	if (channel == 0 && h->ioc_present)
+		return 0444;
+	if (channel == 1 && h->board_present)
+		return 0444;
+	return 0;
+}
+
+static int

Again... Not going to comment on the others.

Quoted 159 lines
drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file +135 -0
+_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+	    u32 attr, int channel, long *val)
+{
+	struct mpt3sas_hwmon *h = dev_get_drvdata(dev);
+	Mpi2ConfigReply_t mpi_reply;
+	Mpi2IOUnitPage7_t page;
+	int r;
+
+	if (type != hwmon_temp || attr != hwmon_temp_input)
+		return -EOPNOTSUPP;
+
+	r = mpt3sas_config_get_iounit_pg7(h->ioc, &mpi_reply, &page);
+	if (r)
+		return r;
+
+	if (channel == 0)
+		return _hwmon_to_mdegc((s16)le16_to_cpu(page.IOCTemperature),
+				       page.IOCTemperatureUnits, val);
+	if (channel == 1)
+		return _hwmon_to_mdegc((s16)le16_to_cpu(page.BoardTemperature),
+				       page.BoardTemperatureUnits, val);
+	return -EOPNOTSUPP;
+}
+
+static const char * const mpt3sas_hwmon_temp_labels[] = {
+	"IOC",
+	"Board",
+};
+
+static int
+_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
+		   u32 attr, int channel, const char **str)
+{
+	if (type != hwmon_temp || attr != hwmon_temp_label)
+		return -EOPNOTSUPP;
+	*str = mpt3sas_hwmon_temp_labels[channel];
+	return 0;
+}
+
+static const struct hwmon_channel_info * const mpt3sas_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL,
+};
+
+static const struct hwmon_ops mpt3sas_hwmon_ops = {
+	.is_visible	= _hwmon_is_visible,
+	.read		= _hwmon_read,
+	.read_string	= _hwmon_read_string,
+};
+
+static const struct hwmon_chip_info mpt3sas_hwmon_chip_info = {
+	.ops	= &mpt3sas_hwmon_ops,
+	.info	= mpt3sas_hwmon_info,
+};
+
+/**
+ * mpt3sas_hwmon_register - register an hwmon device for the IOC
+ * @ioc: per adapter object
+ * Context: sleep.
+ *
+ * Succeeds without registering when no temperature sensors are present,
+ * so cards without thermal monitoring do not expose an empty hwmon node.
+ * Paired with mpt3sas_hwmon_unregister() from the driver's remove path.
+ *
+ * Return: 0 for success, non-zero for failure.
+ */
+int
+mpt3sas_hwmon_register(struct MPT3SAS_ADAPTER *ioc)
+{
+	struct device *parent = &ioc->pdev->dev;
+	struct mpt3sas_hwmon *h;
+	struct device *hwdev;
+	Mpi2ConfigReply_t mpi_reply;
+	Mpi2IOUnitPage7_t page;
+	int r;
+
+	h = kzalloc_obj(*h);
+	if (!h)
+		return -ENOMEM;
+
+	h->ioc = ioc;
+
+	r = mpt3sas_config_get_iounit_pg7(ioc, &mpi_reply, &page);
+	if (r) {
+		kfree(h);
+		return r;
+	}
+
+	h->ioc_present = page.IOCTemperatureUnits != MPI2_IOUNITPAGE7_IOC_TEMP_NOT_PRESENT;
+	h->board_present = page.BoardTemperatureUnits != MPI2_IOUNITPAGE7_BOARD_TEMP_NOT_PRESENT;
+
+	/*
+	 * A page where both *TemperatureUnits are NOT_PRESENT covers
+	 * two cases: cards that genuinely lack sensors, and firmware
+	 * errors that left the page zero-filled (the accessor mirrors
+	 * _config_request() behaviour). Either way: skip registration.
+	 */
+	if (!h->ioc_present && !h->board_present) {
+		kfree(h);
+		return 0;
+	}
+
+	hwdev = hwmon_device_register_with_info(parent, "mpt3sas", h,
+						&mpt3sas_hwmon_chip_info,
+						NULL);
+	if (IS_ERR(hwdev)) {
+		kfree(h);
+		return PTR_ERR(hwdev);
+	}
+
+	h->hwmon_dev = hwdev;
+	ioc->hwmon = h;
+	return 0;
+}
+
+/**
+ * mpt3sas_hwmon_unregister - tear down the hwmon device, if any
+ * @ioc: per adapter object
+ *
+ * Safe to call when registration was skipped (no sensors) or
+ * failed; in those cases ioc->hwmon is NULL and this is a no-op.
+ */
+void
+mpt3sas_hwmon_unregister(struct MPT3SAS_ADAPTER *ioc)
+{
+	struct mpt3sas_hwmon *h = ioc->hwmon;
+
+	if (!h)
+		return;
+	hwmon_device_unregister(h->hwmon_dev);
+	kfree(h);
+	ioc->hwmon = NULL;
+}
drivers/scsi/mpt3sas/mpt3sas_scsih.c +6 -0
index 12caffeed3a0..dea78688cc9b 100644
@@ -12562,6 +12562,7 @@ static void scsih_remove(struct pci_dev *pdev)
 	/* release all the volumes */
 	_scsih_ir_shutdown(ioc);
 	mpt3sas_destroy_debugfs(ioc);
+	mpt3sas_hwmon_unregister(ioc);
 	sas_remove_host(shost);
 	list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
 	    list) {
@@ -13651,6 +13652,11 @@ _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	}
 
 	scsi_scan_host(shost);
+
+	if (mpt3sas_hwmon_register(ioc))
+		ioc_warn(ioc,
+			 "hwmon registration failed; temperatures not exposed\n");
+
 	mpt3sas_setup_debugfs(ioc);
 	return 0;
 out_add_shost_fail:
Signature · 2 lines
Damien Le Moal
Western Digital Research
#9 lore raw
Quoted 10 lines · Damien Le Moal

drivers/scsi/mpt3sas/Kconfig config SCSI_MPT3SAS_MAX_SGE · line 83

Quoted 8 lines
drivers/scsi/mpt3sas/Kconfig +8 -0
+config SCSI_MPT3SAS_HWMON
+	bool "LSI MPT Fusion SAS hwmon support"
+	depends on SCSI_MPT3SAS && HWMON
+	depends on !(SCSI_MPT3SAS=y && HWMON=m)
+	help
+	Say Y here to expose the IOC and board temperature sensors of
+	LSI / Broadcom SAS HBAs (such as the 9300, 9400, and 9500 series)
+	through hwmon.

Why do you need this ?

I was following the logic used by NVME_HWMON to prevent issues with SCSI_MPT3SAS=y and HWMON=m.

Quoted 4 lines

drivers/scsi/mpt3sas/mpt3sas_base.h struct MPT3SAS_ADAPTER { · line 1632

Quoted 1 line
drivers/scsi/mpt3sas/mpt3sas_base.h +1 -0
+	struct mpt3sas_hwmon *hwmon;

This should be conditionally defined with "#ifdef CONFIG_HWMON". Then you can simply drop the config entry you added.

If I dropped SCSI_MPT3SAS_HWMON, I would use
"#if IS_REACHABLE(CONFIG_HWMON)" to match what i915_hwmon.h and xe_hwmon.h do and properly handle the SCSI_MPT3SAS=y and HWMON=m case. What do you think?

Quoted 3 lines
Quoted 1 line

+static int

Again... Not going to comment on the others.

Noted, I will fix all of them in v4.

#10 lore raw
Quoted 12 lines · Louis Sautier
Quoted 10 lines · Damien Le Moal

drivers/scsi/mpt3sas/Kconfig config SCSI_MPT3SAS_MAX_SGE · line 83

Quoted 8 lines
drivers/scsi/mpt3sas/Kconfig +8 -0
+config SCSI_MPT3SAS_HWMON
+	bool "LSI MPT Fusion SAS hwmon support"
+	depends on SCSI_MPT3SAS && HWMON
+	depends on !(SCSI_MPT3SAS=y && HWMON=m)
+	help
+	Say Y here to expose the IOC and board temperature sensors of
+	LSI / Broadcom SAS HBAs (such as the 9300, 9400, and 9500 series)
+	through hwmon.

Why do you need this ?

Quoted 2 lines

I was following the logic used by NVME_HWMON to prevent issues with SCSI_MPT3SAS=y and HWMON=m.

Ah, yes, indeed. I did not thought of this case.

Quoted 10 lines
Quoted 5 lines

drivers/scsi/mpt3sas/mpt3sas_base.h struct MPT3SAS_ADAPTER { · line 1632

Quoted 2 lines
drivers/scsi/mpt3sas/mpt3sas_base.h +1 -0
+	struct mpt3sas_hwmon *hwmon;

This should be conditionally defined with "#ifdef CONFIG_HWMON". Then you can simply drop the config entry you added.

If I dropped SCSI_MPT3SAS_HWMON, I would use
"#if IS_REACHABLE(CONFIG_HWMON)" to match what i915_hwmon.h and xe_hwmon.h do and properly handle the SCSI_MPT3SAS=y and HWMON=m case. What do you think?

That seems appropriate. If there is a clean way to avoid adding the new config option, we should use that method.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#11 lore raw
Quoted 7 lines · Damien Le Moal
Quoted 4 lines

If I dropped SCSI_MPT3SAS_HWMON, I would use
"#if IS_REACHABLE(CONFIG_HWMON)" to match what i915_hwmon.h and xe_hwmon.h do and properly handle the SCSI_MPT3SAS=y and HWMON=m case. What do you think?

That seems appropriate. If there is a clean way to avoid adding the new config option, we should use that method.

Hi Damien,

I did this in v4. Could you please review it?

https://lore.kernel.org/linux-hwmon/[email protected]/

#12 lore raw
Quoted 6 lines · Louis Sautier
Quoted 5 lines · Damien Le Moal
Quoted 4 lines

If I dropped SCSI_MPT3SAS_HWMON, I would use
"#if IS_REACHABLE(CONFIG_HWMON)" to match what i915_hwmon.h and xe_hwmon.h do and properly handle the SCSI_MPT3SAS=y and HWMON=m case. What do you think?

Quoted 6 lines
Quoted 2 lines

That seems appropriate. If there is a clean way to avoid adding the new config option, we should use that method.

Hi Damien,

I did this in v4. Could you please review it?

I saw only patch 1/2... I was waiting for the new version of the full series.

Quoted 2 lines

https://lore.kernel.org/linux-hwmon/[email protected]/

Signature · 2 lines
Damien Le Moal
Western Digital Research
#14 lore raw
Quoted 7 lines · Louis Sautier
Quoted 1 line · Damien Le Moal

I saw only patch 1/2... I was waiting for the new version of the full series.

Hi again Damien,

The whole series is there, including 2/2. I sent it all on June 13:
https://lore.kernel.org/linux-hwmon/[email protected]/

If you want me to review, address to me all patches. I only got 1/2 which I had already reviewed.

Signature · 2 lines
Damien Le Moal
Western Digital Research