From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB26832B134 for ; Tue, 30 Jun 2026 22:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782859890; cv=none; b=s8VhNTyRFJ0GqXbJXXTN8Dl3xhV/t2rl6d47OJNj5em4iDhPWVSkC3uaTAS7To9VYB+9I2a59wOLnRIrKU+5CwJIrZ6XBQo/bI4KknQ9ziATRXYpKHW5ULx+l5nkMdk2hjU2IxCI7eIwWsTHO4QhOgLFsuMq4mav4R0IzNzH+n8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782859890; c=relaxed/simple; bh=nUWDqh897qmajP8SSrxwrgXk4ZRjXvT+9q1tST3XpoM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=N+LyKI1HRr6zb1CtyVX78LyYttCzpdm+1pfaOlNiGH+lamrCZBYde7E3dVOUJDn5ONkEXmV5fOwLl+R5GdbsDWzhmDToxnbcdwEXI9Y4p6KyALGL9oV7B4NvbUaFsUXMedqpYMmV5A2iZU68/X7fUxZ7ZO3JWyWkX37CFdU0aNA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Na5TGJT2; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Na5TGJT2" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-493b27c7451so12060535e9.0 for ; Tue, 30 Jun 2026 15:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782859887; x=1783464687; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YFt61Rv4i/gKTp1WVZ1XNmmlE6tpoBJAE5QWfVYntys=; b=Na5TGJT25GLVNMP8wTSO+q0RFCK1fNJzf1a2Zb0yXXHyXajzQRGxFmkZtMH6Hy7EC6 qM0PjbtMtS5cZMLI1AsUcsLO2ywSEPYKLamUjSGyr/gVQg9msLCbPrsZE82Va7YzEz7n 8fcpaUwreWPRbyPTYxnoCoZ3b8g3jCFV5pWuWTVXHvdkfHpVs/qhYdj1Ijcjba1YGNJU dbgMTYJP3ccta7b/hAaqAv915LxsGaOG4NeCJMNH34+j9mylZFWZJ0kyBBqu75r3CkoZ 4CooAmT5CWeG43pPALrWfZn77JfYKPncOhO6OjBojqk0TM7DbZw6aBZTO8abRnwXJNmI mM/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782859887; x=1783464687; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=YFt61Rv4i/gKTp1WVZ1XNmmlE6tpoBJAE5QWfVYntys=; b=rObwNNUZi02EOaS1GBk2bkeQzzhmjw/VnycUhnJFU4pcOXqx93M/2rqa7vUTBMs24p buq18rv2LSVRpyJ+wbsmcsoH5zq/Ql5+CMFWeMDLeBzpajN4CyYvBvFVCrvSicU+FHZf qwwey698t2mfY4sD0NDb7IJZpw2++wTMC7945lAlSVa+d+xys5wo2Zn6KCniFR70ydPA MmUZudNga+JgU0NLNe1ywbyiqXFpciRqZnCC0omK3/vBsO2wtQakn1NCk5L1NY0saEdP 7ScDEX4wPzHxHWStmgFN0i/mISCDVQn62iVeO/X0y0SxBy8eSany6xb0YwlqgUdMWNe4 YwJg== X-Forwarded-Encrypted: i=1; AFNElJ8a1aN4ABB3NzxLM0yf++RqLYTQf3kg+qwPyK/crtgR5f/H/ovo/3lR4ucMnbfyRnIr/F+YDo8OBV5SUQ==@vger.kernel.org X-Gm-Message-State: AOJu0YwtTS+pCLMLttpzcqPEsOE74nX8n+yXYRYcmTfBcilREMUwwC0b 4JsgDaPXKYFLX/tZNSGI5ViJ0mOHba3KU+1wK53S6VLzzE01opPY7bCa X-Gm-Gg: AfdE7ckhzQL3lRg+AbVo0jYKjfOOtpB2pRBWkh9EJUkQ1tJ8tkWrFhpT5YEu+FF7ynF uzmDadrpqqtphEMSsZaxgQm2vfNBno2e5c+Q2bNoolCEQXkn+EFGM1BnYtN4QVfCH6MsSRlScfq 4KLBVRX7EZZwBXk3gWyRfJ5k5r8jLx0StJzk+vJ0y/U4o9GLkpRT4gW3/OHjI6yCy/fKNIUvloS OC3JZ/DWpjz3jvvflhYpl+hOo5IfJ+2bTTGVxm0heQmw7xfB5s1LnxfDVRsnmtE6oei0ToLIKx/ IoTJmXTyhGFCVGJcuYhBDrvSDINJBXBxomhaZKkomyaFPGXw4+crNgNgkjnKPjFc0LLlutItPxJ 84kUfk8ayL48sLrD6BnW/7FcKjMuZWCZCEGa+f2XlitaYZc07ZV/CSQPhHzhdPl+jdLkULm0r X-Received: by 2002:a05:600c:c491:b0:493:a43d:6a2e with SMTP id 5b1f17b1804b1-493bc53486emr46117985e9.8.1782859887507; Tue, 30 Jun 2026 15:51:27 -0700 (PDT) Received: from localhost ([2603:c027:c000:3cde::f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493be81fca5sm31793945e9.14.2026.06.30.15.51.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jun 2026 15:51:26 -0700 (PDT) From: Louis Sautier To: Sathya Prakash , Sreekanth Reddy , Suganath Prabu Subramani , Ranjan Kumar , "James E.J. Bottomley" , "Martin K. Petersen" Cc: Guenter Roeck , MPT-FusionLinux.pdl@broadcom.com, linux-scsi@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, Damien Le Moal Subject: [PATCH v5 2/2] scsi: mpt3sas: add hwmon support Date: Wed, 1 Jul 2026 00:49:22 +0200 Message-ID: <20260630224922.2543096-3-sautier.louis@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260630224922.2543096-1-sautier.louis@gmail.com> References: <20260630224922.2543096-1-sautier.louis@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. The hwmon code is gated directly on CONFIG_HWMON. IS_REACHABLE() is used rather than IS_ENABLED() so that SCSI_MPT3SAS=y with HWMON=m still builds; in that configuration, the sensors are not exposed (same pattern as i915 and xe). Assisted-by: Claude:claude-opus-4-7 Signed-off-by: Louis Sautier Reviewed-by: Damien Le Moal --- drivers/scsi/mpt3sas/Makefile | 2 + drivers/scsi/mpt3sas/mpt3sas_base.h | 17 +++ drivers/scsi/mpt3sas/mpt3sas_hwmon.c | 191 +++++++++++++++++++++++++++ drivers/scsi/mpt3sas/mpt3sas_scsih.c | 6 + 4 files changed, 216 insertions(+) create mode 100644 drivers/scsi/mpt3sas/mpt3sas_hwmon.c diff --git a/drivers/scsi/mpt3sas/Makefile b/drivers/scsi/mpt3sas/Makefile index e76d994dbed3..18e2d87eb4a2 100644 --- a/drivers/scsi/mpt3sas/Makefile +++ b/drivers/scsi/mpt3sas/Makefile @@ -9,3 +9,5 @@ mpt3sas-y += mpt3sas_base.o \ mpt3sas_trigger_diag.o \ mpt3sas_warpdrive.o \ mpt3sas_debugfs.o \ + +mpt3sas-$(CONFIG_HWMON) += mpt3sas_hwmon.o diff --git a/drivers/scsi/mpt3sas/mpt3sas_base.h b/drivers/scsi/mpt3sas/mpt3sas_base.h index fe21b0425047..47255bf9cdda 100644 --- a/drivers/scsi/mpt3sas/mpt3sas_base.h +++ b/drivers/scsi/mpt3sas/mpt3sas_base.h @@ -1629,6 +1629,9 @@ struct MPT3SAS_ADAPTER { u8 is_aero_ioc; struct dentry *debugfs_root; struct dentry *ioc_dump; +#if IS_REACHABLE(CONFIG_HWMON) + struct mpt3sas_hwmon *hwmon; +#endif 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; @@ -2050,6 +2053,20 @@ void mpt3sas_destroy_debugfs(struct MPT3SAS_ADAPTER *ioc); void mpt3sas_init_debugfs(void); void mpt3sas_exit_debugfs(void); +#if IS_REACHABLE(CONFIG_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. diff --git a/drivers/scsi/mpt3sas/mpt3sas_hwmon.c b/drivers/scsi/mpt3sas/mpt3sas_hwmon.c new file mode 100644 index 000000000000..297a8efecadb --- /dev/null +++ b/drivers/scsi/mpt3sas/mpt3sas_hwmon.c @@ -0,0 +1,191 @@ +// 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 +#include +#include + +#include "mpt3sas_base.h" + +struct mpt3sas_hwmon { + struct MPT3SAS_ADAPTER *ioc; + struct device *hwmon_dev; + bool ioc_present; + bool board_present; +}; + +/* + * 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 _hwmon_to_mdegc(s16 raw, u8 units, long *out) +{ + switch (units) { + case MPI2_IOUNITPAGE7_IOC_TEMP_CELSIUS: + *out = (long)raw * 1000; + return 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 _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; + 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 _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; + + r = mpt3sas_config_get_iounit_pg7(ioc, &mpi_reply, &page); + if (r) + return r; + + /* + * 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 (page.IOCTemperatureUnits == MPI2_IOUNITPAGE7_IOC_TEMP_NOT_PRESENT && + page.BoardTemperatureUnits == MPI2_IOUNITPAGE7_BOARD_TEMP_NOT_PRESENT) + return 0; + + h = kzalloc_obj(*h); + if (!h) + return -ENOMEM; + + h->ioc = ioc; + h->ioc_present = page.IOCTemperatureUnits != MPI2_IOUNITPAGE7_IOC_TEMP_NOT_PRESENT; + h->board_present = page.BoardTemperatureUnits != MPI2_IOUNITPAGE7_BOARD_TEMP_NOT_PRESENT; + + 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; +} diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c index 12caffeed3a0..dea78688cc9b 100644 --- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c +++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c @@ -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: -- 2.54.0