ipmi: Allow raw access to KCS devices
v3 cover ipmi: Allow raw access to KCS devices
Hi Joel,
This series is (mostly) a backport of v3 I sent upstream:
https://lore.kernel.org/lkml/[email protected]/T/#m26377de5e29c6d7d8091a6584457657e5f740dbc
The patches that aren't on the upstream lists are 17 and 18.
There's no reason that 17 isn't on the upstream lists, and I'll send it shortly (too many branches lying around).
As for patch 18, it's a more thorough implementation of the hacky driver we already have in the OpenBMC tree at drivers/misc/mctp-lpc.c. mctp-lpc.c only worked against KCS channel 4 whereas kcs_bmc_cdev_raw will work with any KCS channel in the system. We need to switch away from KCS channel 4 on the Aspeed BMCs due to the way the status bits are initialised in hardware[1].
Both mctp-lpc.c and kcs_bmc_cdev_raw.c will go away in the mid-term once we have jk's MCTP subsystem merged into net/ and I've implemented the in-kernel LPC binding against it. In the mean time, kcs_bmc_cdev_raw.c replaces mctp-lpc.c. Its chardev semantics are a superset of mctp-lpc.c so there is no userspace impact aside from needing to ship a udev rule to account for the name of the device node.
I plan to send a follow up series to do the switch-over in the devicetree and remove mctp-lpc.c (which also removes a hack from the devicetree).
Aside from patch 18 (which is temporary) there should be no userspace-visible impact from the series. Modulo bugs, this should make it safe to merge now.
I've tested the series on a Rainier system and it can boot the host without issue. I've also exercised all the insmod/rmmod combinations with the kernel debug config options listed in Documentation/process/submit-checklist.rst enabled and found no issues.
Please take a look!
Andrew
[1] They're initialised in IPMI style, with some bits set to indicate the channel isn't active. Unfortunately these are the same bits we chose to represent the channel as active for the LPC MCTP binding.
Andrew Jeffery (18):
ipmi: kcs_bmc_aspeed: Use of match data to extract KCS properties
ipmi: kcs_bmc: Make status update atomic
ipmi: kcs_bmc: Rename {read,write}_{status,data}() functions
ipmi: kcs_bmc: Split out kcs_bmc_cdev_ipmi
ipmi: kcs_bmc: Turn the driver data-structures inside-out
ipmi: kcs_bmc: Split headers into device and client
ipmi: kcs_bmc: Strip private client data from struct kcs_bmc
ipmi: kcs_bmc: Decouple the IPMI chardev from the core
ipmi: kcs_bmc: Allow clients to control KCS IRQ state
ipmi: kcs_bmc: Don't enforce single-open policy in the kernel
ipmi: kcs_bmc: Add serio adaptor
dt-bindings: ipmi: Convert ASPEED KCS binding to schema
dt-bindings: ipmi: Add optional SerIRQ property to ASPEED KCS devices
ipmi: kcs_bmc_aspeed: Implement KCS SerIRQ configuration
ipmi: kcs_bmc_aspeed: Fix IBFIE typo from datasheet
ipmi: kcs_bmc_aspeed: Optionally apply status address
ARM: dts: rainier: Enable KCS channel 2
ipmi: kcs_bmc: Add a "raw" character device interface
Documentation/ABI/testing/dev-raw-kcs | 25 + .../bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml | 106 +++ .../bindings/ipmi/aspeed-kcs-bmc.txt | 33 - arch/arm/boot/dts/aspeed-bmc-ibm-rainier.dts | 5 + drivers/char/ipmi/Kconfig | 44 ++ drivers/char/ipmi/Makefile | 3 + drivers/char/ipmi/kcs_bmc.c | 526 ++++----------- drivers/char/ipmi/kcs_bmc.h | 92 +-- drivers/char/ipmi/kcs_bmc_aspeed.c | 637 +++++++++++++----- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 568 ++++++++++++++++ drivers/char/ipmi/kcs_bmc_cdev_raw.c | 440 ++++++++++++ drivers/char/ipmi/kcs_bmc_client.h | 48 ++ drivers/char/ipmi/kcs_bmc_device.h | 22 + drivers/char/ipmi/kcs_bmc_npcm7xx.c | 94 ++- drivers/char/ipmi/kcs_bmc_serio.c | 151 +++++ 15 files changed, 2071 insertions(+), 723 deletions(-) create mode 100644 Documentation/ABI/testing/dev-raw-kcs create mode 100644 Documentation/devicetree/bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml delete mode 100644 Documentation/devicetree/bindings/ipmi/aspeed-kcs-bmc.txt create mode 100644 drivers/char/ipmi/kcs_bmc_cdev_ipmi.c create mode 100644 drivers/char/ipmi/kcs_bmc_cdev_raw.c create mode 100644 drivers/char/ipmi/kcs_bmc_client.h create mode 100644 drivers/char/ipmi/kcs_bmc_device.h create mode 100644 drivers/char/ipmi/kcs_bmc_serio.c
Signature · 1 line
2.27.0
PATCH v3 1/18 ipmi: kcs_bmc_aspeed: Use of match data to extract KCS properties
Unpack and remove the aspeed_kcs_probe_of_v[12]() functions to aid rearranging how the private device-driver memory is allocated.
--- drivers/char/ipmi/kcs_bmc_aspeed.c | 156 ++++++++++++++--------------- 1 file changed, 77 insertions(+), 79 deletions(-)
drivers/char/ipmi/kcs_bmc_aspeed.c +77 -79
@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/of_address.h> +#include <linux/of_device.h> #include <linux/platform_device.h> #include <linux/poll.h> #include <linux/regmap.h>
@@ -63,6 +64,10 @@ struct aspeed_kcs_bmc {struct regmap *map; }; +struct aspeed_kcs_of_ops { +int (*get_channel)(struct platform_device *pdev); +int (*get_io_address)(struct platform_device *pdev); +};static u8 aspeed_kcs_inb (struct kcs_bmc *kcs_bmc,u32 reg) {
@@ -231,13 +236,10 @@ static const struct kcs_ioreg ast_kcs_bmc_ioregs[KCS_CHANNEL_MAX] = { { .idr = LPC_IDR4, .odr = LPC_ODR4, .str = LPC_STR4 }, }; -static struct kcs_bmc *aspeed_kcs_probe_of_v1 (struct platform_device *pdev) +static int aspeed_kcs_of_v1_get_channel (struct platform_device *pdev) { -struct aspeed_kcs_bmc *priv;struct device_node *np; -struct kcs_bmc *kcs;u32 channel; -u32 slave;int rc; np = pdev->dev.of_node;
@@ -245,105 +247,79 @@ static struct kcs_bmc *aspeed_kcs_probe_of_v1(struct platform_device *pdev) rc =of_property_read_u32 (np,"kcs_chan" , &channel);if ((rc !=0 ) || (channel ==0 || channel >KCS_CHANNEL_MAX )) {dev_err (&pdev->dev,"no valid 'kcs_chan' configured\n" ); -return ERR_PTR (-EINVAL ); +return -EINVAL ; } - kcs =kcs_bmc_alloc (&pdev->dev,sizeof (struct aspeed_kcs_bmc ), channel); -if (!kcs) -return ERR_PTR (-ENOMEM ); +return channel; +} - priv =kcs_bmc_priv (kcs); - priv->map =syscon_node_to_regmap (pdev->dev.parent->of_node); -if (IS_ERR (priv->map)) { -dev_err (&pdev->dev,"Couldn't get regmap\n" ); -return ERR_PTR (-ENODEV ); - } +static int aspeed_kcs_of_v1_get_io_address (struct platform_device *pdev) +{ +u32 slave; +int rc; - rc =of_property_read_u32 (np,"kcs_addr" , &slave); -if (rc) { + rc =of_property_read_u32 (pdev->dev.of_node,"kcs_addr" , &slave); +if (rc || slave >0xffff ) {dev_err (&pdev->dev,"no valid 'kcs_addr' configured\n" ); -return ERR_PTR (-EINVAL ); +return -EINVAL ; } - kcs->ioreg = ast_kcs_bmc_ioregs[channel -1 ]; -aspeed_kcs_set_address (kcs, slave); - -return kcs; -} - -static int aspeed_kcs_calculate_channel (const struct kcs_ioreg *regs) -{ -int i; - -for (i =0 ; i <ARRAY_SIZE (ast_kcs_bmc_ioregs); i++) { -if (!memcmp (&ast_kcs_bmc_ioregs[i], regs,sizeof (*regs))) -return i +1 ; - } - -return -EINVAL ; +return slave; } -static struct kcs_bmc *aspeed_kcs_probe_of_v2 (struct platform_device *pdev) +static int aspeed_kcs_of_v2_get_channel (struct platform_device *pdev) { -struct aspeed_kcs_bmc *priv;struct device_node *np;struct kcs_ioreg ioreg; -struct kcs_bmc *kcs;const __be32 *reg; -int channel; -u32 slave; -int rc; +int i; np = pdev->dev.of_node;/* Don't translate addresses, we want offsets for the regmaps */ reg =of_get_address (np,0 ,NULL ,NULL );if (!reg) -return ERR_PTR (-EINVAL ); +return -EINVAL ; ioreg.idr =be32_to_cpup (reg); reg =of_get_address (np,1 ,NULL ,NULL );if (!reg) -return ERR_PTR (-EINVAL ); +return -EINVAL ; ioreg.odr =be32_to_cpup (reg); reg =of_get_address (np,2 ,NULL ,NULL );if (!reg) -return ERR_PTR (-EINVAL ); +return -EINVAL ; ioreg.str =be32_to_cpup (reg); - channel =aspeed_kcs_calculate_channel (&ioreg); -if (channel <0 ) -return ERR_PTR (channel); - - kcs =kcs_bmc_alloc (&pdev->dev,sizeof (struct aspeed_kcs_bmc ), channel); -if (!kcs) -return ERR_PTR (-ENOMEM ); - - kcs->ioreg = ioreg; - - priv =kcs_bmc_priv (kcs); - priv->map =syscon_node_to_regmap (pdev->dev.parent->of_node); -if (IS_ERR (priv->map)) { -dev_err (&pdev->dev,"Couldn't get regmap\n" ); -return ERR_PTR (-ENODEV ); +for (i =0 ; i <ARRAY_SIZE (ast_kcs_bmc_ioregs); i++) { +if (!memcmp (&ast_kcs_bmc_ioregs[i], &ioreg,sizeof (ioreg))) +return i +1 ; } - rc =of_property_read_u32 (np,"aspeed,lpc-io-reg" , &slave); -if (rc) -return ERR_PTR (rc); +return -EINVAL ; +} -aspeed_kcs_set_address (kcs, slave); +static int aspeed_kcs_of_v2_get_io_address (struct platform_device *pdev) +{ +uint32_t slave; +int rc; -return kcs; + rc =of_property_read_u32 (pdev->dev.of_node,"aspeed,lpc-io-reg" , &slave); +if (rc || slave >0xffff ) +return -EINVAL ; + +return slave; }static int aspeed_kcs_probe (struct platform_device *pdev) { +const struct aspeed_kcs_of_ops *ops;struct device *dev = &pdev->dev; +struct aspeed_kcs_bmc *priv;struct kcs_bmc *kcs_bmc;struct device_node *np; -int rc; +int rc, channel, addr; np = dev->of_node->parent; if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
@@ -352,23 +328,35 @@ static int aspeed_kcs_probe(struct platform_device *pdev)dev_err (dev,"unsupported LPC device binding\n" );return -ENODEV ; } - - np = dev->of_node; -if (of_device_is_compatible (np,"aspeed,ast2400-kcs-bmc" ) || -of_device_is_compatible (np,"aspeed,ast2500-kcs-bmc" )) - kcs_bmc =aspeed_kcs_probe_of_v1 (pdev); -else if (of_device_is_compatible (np,"aspeed,ast2400-kcs-bmc-v2" ) || -of_device_is_compatible (np,"aspeed,ast2500-kcs-bmc-v2" )) - kcs_bmc =aspeed_kcs_probe_of_v2 (pdev); -else + ops =of_device_get_match_data (&pdev->dev); +if (!ops)return -EINVAL ; -if (IS_ERR (kcs_bmc)) -return PTR_ERR (kcs_bmc); + channel = ops->get_channel (pdev); +if (channel <0 ) +return channel; + kcs_bmc =kcs_bmc_alloc (&pdev->dev,sizeof (struct aspeed_kcs_bmc ), channel); +if (!kcs_bmc) +return -ENOMEM ; + + kcs_bmc->ioreg = ast_kcs_bmc_ioregs[channel -1 ]; kcs_bmc->io_inputb = aspeed_kcs_inb; kcs_bmc->io_outputb = aspeed_kcs_outb; + addr = ops->get_io_address (pdev); +if (addr <0 ) +return addr; + + priv =kcs_bmc_priv (kcs_bmc); + priv->map =syscon_node_to_regmap (pdev->dev.parent->of_node); +if (IS_ERR (priv->map)) { +dev_err (&pdev->dev,"Couldn't get regmap\n" ); +return -ENODEV ; + } + +aspeed_kcs_set_address (kcs_bmc, addr); + rc =aspeed_kcs_config_irq (kcs_bmc, pdev);if (rc)return rc;
@@ -400,12 +388,22 @@ static int aspeed_kcs_remove(struct platform_device *pdev)return 0 ; } +static const struct aspeed_kcs_of_ops of_v1_ops = { + .get_channel = aspeed_kcs_of_v1_get_channel, + .get_io_address = aspeed_kcs_of_v1_get_io_address, +}; + +static const struct aspeed_kcs_of_ops of_v2_ops = { + .get_channel = aspeed_kcs_of_v2_get_channel, + .get_io_address = aspeed_kcs_of_v2_get_io_address, +}; +static const struct of_device_id ast_kcs_bmc_match[] = { - { .compatible ="aspeed,ast2400-kcs-bmc" }, - { .compatible ="aspeed,ast2500-kcs-bmc" }, - { .compatible ="aspeed,ast2400-kcs-bmc-v2" }, - { .compatible ="aspeed,ast2500-kcs-bmc-v2" }, - { .compatible ="aspeed,ast2600-kcs-bmc" }, + { .compatible ="aspeed,ast2400-kcs-bmc" , .data = &of_v1_ops }, + { .compatible ="aspeed,ast2500-kcs-bmc" , .data = &of_v1_ops }, + { .compatible ="aspeed,ast2400-kcs-bmc-v2" , .data = &of_v2_ops }, + { .compatible ="aspeed,ast2500-kcs-bmc-v2" , .data = &of_v2_ops }, + { .compatible ="aspeed,ast2600-kcs-bmc" , .data = &of_v2_ops }, { } };MODULE_DEVICE_TABLE (of, ast_kcs_bmc_match);
Signature · 1 line
2.27.0
PATCH v3 2/18 ipmi: kcs_bmc: Make status update atomic
In reply to Andrew JefferyEnable more efficient implementation of read-modify-write sequences. Both device drivers for the KCS BMC stack use regmaps. The new callback allows us to exploit regmap_update_bits().
--- drivers/char/ipmi/kcs_bmc.c | 7 +------ drivers/char/ipmi/kcs_bmc.h | 1 + drivers/char/ipmi/kcs_bmc_aspeed.c | 9 +++++++++ drivers/char/ipmi/kcs_bmc_npcm7xx.c | 10 ++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-)
drivers/char/ipmi/kcs_bmc.c +1 -6
@@ -67,12 +67,7 @@ static inline void write_status(struct kcs_bmc *kcs_bmc, u8 data)static void update_status_bits (struct kcs_bmc *kcs_bmc,u8 mask,u8 val) { -u8 tmp =read_status (kcs_bmc); - - tmp &= ~mask; - tmp |= val & mask; - -write_status (kcs_bmc, tmp); + kcs_bmc->io_updateb (kcs_bmc, kcs_bmc->ioreg.str, mask, val); }static inline void set_state (struct kcs_bmc *kcs_bmc,u8 state)
drivers/char/ipmi/kcs_bmc.h +1 -0
@@ -76,6 +76,7 @@ struct kcs_bmc {struct kcs_ioreg ioreg;u8 (*io_inputb)(struct kcs_bmc *kcs_bmc,u32 reg);void (*io_outputb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 b); +void (*io_updateb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 val);enum kcs_phases phase;enum kcs_errors error;
drivers/char/ipmi/kcs_bmc_aspeed.c +9 -0
@@ -90,6 +90,14 @@ static void aspeed_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)WARN (rc !=0 ,"regmap_write() failed: %d\n" , rc); } +static void aspeed_kcs_updateb (struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 val) +{ +struct aspeed_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc); +int rc; + + rc =regmap_update_bits (priv->map, reg, mask, val); +WARN (rc !=0 ,"regmap_update_bits() failed: %d\n" , rc); +}/* * AST_usrGuide_KCS.pdf
@@ -343,6 +351,7 @@ static int aspeed_kcs_probe(struct platform_device *pdev) kcs_bmc->ioreg = ast_kcs_bmc_ioregs[channel -1 ]; kcs_bmc->io_inputb = aspeed_kcs_inb; kcs_bmc->io_outputb = aspeed_kcs_outb; + kcs_bmc->io_updateb = aspeed_kcs_updateb; addr = ops->get_io_address (pdev);if (addr <0 )
drivers/char/ipmi/kcs_bmc_npcm7xx.c +10 -0
@@ -97,6 +97,15 @@ static void npcm7xx_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)WARN (rc !=0 ,"regmap_write() failed: %d\n" , rc); } +static void npcm7xx_kcs_updateb (struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 data) +{ +struct npcm7xx_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc); +int rc; + + rc =regmap_update_bits (priv->map, reg, mask, data); +WARN (rc !=0 ,"regmap_update_bits() failed: %d\n" , rc); +} +static void npcm7xx_kcs_enable_channel (struct kcs_bmc *kcs_bmc,bool enable) {struct npcm7xx_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc);
@@ -163,6 +172,7 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev) kcs_bmc->ioreg.str = priv->reg->sts; kcs_bmc->io_inputb = npcm7xx_kcs_inb; kcs_bmc->io_outputb = npcm7xx_kcs_outb; + kcs_bmc->io_updateb = npcm7xx_kcs_updateb;dev_set_drvdata (dev, kcs_bmc);
Signature · 1 line
2.27.0
PATCH v3 3/18 ipmi: kcs_bmc: Rename {read, write}_{status, data}() functions
In reply to Andrew JefferyRename the functions in preparation for separating the IPMI chardev out from the KCS BMC core.
--- drivers/char/ipmi/kcs_bmc.c | 52 ++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 27 deletions(-)
drivers/char/ipmi/kcs_bmc.c +25 -27
@@ -45,42 +45,42 @@ enum kcs_states {#define KCS_CMD_WRITE_END 0x62#define KCS_CMD_READ_BYTE 0x68 -static inline u8 read_data (struct kcs_bmc *kcs_bmc) +static inline u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc) {return kcs_bmc->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); } -static inline void write_data (struct kcs_bmc *kcs_bmc,u8 data) +static inline void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data) { kcs_bmc->io_outputb (kcs_bmc, kcs_bmc->ioreg.odr, data); } -static inline u8 read_status (struct kcs_bmc *kcs_bmc) +static inline u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc) {return kcs_bmc->io_inputb (kcs_bmc, kcs_bmc->ioreg.str); } -static inline void write_status (struct kcs_bmc *kcs_bmc,u8 data) +static inline void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data) { kcs_bmc->io_outputb (kcs_bmc, kcs_bmc->ioreg.str, data); } -static void update_status_bits (struct kcs_bmc *kcs_bmc,u8 mask,u8 val) +static void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val) { kcs_bmc->io_updateb (kcs_bmc, kcs_bmc->ioreg.str, mask, val); }static inline void set_state (struct kcs_bmc *kcs_bmc,u8 state) { -update_status_bits (kcs_bmc,KCS_STATUS_STATE_MASK , +kcs_bmc_update_status (kcs_bmc,KCS_STATUS_STATE_MASK ,KCS_STATUS_STATE (state)); }static void kcs_force_abort (struct kcs_bmc *kcs_bmc) {set_state (kcs_bmc,ERROR_STATE ); -read_data (kcs_bmc); -write_data (kcs_bmc,KCS_ZERO_DATA ); +kcs_bmc_read_data (kcs_bmc); +kcs_bmc_write_data (kcs_bmc,KCS_ZERO_DATA ); kcs_bmc->phase =KCS_PHASE_ERROR ; kcs_bmc->data_in_avail = false;
@@ -99,9 +99,9 @@ static void kcs_bmc_handle_data(struct kcs_bmc *kcs_bmc)case KCS_PHASE_WRITE_DATA :if (kcs_bmc->data_in_idx <KCS_MSG_BUFSIZ ) {set_state (kcs_bmc,WRITE_STATE ); -write_data (kcs_bmc,KCS_ZERO_DATA ); +kcs_bmc_write_data (kcs_bmc,KCS_ZERO_DATA ); kcs_bmc->data_in[kcs_bmc->data_in_idx++] = -read_data (kcs_bmc); +kcs_bmc_read_data (kcs_bmc); }else {kcs_force_abort (kcs_bmc); kcs_bmc->error =KCS_LENGTH_ERROR ;
@@ -112,7 +112,7 @@ static void kcs_bmc_handle_data(struct kcs_bmc *kcs_bmc)if (kcs_bmc->data_in_idx <KCS_MSG_BUFSIZ ) {set_state (kcs_bmc,READ_STATE ); kcs_bmc->data_in[kcs_bmc->data_in_idx++] = -read_data (kcs_bmc); +kcs_bmc_read_data (kcs_bmc); kcs_bmc->phase =KCS_PHASE_WRITE_DONE ; kcs_bmc->data_in_avail = true;wake_up_interruptible (&kcs_bmc->queue);
@@ -126,34 +126,34 @@ static void kcs_bmc_handle_data(struct kcs_bmc *kcs_bmc)if (kcs_bmc->data_out_idx == kcs_bmc->data_out_len)set_state (kcs_bmc,IDLE_STATE ); - data =read_data (kcs_bmc); + data =kcs_bmc_read_data (kcs_bmc);if (data !=KCS_CMD_READ_BYTE ) {set_state (kcs_bmc,ERROR_STATE ); -write_data (kcs_bmc,KCS_ZERO_DATA ); +kcs_bmc_write_data (kcs_bmc,KCS_ZERO_DATA );break ; }if (kcs_bmc->data_out_idx == kcs_bmc->data_out_len) { -write_data (kcs_bmc,KCS_ZERO_DATA ); +kcs_bmc_write_data (kcs_bmc,KCS_ZERO_DATA ); kcs_bmc->phase =KCS_PHASE_IDLE ;break ; } -write_data (kcs_bmc, +kcs_bmc_write_data (kcs_bmc, kcs_bmc->data_out[kcs_bmc->data_out_idx++]);break ;case KCS_PHASE_ABORT_ERROR1 :set_state (kcs_bmc,READ_STATE ); -read_data (kcs_bmc); -write_data (kcs_bmc, kcs_bmc->error); +kcs_bmc_read_data (kcs_bmc); +kcs_bmc_write_data (kcs_bmc, kcs_bmc->error); kcs_bmc->phase =KCS_PHASE_ABORT_ERROR2 ;break ;case KCS_PHASE_ABORT_ERROR2 :set_state (kcs_bmc,IDLE_STATE ); -read_data (kcs_bmc); -write_data (kcs_bmc,KCS_ZERO_DATA ); +kcs_bmc_read_data (kcs_bmc); +kcs_bmc_write_data (kcs_bmc,KCS_ZERO_DATA ); kcs_bmc->phase =KCS_PHASE_IDLE ;break ;
@@ -168,9 +168,9 @@ static void kcs_bmc_handle_cmd(struct kcs_bmc *kcs_bmc)u8 cmd;set_state (kcs_bmc,WRITE_STATE ); -write_data (kcs_bmc,KCS_ZERO_DATA ); +kcs_bmc_write_data (kcs_bmc,KCS_ZERO_DATA ); - cmd =read_data (kcs_bmc); + cmd =kcs_bmc_read_data (kcs_bmc);switch (cmd) {case KCS_CMD_WRITE_START : kcs_bmc->phase =KCS_PHASE_WRITE_START ;
@@ -212,7 +212,7 @@ int kcs_bmc_handle_event(struct kcs_bmc *kcs_bmc)spin_lock_irqsave (&kcs_bmc->lock, flags); - status =read_status (kcs_bmc); + status =kcs_bmc_read_status (kcs_bmc);if (status &KCS_STATUS_IBF ) {if (!kcs_bmc->running)kcs_force_abort (kcs_bmc);
@@ -350,7 +350,7 @@ static ssize_t kcs_bmc_write(struct file *filp, const char __user *buf, kcs_bmc->data_out_idx =1 ; kcs_bmc->data_out_len = count;memcpy (kcs_bmc->data_out, kcs_bmc->kbuffer, count); -write_data (kcs_bmc, kcs_bmc->data_out[0 ]); +kcs_bmc_write_data (kcs_bmc, kcs_bmc->data_out[0 ]); ret = count; }else { ret = -EINVAL ;
@@ -373,13 +373,11 @@ static long kcs_bmc_ioctl(struct file *filp, unsigned int cmd,switch (cmd) {case IPMI_BMC_IOCTL_SET_SMS_ATN : -update_status_bits (kcs_bmc,KCS_STATUS_SMS_ATN , -KCS_STATUS_SMS_ATN ); +kcs_bmc_update_status (kcs_bmc,KCS_STATUS_SMS_ATN ,KCS_STATUS_SMS_ATN );break ;case IPMI_BMC_IOCTL_CLEAR_SMS_ATN : -update_status_bits (kcs_bmc,KCS_STATUS_SMS_ATN , -0 ); +kcs_bmc_update_status (kcs_bmc,KCS_STATUS_SMS_ATN ,0 );break ;case IPMI_BMC_IOCTL_FORCE_ABORT :
Signature · 1 line
2.27.0
PATCH v3 4/18 ipmi: kcs_bmc: Split out kcs_bmc_cdev_ipmi
In reply to Andrew JefferyTake steps towards defining a coherent API to separate the KCS device drivers from the userspace interface. Decreasing the coupling will improve the separation of concerns and enable the introduction of alternative userspace interfaces.
For now, simply split the chardev logic out to a separate file. The code continues to build into the same module.
--- drivers/char/ipmi/Makefile | 2 +- drivers/char/ipmi/kcs_bmc.c | 423 +------------------------ drivers/char/ipmi/kcs_bmc.h | 10 +- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 428 ++++++++++++++++++++++++++ 4 files changed, 451 insertions(+), 412 deletions(-) create mode 100644 drivers/char/ipmi/kcs_bmc_cdev_ipmi.c
drivers/char/ipmi/Makefile +1 -1
@@ -22,7 +22,7 @@ obj-$(CONFIG_IPMI_SSIF) += ipmi_ssif.o obj-$(CONFIG_IPMI_POWERNV) += ipmi_powernv.o obj-$(CONFIG_IPMI_WATCHDOG) += ipmi_watchdog.o obj-$(CONFIG_IPMI_POWEROFF) += ipmi_poweroff.o -obj-$(CONFIG_IPMI_KCS_BMC) += kcs_bmc.o +obj-$(CONFIG_IPMI_KCS_BMC) += kcs_bmc.o kcs_bmc_cdev_ipmi.o obj-$(CONFIG_ASPEED_BT_IPMI_BMC) += bt-bmc.o obj-$(CONFIG_ASPEED_KCS_IPMI_BMC) += kcs_bmc_aspeed.o obj-$(CONFIG_NPCM7XX_KCS_IPMI_BMC) += kcs_bmc_npcm7xx.o
drivers/char/ipmi/kcs_bmc.c +14 -409
460 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc.h +8 -2
@@ -104,6 +104,12 @@ static inline void *kcs_bmc_priv(struct kcs_bmc *kcs_bmc) }int kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc); -struct kcs_bmc *kcs_bmc_alloc (struct device *dev,int sizeof_priv, -u32 channel); +struct kcs_bmc *kcs_bmc_alloc (struct device *dev,int sizeof_priv,u32 channel); + +u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc); +void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data); +u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc); +void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data); +void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val); +#endif /* __KCS_BMC_H__ */
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c new file +428 -0
428 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v3 5/18 ipmi: kcs_bmc: Turn the driver data-structures inside-out
In reply to Andrew JefferyMake the KCS device drivers responsible for allocating their own memory.
Until now the private data for the device driver was allocated internal to the private data for the chardev interface. This coupling required the slightly awkward API of passing through the struct size for the driver private data to the chardev constructor, and then retrieving a pointer to the driver private data from the allocated chardev memory.
In addition to being awkward, the arrangement prevents the implementation of alternative userspace interfaces as the device driver private data is not independent.
Peel a layer off the onion and turn the data-structures inside out by exploiting container_of() and embedding struct kcs_device in the driver private data.
--- drivers/char/ipmi/kcs_bmc.c | 19 +++++++-- drivers/char/ipmi/kcs_bmc.h | 12 ++---- drivers/char/ipmi/kcs_bmc_aspeed.c | 56 +++++++++++++------------ drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 60 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_npcm7xx.c | 37 ++++++++++------- 5 files changed, 111 insertions(+), 73 deletions(-)
drivers/char/ipmi/kcs_bmc.c +15 -4
@@ -44,12 +44,23 @@ int kcs_bmc_handle_event(struct kcs_bmc *kcs_bmc) }EXPORT_SYMBOL (kcs_bmc_handle_event); -struct kcs_bmc *kcs_bmc_ipmi_alloc (struct device *dev,int sizeof_priv,u32 channel); -struct kcs_bmc *kcs_bmc_alloc (struct device *dev,int sizeof_priv,u32 channel) +int kcs_bmc_ipmi_add_device (struct kcs_bmc *kcs_bmc); +void kcs_bmc_add_device (struct kcs_bmc *kcs_bmc) { -return kcs_bmc_ipmi_alloc (dev, sizeof_priv, channel); +if (kcs_bmc_ipmi_add_device (kcs_bmc)) +pr_warn ("Failed to add device for KCS channel %d\n" , + kcs_bmc->channel); } -EXPORT_SYMBOL (kcs_bmc_alloc); +EXPORT_SYMBOL (kcs_bmc_add_device); + +int kcs_bmc_ipmi_remove_device (struct kcs_bmc *kcs_bmc); +void kcs_bmc_remove_device (struct kcs_bmc *kcs_bmc) +{ +if (kcs_bmc_ipmi_remove_device (kcs_bmc)) +pr_warn ("Failed to remove device for KCS channel %d\n" , + kcs_bmc->channel); +} +EXPORT_SYMBOL (kcs_bmc_remove_device);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );
drivers/char/ipmi/kcs_bmc.h +4 -8
@@ -67,6 +67,8 @@ struct kcs_ioreg { };struct kcs_bmc { +struct device *dev; +spinlock_t lock;u32 channel;
@@ -94,17 +96,11 @@ struct kcs_bmc {u8 *kbuffer;struct miscdevice miscdev; - -unsigned long priv[]; }; -static inline void *kcs_bmc_priv (struct kcs_bmc *kcs_bmc) -{ -return kcs_bmc->priv; -} -int kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc); -struct kcs_bmc *kcs_bmc_alloc (struct device *dev,int sizeof_priv,u32 channel); +void kcs_bmc_add_device (struct kcs_bmc *kcs_bmc); +void kcs_bmc_remove_device (struct kcs_bmc *kcs_bmc);u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc);void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data);
drivers/char/ipmi/kcs_bmc_aspeed.c +29 -27
138 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +41 -19
84 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_npcm7xx.c +22 -15
@@ -65,6 +65,8 @@ struct npcm7xx_kcs_reg { };struct npcm7xx_kcs_bmc { +struct kcs_bmc kcs_bmc; +struct regmap *map;const struct npcm7xx_kcs_reg *reg;
@@ -76,9 +78,14 @@ static const struct npcm7xx_kcs_reg npcm7xx_kcs_reg_tbl[KCS_CHANNEL_MAX] = { { .sts = KCS3ST, .dob = KCS3DO, .dib = KCS3DI, .ctl = KCS3CTL, .ie = KCS3IE }, }; +static inline struct npcm7xx_kcs_bmc *to_npcm7xx_kcs_bmc (struct kcs_bmc *kcs_bmc) +{ + return container_of(kcs_bmc, struct npcm7xx_kcs_bmc, kcs_bmc); +} +static u8 npcm7xx_kcs_inb (struct kcs_bmc *kcs_bmc,u32 reg) { -struct npcm7xx_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc); +struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);u32 val =0 ;int rc;
@@ -90,7 +97,7 @@ static u8 npcm7xx_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)static void npcm7xx_kcs_outb (struct kcs_bmc *kcs_bmc,u32 reg,u8 data) { -struct npcm7xx_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc); +struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);int rc; rc =regmap_write (priv->map, reg, data);
@@ -99,7 +106,7 @@ static void npcm7xx_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)static void npcm7xx_kcs_updateb (struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 data) { -struct npcm7xx_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc); +struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);int rc; rc =regmap_update_bits (priv->map, reg, mask, data);
@@ -108,7 +115,7 @@ static void npcm7xx_kcs_updateb(struct kcs_bmc *kcs_bmc, u32 reg, u8 mask, u8 dastatic void npcm7xx_kcs_enable_channel (struct kcs_bmc *kcs_bmc,bool enable) { -struct npcm7xx_kcs_bmc *priv =kcs_bmc_priv (kcs_bmc); +struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);regmap_update_bits (priv->map, priv->reg->ctl,KCS_CTL_IBFIE , enable ?KCS_CTL_IBFIE :0 );
@@ -155,11 +162,10 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev)return -ENODEV ; } - kcs_bmc =kcs_bmc_alloc (dev,sizeof (*priv), chan); -if (!kcs_bmc) + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv)return -ENOMEM ; - priv =kcs_bmc_priv (kcs_bmc); priv->map =syscon_node_to_regmap (dev->parent->of_node);if (IS_ERR (priv->map)) {dev_err (dev,"Couldn't get regmap\n" );
@@ -167,6 +173,9 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev) } priv->reg = &npcm7xx_kcs_reg_tbl[chan -1 ]; + kcs_bmc = &priv->kcs_bmc; + kcs_bmc->dev = &pdev->dev; + kcs_bmc->channel = chan; kcs_bmc->ioreg.idr = priv->reg->dib; kcs_bmc->ioreg.odr = priv->reg->dob; kcs_bmc->ioreg.str = priv->reg->sts;
@@ -174,31 +183,29 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev) kcs_bmc->io_outputb = npcm7xx_kcs_outb; kcs_bmc->io_updateb = npcm7xx_kcs_updateb; -dev_set_drvdata (dev, kcs_bmc); +platform_set_drvdata (pdev, priv);npcm7xx_kcs_enable_channel (kcs_bmc, true); rc =npcm7xx_kcs_config_irq (kcs_bmc, pdev);if (rc)return rc; - rc =misc_register (&kcs_bmc->miscdev); -if (rc) { -dev_err (dev,"Unable to register device\n" ); -return rc; - }pr_info ("channel=%u idr=0x%x odr=0x%x str=0x%x\n" , chan, kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr, kcs_bmc->ioreg.str); +kcs_bmc_add_device (kcs_bmc); +return 0 ; }static int npcm7xx_kcs_remove (struct platform_device *pdev) { -struct kcs_bmc *kcs_bmc =dev_get_drvdata (&pdev->dev); +struct npcm7xx_kcs_bmc *priv =platform_get_drvdata (pdev); +struct kcs_bmc *kcs_bmc = &priv->kcs_bmc; -misc_deregister (&kcs_bmc->miscdev); +kcs_bmc_remove_device (kcs_bmc);return 0 ; }
Signature · 1 line
2.27.0
PATCH v3 6/18 ipmi: kcs_bmc: Split headers into device and client
In reply to Andrew JefferyStrengthen the distinction between code that abstracts the implementation of the KCS behaviours (device drivers) and code that exploits KCS behaviours (clients). Neither needs to know about the APIs required by the other, so provide separate headers.
--- drivers/char/ipmi/kcs_bmc.c | 23 ++++++++++------ drivers/char/ipmi/kcs_bmc.h | 27 +++++++++---------- drivers/char/ipmi/kcs_bmc_aspeed.c | 17 ++++++------ drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 39 ++++++++++++++++++--------- drivers/char/ipmi/kcs_bmc_client.h | 30 +++++++++++++++++++++ drivers/char/ipmi/kcs_bmc_device.h | 19 +++++++++++++ drivers/char/ipmi/kcs_bmc_npcm7xx.c | 17 ++++++------ 7 files changed, 120 insertions(+), 52 deletions(-) create mode 100644 drivers/char/ipmi/kcs_bmc_client.h create mode 100644 drivers/char/ipmi/kcs_bmc_device.h
drivers/char/ipmi/kcs_bmc.c +15 -8
@@ -1,46 +1,52 @@// SPDX-License-Identifier: GPL-2.0 /* * Copyright (c) 2015-2018, Intel Corporation. +* Copyright (c) 2021, IBM Corp. */ #include <linux/module.h> #include "kcs_bmc.h" +/* Implement both the device and client interfaces here */ +#include "kcs_bmc_device.h" +#include "kcs_bmc_client.h" + +/* Consumer data access */ +u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc) { -return kcs_bmc->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); +return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); }EXPORT_SYMBOL (kcs_bmc_read_data);void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data) { - kcs_bmc->io_outputb (kcs_bmc, kcs_bmc->ioreg.odr, data); + kcs_bmc->ops->io_outputb (kcs_bmc, kcs_bmc->ioreg.odr, data); }EXPORT_SYMBOL (kcs_bmc_write_data);u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc) { -return kcs_bmc->io_inputb (kcs_bmc, kcs_bmc->ioreg.str); +return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.str); }EXPORT_SYMBOL (kcs_bmc_read_status);void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data) { - kcs_bmc->io_outputb (kcs_bmc, kcs_bmc->ioreg.str, data); + kcs_bmc->ops->io_outputb (kcs_bmc, kcs_bmc->ioreg.str, data); }EXPORT_SYMBOL (kcs_bmc_write_status);void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val) { - kcs_bmc->io_updateb (kcs_bmc, kcs_bmc->ioreg.str, mask, val); + kcs_bmc->ops->io_updateb (kcs_bmc, kcs_bmc->ioreg.str, mask, val); }EXPORT_SYMBOL (kcs_bmc_update_status); -int kcs_bmc_ipmi_event (struct kcs_bmc *kcs_bmc); -int kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc) +irqreturn_t kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc) { -return kcs_bmc_ipmi_event (kcs_bmc); +return kcs_bmc->client.ops->event (&kcs_bmc->client); }EXPORT_SYMBOL (kcs_bmc_handle_event);
@@ -64,4 +70,5 @@ EXPORT_SYMBOL(kcs_bmc_remove_device);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" ); +MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );MODULE_DESCRIPTION ("KCS BMC to handle the IPMI request from system software" );
drivers/char/ipmi/kcs_bmc.h +12 -15
@@ -8,6 +8,12 @@#include <linux/miscdevice.h> +#include "kcs_bmc_client.h" + +#define KCS_BMC_STR_OBF BIT(0) +#define KCS_BMC_STR_IBF BIT(1) +#define KCS_BMC_STR_CMD_DAT BIT(3) +/* Different phases of the KCS BMC module. * KCS_PHASE_IDLE: * BMC should not be expecting nor sending any data.
@@ -66,19 +72,21 @@ struct kcs_ioreg {u32 str; }; +struct kcs_bmc_device_ops ; +struct kcs_bmc {struct device *dev; +const struct kcs_bmc_device_ops *ops; + +struct kcs_bmc_client client; +spinlock_t lock;u32 channel;int running; -/* Setup by BMC KCS controller driver */ struct kcs_ioreg ioreg; -u8 (*io_inputb)(struct kcs_bmc *kcs_bmc,u32 reg); -void (*io_outputb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 b); -void (*io_updateb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 val);enum kcs_phases phase;enum kcs_errors error;
@@ -97,15 +105,4 @@ struct kcs_bmc { struct miscdevice miscdev; }; - -int kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc); -void kcs_bmc_add_device (struct kcs_bmc *kcs_bmc); -void kcs_bmc_remove_device (struct kcs_bmc *kcs_bmc); - -u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc); -void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data); -u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc); -void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data); -void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val); -#endif /* __KCS_BMC_H__ */
drivers/char/ipmi/kcs_bmc_aspeed.c +9 -8
@@ -21,7 +21,7 @@#include <linux/slab.h> #include <linux/timer.h> -#include "kcs_bmc.h" +#include "kcs_bmc_device.h" #define DEVICE_NAME "ast-kcs-bmc"
@@ -220,14 +220,17 @@ static void aspeed_kcs_enable_channel(struct kcs_bmc *kcs_bmc, bool enable) } } +static const struct kcs_bmc_device_ops aspeed_kcs_ops = { + .io_inputb = aspeed_kcs_inb, + .io_outputb = aspeed_kcs_outb, + .io_updateb = aspeed_kcs_updateb, +}; +static irqreturn_t aspeed_kcs_irq (int irq,void *arg) {struct kcs_bmc *kcs_bmc = arg; -if (!kcs_bmc_handle_event (kcs_bmc)) -return IRQ_HANDLED ; - -return IRQ_NONE ; +return kcs_bmc_handle_event (kcs_bmc); } static int aspeed_kcs_config_irq(struct kcs_bmc *kcs_bmc,
@@ -362,9 +365,7 @@ static int aspeed_kcs_probe(struct platform_device *pdev) kcs_bmc->dev = &pdev->dev; kcs_bmc->channel = channel; kcs_bmc->ioreg = ast_kcs_bmc_ioregs[channel -1 ]; - kcs_bmc->io_inputb = aspeed_kcs_inb; - kcs_bmc->io_outputb = aspeed_kcs_outb; - kcs_bmc->io_updateb = aspeed_kcs_updateb; + kcs_bmc->ops = &aspeed_kcs_ops; priv->map =syscon_node_to_regmap (pdev->dev.parent->of_node);if (IS_ERR (priv->map)) {
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +26 -13
@@ -22,7 +22,6 @@#define KCS_ZERO_DATA 0 -/* IPMI 2.0 - Table 9-1, KCS Interface Status Register Bits */ #define KCS_STATUS_STATE (state) (state << 6)#define KCS_STATUS_STATE_MASK GENMASK(7, 6)
@@ -179,12 +178,19 @@ static void kcs_bmc_ipmi_handle_cmd(struct kcs_bmc *kcs_bmc) } } -int kcs_bmc_ipmi_event (struct kcs_bmc *kcs_bmc); -int kcs_bmc_ipmi_event (struct kcs_bmc *kcs_bmc) +static inline struct kcs_bmc *client_to_kcs_bmc (struct kcs_bmc_client *client) { + return container_of(client, struct kcs_bmc, client); +} + +static irqreturn_t kcs_bmc_ipmi_event (struct kcs_bmc_client *client) +{ +struct kcs_bmc *kcs_bmc;unsigned long flags; -int ret = -ENODATA ;u8 status; +int ret; + + kcs_bmc =client_to_kcs_bmc (client);spin_lock_irqsave (&kcs_bmc->lock, flags);
@@ -197,23 +203,28 @@ int kcs_bmc_ipmi_event(struct kcs_bmc *kcs_bmc)else kcs_bmc_ipmi_handle_data (kcs_bmc); - ret =0 ; + ret =IRQ_HANDLED ; + }else { + ret =IRQ_NONE ; }spin_unlock_irqrestore (&kcs_bmc->lock, flags);return ret; } -EXPORT_SYMBOL (kcs_bmc_ipmi_event); -static inline struct kcs_bmc *to_kcs_bmc (struct file *filp) +static const struct kcs_bmc_client_ops kcs_bmc_ipmi_client_ops = { + .event = kcs_bmc_ipmi_event, +}; + +static inline struct kcs_bmc *file_to_kcs_bmc (struct file *filp) { return container_of(filp->private_data, struct kcs_bmc, miscdev); }static int kcs_bmc_ipmi_open (struct inode *inode,struct file *filp) { -struct kcs_bmc *kcs_bmc =to_kcs_bmc (filp); +struct kcs_bmc *kcs_bmc =file_to_kcs_bmc (filp);int ret =0 ;spin_lock_irq (&kcs_bmc->lock);
@@ -228,7 +239,7 @@ static int kcs_bmc_ipmi_open(struct inode *inode, struct file *filp)static __poll_t kcs_bmc_ipmi_poll (struct file *filp,poll_table *wait) { -struct kcs_bmc *kcs_bmc =to_kcs_bmc (filp); +struct kcs_bmc *kcs_bmc =file_to_kcs_bmc (filp);__poll_t mask =0 ;poll_wait (filp, &kcs_bmc->queue, wait);
@@ -244,7 +255,7 @@ static __poll_t kcs_bmc_ipmi_poll(struct file *filp, poll_table *wait)static ssize_t kcs_bmc_ipmi_read (struct file *filp,char __user *buf,size_t count,loff_t *ppos) { -struct kcs_bmc *kcs_bmc =to_kcs_bmc (filp); +struct kcs_bmc *kcs_bmc =file_to_kcs_bmc (filp);bool data_avail;size_t data_len;ssize_t ret;
@@ -306,7 +317,7 @@ static ssize_t kcs_bmc_ipmi_read(struct file *filp, char __user *buf,static ssize_t kcs_bmc_ipmi_write (struct file *filp,const char __user *buf,size_t count,loff_t *ppos) { -struct kcs_bmc *kcs_bmc =to_kcs_bmc (filp); +struct kcs_bmc *kcs_bmc =file_to_kcs_bmc (filp);ssize_t ret;/* a minimum response size '3' : netfn + cmd + ccode */
@@ -342,7 +353,7 @@ static ssize_t kcs_bmc_ipmi_write(struct file *filp, const char __user *buf,static long kcs_bmc_ipmi_ioctl (struct file *filp,unsigned int cmd,unsigned long arg) { -struct kcs_bmc *kcs_bmc =to_kcs_bmc (filp); +struct kcs_bmc *kcs_bmc =file_to_kcs_bmc (filp);long ret =0 ;spin_lock_irq (&kcs_bmc->lock);
@@ -372,7 +383,7 @@ static long kcs_bmc_ipmi_ioctl(struct file *filp, unsigned int cmd,static int kcs_bmc_ipmi_release (struct inode *inode,struct file *filp) { -struct kcs_bmc *kcs_bmc =to_kcs_bmc (filp); +struct kcs_bmc *kcs_bmc =file_to_kcs_bmc (filp);spin_lock_irq (&kcs_bmc->lock); kcs_bmc->running =0 ;
@@ -401,6 +412,8 @@ int kcs_bmc_ipmi_add_device(struct kcs_bmc *kcs_bmc)mutex_init (&kcs_bmc->mutex);init_waitqueue_head (&kcs_bmc->queue); + kcs_bmc->client.dev = kcs_bmc; + kcs_bmc->client.ops = &kcs_bmc_ipmi_client_ops; kcs_bmc->data_in =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); kcs_bmc->data_out =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL ); kcs_bmc->kbuffer =devm_kmalloc (kcs_bmc->dev,KCS_MSG_BUFSIZ ,GFP_KERNEL );
drivers/char/ipmi/kcs_bmc_client.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021, IBM Corp. */ + +#ifndef __KCS_BMC_CONSUMER_H__ +#define __KCS_BMC_CONSUMER_H__ + +#include <linux/irqreturn.h> +#include <linux/list.h> +#include <linux/notifier.h> +#include <stdbool.h> + +struct kcs_bmc ; +struct kcs_bmc_client_ops ; + +struct kcs_bmc_client { +const struct kcs_bmc_client_ops *ops; + +struct kcs_bmc *dev; +}; + +struct kcs_bmc_client_ops { +irqreturn_t (*event)(struct kcs_bmc_client *client); +}; + +u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc); +void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data); +u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc); +void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data); +void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val); +#endif
drivers/char/ipmi/kcs_bmc_device.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2021, IBM Corp. */ + +#ifndef __KCS_BMC_DEVICE_H__ +#define __KCS_BMC_DEVICE_H__ + +#include "kcs_bmc.h" + +struct kcs_bmc_device_ops { +u8 (*io_inputb)(struct kcs_bmc *kcs_bmc,u32 reg); +void (*io_outputb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 b); +void (*io_updateb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 b); +}; + +irqreturn_t kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc); +void kcs_bmc_add_device (struct kcs_bmc *kcs_bmc); +void kcs_bmc_remove_device (struct kcs_bmc *kcs_bmc); + +#endif
drivers/char/ipmi/kcs_bmc_npcm7xx.c +9 -8
@@ -17,7 +17,7 @@#include <linux/regmap.h> #include <linux/slab.h> -#include "kcs_bmc.h" +#include "kcs_bmc_device.h" #define DEVICE_NAME "npcm-kcs-bmc"#define KCS_CHANNEL_MAX 3
@@ -128,10 +128,7 @@ static irqreturn_t npcm7xx_kcs_irq(int irq, void *arg) {struct kcs_bmc *kcs_bmc = arg; -if (!kcs_bmc_handle_event (kcs_bmc)) -return IRQ_HANDLED ; - -return IRQ_NONE ; +return kcs_bmc_handle_event (kcs_bmc); } static int npcm7xx_kcs_config_irq(struct kcs_bmc *kcs_bmc,
@@ -148,6 +145,12 @@ static int npcm7xx_kcs_config_irq(struct kcs_bmc *kcs_bmc, dev_name(dev), kcs_bmc); } +static const struct kcs_bmc_device_ops npcm7xx_kcs_ops = { + .io_inputb = npcm7xx_kcs_inb, + .io_outputb = npcm7xx_kcs_outb, + .io_updateb = npcm7xx_kcs_updateb, +}; +static int npcm7xx_kcs_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev;
@@ -179,9 +182,7 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev) kcs_bmc->ioreg.idr = priv->reg->dib; kcs_bmc->ioreg.odr = priv->reg->dob; kcs_bmc->ioreg.str = priv->reg->sts; - kcs_bmc->io_inputb = npcm7xx_kcs_inb; - kcs_bmc->io_outputb = npcm7xx_kcs_outb; - kcs_bmc->io_updateb = npcm7xx_kcs_updateb; + kcs_bmc->ops = &npcm7xx_kcs_ops;platform_set_drvdata (pdev, priv);
Signature · 1 line
2.27.0
PATCH v3 7/18 ipmi: kcs_bmc: Strip private client data from struct kcs_bmc
In reply to Andrew JefferyMove all client-private data out of struct kcs_bmc into the KCS client implementation.
With this change the KCS BMC core code now only concerns itself with abstract struct kcs_bmc and struct kcs_bmc_client types, achieving expected separation of concerns. Further, the change clears the path for implementation of alternative userspace interfaces.
The chardev data-structures are rearranged in the same manner applied to the KCS device driver data-structures in an earlier patch - struct kcs_bmc_client is embedded in the client's private data and we exploit container_of() to translate as required.
Finally, now that it is free of client data, struct kcs_bmc is renamed to struct kcs_bmc_device to contrast struct kcs_bmc_client.
--- drivers/char/ipmi/kcs_bmc.c | 70 ++++- drivers/char/ipmi/kcs_bmc.h | 83 +---- drivers/char/ipmi/kcs_bmc_aspeed.c | 22 +- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 428 ++++++++++++++++---------- drivers/char/ipmi/kcs_bmc_client.h | 28 +- drivers/char/ipmi/kcs_bmc_device.h | 14 +- drivers/char/ipmi/kcs_bmc_npcm7xx.c | 20 +- 7 files changed, 369 insertions(+), 296 deletions(-)
drivers/char/ipmi/kcs_bmc.c +59 -11
@@ -4,6 +4,7 @@* Copyright (c) 2021, IBM Corp. */ +#include <linux/device.h> #include <linux/module.h> #include "kcs_bmc.h"
@@ -14,44 +15,91 @@/* Consumer data access */ -u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc) +u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc) {return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.idr); }EXPORT_SYMBOL (kcs_bmc_read_data); -void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data) +void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data) { kcs_bmc->ops->io_outputb (kcs_bmc, kcs_bmc->ioreg.odr, data); }EXPORT_SYMBOL (kcs_bmc_write_data); -u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc) +u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc) {return kcs_bmc->ops->io_inputb (kcs_bmc, kcs_bmc->ioreg.str); }EXPORT_SYMBOL (kcs_bmc_read_status); -void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data) +void kcs_bmc_write_status (struct kcs_bmc_device *kcs_bmc,u8 data) { kcs_bmc->ops->io_outputb (kcs_bmc, kcs_bmc->ioreg.str, data); }EXPORT_SYMBOL (kcs_bmc_write_status); -void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val) +void kcs_bmc_update_status (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 val) { kcs_bmc->ops->io_updateb (kcs_bmc, kcs_bmc->ioreg.str, mask, val); }EXPORT_SYMBOL (kcs_bmc_update_status); -irqreturn_t kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc) +irqreturn_t kcs_bmc_handle_event (struct kcs_bmc_device *kcs_bmc) { -return kcs_bmc->client.ops->event (&kcs_bmc->client); +struct kcs_bmc_client *client; +irqreturn_t rc; + +spin_lock (&kcs_bmc->lock); + client = kcs_bmc->client; +if (client) { + rc = client->ops->event (client); + }else { +u8 status; + + status =kcs_bmc_read_status (kcs_bmc); +if (status &KCS_BMC_STR_IBF ) { +/* Ack the event by reading the data */ +kcs_bmc_read_data (kcs_bmc); + rc =IRQ_HANDLED ; + }else { + rc =IRQ_NONE ; + } + } +spin_unlock (&kcs_bmc->lock); + +return rc; }EXPORT_SYMBOL (kcs_bmc_handle_event); -int kcs_bmc_ipmi_add_device (struct kcs_bmc *kcs_bmc); -void kcs_bmc_add_device (struct kcs_bmc *kcs_bmc) +int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) +{ +int rc; + +spin_lock_irq (&kcs_bmc->lock); +if (kcs_bmc->client) { + rc = -EBUSY ; + }else { + kcs_bmc->client = client; + rc =0 ; + } +spin_unlock_irq (&kcs_bmc->lock); + +return rc; +} +EXPORT_SYMBOL (kcs_bmc_enable_device); + +void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) +{ +spin_lock_irq (&kcs_bmc->lock); +if (client == kcs_bmc->client) + kcs_bmc->client =NULL ; +spin_unlock_irq (&kcs_bmc->lock); +} +EXPORT_SYMBOL (kcs_bmc_disable_device); + +int kcs_bmc_ipmi_add_device (struct kcs_bmc_device *kcs_bmc); +void kcs_bmc_add_device (struct kcs_bmc_device *kcs_bmc) {if (kcs_bmc_ipmi_add_device (kcs_bmc)) pr_warn("Failed to add device for KCS channel %d\n",
@@ -59,8 +107,8 @@ void kcs_bmc_add_device(struct kcs_bmc *kcs_bmc) }EXPORT_SYMBOL (kcs_bmc_add_device); -int kcs_bmc_ipmi_remove_device (struct kcs_bmc *kcs_bmc); -void kcs_bmc_remove_device (struct kcs_bmc *kcs_bmc) +int kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc); +void kcs_bmc_remove_device (struct kcs_bmc_device *kcs_bmc) {if (kcs_bmc_ipmi_remove_device (kcs_bmc)) pr_warn("Failed to remove device for KCS channel %d\n",
drivers/char/ipmi/kcs_bmc.h +9 -74
106 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_aspeed.c +11 -11
@@ -61,7 +61,7 @@#define LPC_STR4 0x11Cstruct aspeed_kcs_bmc { -struct kcs_bmc kcs_bmc; +struct kcs_bmc_device kcs_bmc;struct regmap *map; };
@@ -71,12 +71,12 @@ struct aspeed_kcs_of_ops {int (*get_io_address)(struct platform_device *pdev); }; -static inline struct aspeed_kcs_bmc *to_aspeed_kcs_bmc (struct kcs_bmc *kcs_bmc) +static inline struct aspeed_kcs_bmc *to_aspeed_kcs_bmc (struct kcs_bmc_device *kcs_bmc) { return container_of(kcs_bmc, struct aspeed_kcs_bmc, kcs_bmc); } -static u8 aspeed_kcs_inb (struct kcs_bmc *kcs_bmc,u32 reg) +static u8 aspeed_kcs_inb (struct kcs_bmc_device *kcs_bmc,u32 reg) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc);u32 val =0 ;
@@ -88,7 +88,7 @@ static u8 aspeed_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)return rc ==0 ? (u8 ) val :0 ; } -static void aspeed_kcs_outb (struct kcs_bmc *kcs_bmc,u32 reg,u8 data) +static void aspeed_kcs_outb (struct kcs_bmc_device *kcs_bmc,u32 reg,u8 data) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc);int rc;
@@ -97,7 +97,7 @@ static void aspeed_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)WARN (rc !=0 ,"regmap_write() failed: %d\n" , rc); } -static void aspeed_kcs_updateb (struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 val) +static void aspeed_kcs_updateb (struct kcs_bmc_device *kcs_bmc,u32 reg,u8 mask,u8 val) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc);int rc;
@@ -119,7 +119,7 @@ static void aspeed_kcs_updateb(struct kcs_bmc *kcs_bmc, u32 reg, u8 mask, u8 val* C. KCS4 * D / C : CA4h / CA5h */ -static void aspeed_kcs_set_address (struct kcs_bmc *kcs_bmc,u16 addr) +static void aspeed_kcs_set_address (struct kcs_bmc_device *kcs_bmc,u16 addr) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc);
@@ -153,7 +153,7 @@ static void aspeed_kcs_set_address(struct kcs_bmc *kcs_bmc, u16 addr) } } -static void aspeed_kcs_enable_channel (struct kcs_bmc *kcs_bmc,bool enable) +static void aspeed_kcs_enable_channel (struct kcs_bmc_device *kcs_bmc,bool enable) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc);
@@ -228,12 +228,12 @@ static const struct kcs_bmc_device_ops aspeed_kcs_ops = {static irqreturn_t aspeed_kcs_irq (int irq,void *arg) { -struct kcs_bmc *kcs_bmc = arg; +struct kcs_bmc_device *kcs_bmc = arg;return kcs_bmc_handle_event (kcs_bmc); } -static int aspeed_kcs_config_irq (struct kcs_bmc *kcs_bmc, +static int aspeed_kcs_config_irq (struct kcs_bmc_device *kcs_bmc,struct platform_device *pdev) {struct device *dev = &pdev->dev;
@@ -333,8 +333,8 @@ static int aspeed_kcs_of_v2_get_io_address(struct platform_device *pdev)static int aspeed_kcs_probe (struct platform_device *pdev) {const struct aspeed_kcs_of_ops *ops; +struct kcs_bmc_device *kcs_bmc;struct aspeed_kcs_bmc *priv; -struct kcs_bmc *kcs_bmc;struct device_node *np;int rc, channel, addr;
@@ -393,7 +393,7 @@ static int aspeed_kcs_probe(struct platform_device *pdev)static int aspeed_kcs_remove (struct platform_device *pdev) {struct aspeed_kcs_bmc *priv =platform_get_drvdata (pdev); -struct kcs_bmc *kcs_bmc = &priv->kcs_bmc; +struct kcs_bmc_device *kcs_bmc = &priv->kcs_bmc;kcs_bmc_remove_device (kcs_bmc);
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +257 -171
673 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_client.h +15 -13
@@ -9,22 +9,24 @@#include <linux/notifier.h> #include <stdbool.h> -struct kcs_bmc ; -struct kcs_bmc_client_ops ; - -struct kcs_bmc_client { -const struct kcs_bmc_client_ops *ops; - -struct kcs_bmc *dev; -}; +#include "kcs_bmc.h" struct kcs_bmc_client_ops {irqreturn_t (*event)(struct kcs_bmc_client *client); }; -u8 kcs_bmc_read_data (struct kcs_bmc *kcs_bmc); -void kcs_bmc_write_data (struct kcs_bmc *kcs_bmc,u8 data); -u8 kcs_bmc_read_status (struct kcs_bmc *kcs_bmc); -void kcs_bmc_write_status (struct kcs_bmc *kcs_bmc,u8 data); -void kcs_bmc_update_status (struct kcs_bmc *kcs_bmc,u8 mask,u8 val); +struct kcs_bmc_client { +const struct kcs_bmc_client_ops *ops; + +struct kcs_bmc_device *dev; +}; + +int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); +void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); + +u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc); +void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data); +u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc); +void kcs_bmc_write_status (struct kcs_bmc_device *kcs_bmc,u8 data); +void kcs_bmc_update_status (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 val);#endif
drivers/char/ipmi/kcs_bmc_device.h +8 -6
@@ -4,16 +4,18 @@#ifndef __KCS_BMC_DEVICE_H__#define __KCS_BMC_DEVICE_H__ +#include <linux/irqreturn.h> +#include "kcs_bmc.h" struct kcs_bmc_device_ops { -u8 (*io_inputb)(struct kcs_bmc *kcs_bmc,u32 reg); -void (*io_outputb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 b); -void (*io_updateb)(struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 b); +u8 (*io_inputb)(struct kcs_bmc_device *kcs_bmc,u32 reg); +void (*io_outputb)(struct kcs_bmc_device *kcs_bmc,u32 reg,u8 b); +void (*io_updateb)(struct kcs_bmc_device *kcs_bmc,u32 reg,u8 mask,u8 b); }; -irqreturn_t kcs_bmc_handle_event (struct kcs_bmc *kcs_bmc); -void kcs_bmc_add_device (struct kcs_bmc *kcs_bmc); -void kcs_bmc_remove_device (struct kcs_bmc *kcs_bmc); +irqreturn_t kcs_bmc_handle_event (struct kcs_bmc_device *kcs_bmc); +void kcs_bmc_add_device (struct kcs_bmc_device *kcs_bmc); +void kcs_bmc_remove_device (struct kcs_bmc_device *kcs_bmc);#endif
drivers/char/ipmi/kcs_bmc_npcm7xx.c +10 -10
@@ -65,7 +65,7 @@ struct npcm7xx_kcs_reg { };struct npcm7xx_kcs_bmc { -struct kcs_bmc kcs_bmc; +struct kcs_bmc_device kcs_bmc;struct regmap *map;
@@ -78,12 +78,12 @@ static const struct npcm7xx_kcs_reg npcm7xx_kcs_reg_tbl[KCS_CHANNEL_MAX] = { { .sts = KCS3ST, .dob = KCS3DO, .dib = KCS3DI, .ctl = KCS3CTL, .ie = KCS3IE }, }; -static inline struct npcm7xx_kcs_bmc *to_npcm7xx_kcs_bmc (struct kcs_bmc *kcs_bmc) +static inline struct npcm7xx_kcs_bmc *to_npcm7xx_kcs_bmc (struct kcs_bmc_device *kcs_bmc) { return container_of(kcs_bmc, struct npcm7xx_kcs_bmc, kcs_bmc); } -static u8 npcm7xx_kcs_inb (struct kcs_bmc *kcs_bmc,u32 reg) +static u8 npcm7xx_kcs_inb (struct kcs_bmc_device *kcs_bmc,u32 reg) {struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);u32 val =0 ;
@@ -95,7 +95,7 @@ static u8 npcm7xx_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)return rc ==0 ? (u8 )val :0 ; } -static void npcm7xx_kcs_outb (struct kcs_bmc *kcs_bmc,u32 reg,u8 data) +static void npcm7xx_kcs_outb (struct kcs_bmc_device *kcs_bmc,u32 reg,u8 data) {struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);int rc;
@@ -104,7 +104,7 @@ static void npcm7xx_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)WARN (rc !=0 ,"regmap_write() failed: %d\n" , rc); } -static void npcm7xx_kcs_updateb (struct kcs_bmc *kcs_bmc,u32 reg,u8 mask,u8 data) +static void npcm7xx_kcs_updateb (struct kcs_bmc_device *kcs_bmc,u32 reg,u8 mask,u8 data) {struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);int rc;
@@ -113,7 +113,7 @@ static void npcm7xx_kcs_updateb(struct kcs_bmc *kcs_bmc, u32 reg, u8 mask, u8 daWARN (rc !=0 ,"regmap_update_bits() failed: %d\n" , rc); } -static void npcm7xx_kcs_enable_channel (struct kcs_bmc *kcs_bmc,bool enable) +static void npcm7xx_kcs_enable_channel (struct kcs_bmc_device *kcs_bmc,bool enable) {struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc);
@@ -126,12 +126,12 @@ static void npcm7xx_kcs_enable_channel(struct kcs_bmc *kcs_bmc, bool enable)static irqreturn_t npcm7xx_kcs_irq (int irq,void *arg) { -struct kcs_bmc *kcs_bmc = arg; +struct kcs_bmc_device *kcs_bmc = arg;return kcs_bmc_handle_event (kcs_bmc); } -static int npcm7xx_kcs_config_irq (struct kcs_bmc *kcs_bmc, +static int npcm7xx_kcs_config_irq (struct kcs_bmc_device *kcs_bmc,struct platform_device *pdev) {struct device *dev = &pdev->dev;
@@ -155,7 +155,7 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev) {struct device *dev = &pdev->dev;struct npcm7xx_kcs_bmc *priv; -struct kcs_bmc *kcs_bmc; +struct kcs_bmc_device *kcs_bmc;u32 chan;int rc;
@@ -204,7 +204,7 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev)static int npcm7xx_kcs_remove (struct platform_device *pdev) {struct npcm7xx_kcs_bmc *priv =platform_get_drvdata (pdev); -struct kcs_bmc *kcs_bmc = &priv->kcs_bmc; +struct kcs_bmc_device *kcs_bmc = &priv->kcs_bmc;kcs_bmc_remove_device (kcs_bmc);
Signature · 1 line
2.27.0
PATCH v3 8/18 ipmi: kcs_bmc: Decouple the IPMI chardev from the core
In reply to Andrew JefferyNow that we have untangled the data-structures, split the userspace interface out into its own module. Userspace interfaces and drivers are registered to the KCS BMC core to support arbitrary binding of either.
--- drivers/char/ipmi/Kconfig | 13 +++++ drivers/char/ipmi/Makefile | 3 +- drivers/char/ipmi/kcs_bmc.c | 76 ++++++++++++++++++++++++--- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 31 ++++++++--- drivers/char/ipmi/kcs_bmc_client.h | 14 +++++ 5 files changed, 122 insertions(+), 15 deletions(-)
drivers/char/ipmi/Kconfig +13 -0
@@ -124,6 +124,19 @@ config NPCM7XX_KCS_IPMI_BMC This support is also available as a module. If so, the module will be called kcs_bmc_npcm7xx. +config IPMI_KCS_BMC_CDEV_IPMI + depends on IPMI_KCS_BMC + tristate "IPMI character device interface for BMC KCS devices" + help + Provides a BMC-side character device implementing IPMI + semantics for KCS IPMI devices. + + Say YES if you wish to expose KCS devices on the BMC for IPMI + purposes. + + This support is also available as a module. The module will be + called kcs_bmc_cdev_ipmi. + config ASPEED_BT_IPMI_BMC depends on ARCH_ASPEED || COMPILE_TEST depends on REGMAP && REGMAP_MMIO && MFD_SYSCON
drivers/char/ipmi/Makefile +2 -1
@@ -22,7 +22,8 @@ obj-$(CONFIG_IPMI_SSIF) += ipmi_ssif.o obj-$(CONFIG_IPMI_POWERNV) += ipmi_powernv.o obj-$(CONFIG_IPMI_WATCHDOG) += ipmi_watchdog.o obj-$(CONFIG_IPMI_POWEROFF) += ipmi_poweroff.o -obj-$(CONFIG_IPMI_KCS_BMC) += kcs_bmc.o kcs_bmc_cdev_ipmi.o +obj-$(CONFIG_IPMI_KCS_BMC) += kcs_bmc.o +obj-$(CONFIG_IPMI_KCS_BMC_CDEV_IPMI) += kcs_bmc_cdev_ipmi.o obj-$(CONFIG_ASPEED_BT_IPMI_BMC) += bt-bmc.o obj-$(CONFIG_ASPEED_KCS_IPMI_BMC) += kcs_bmc_aspeed.o obj-$(CONFIG_NPCM7XX_KCS_IPMI_BMC) += kcs_bmc_npcm7xx.o
drivers/char/ipmi/kcs_bmc.c +68 -8
105 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +25 -6
@@ -469,8 +469,7 @@ static const struct file_operations kcs_bmc_ipmi_fops = {static DEFINE_SPINLOCK (kcs_bmc_ipmi_instances_lock);static LIST_HEAD (kcs_bmc_ipmi_instances); -int kcs_bmc_ipmi_add_device (struct kcs_bmc_device *kcs_bmc); -int kcs_bmc_ipmi_add_device (struct kcs_bmc_device *kcs_bmc) +static int kcs_bmc_ipmi_add_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_ipmi *priv;int rc;
@@ -512,10 +511,8 @@ int kcs_bmc_ipmi_add_device(struct kcs_bmc_device *kcs_bmc)return 0 ; } -EXPORT_SYMBOL (kcs_bmc_ipmi_add_device); -int kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc); -int kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc) +static int kcs_bmc_ipmi_remove_device (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_ipmi *priv =NULL , *pos;
@@ -541,7 +538,29 @@ int kcs_bmc_ipmi_remove_device(struct kcs_bmc_device *kcs_bmc)return 0 ; } -EXPORT_SYMBOL (kcs_bmc_ipmi_remove_device); + +static const struct kcs_bmc_driver_ops kcs_bmc_ipmi_driver_ops = { + .add_device = kcs_bmc_ipmi_add_device, + .remove_device = kcs_bmc_ipmi_remove_device, +}; + +static struct kcs_bmc_driver kcs_bmc_ipmi_driver = { + .ops = &kcs_bmc_ipmi_driver_ops, +}; + +static int kcs_bmc_ipmi_init (void ) +{ +kcs_bmc_register_driver (&kcs_bmc_ipmi_driver); + +return 0 ; +} +module_init (kcs_bmc_ipmi_init); + +static void kcs_bmc_ipmi_exit (void ) +{ +kcs_bmc_unregister_driver (&kcs_bmc_ipmi_driver); +} +module_exit (kcs_bmc_ipmi_exit);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );
drivers/char/ipmi/kcs_bmc_client.h +14 -0
@@ -11,6 +11,17 @@#include "kcs_bmc.h" +struct kcs_bmc_driver_ops { +int (*add_device)(struct kcs_bmc_device *kcs_bmc); +int (*remove_device)(struct kcs_bmc_device *kcs_bmc); +}; + +struct kcs_bmc_driver { +struct list_head entry; + +const struct kcs_bmc_driver_ops *ops; +}; +struct kcs_bmc_client_ops {irqreturn_t (*event)(struct kcs_bmc_client *client); };
@@ -21,6 +32,9 @@ struct kcs_bmc_client {struct kcs_bmc_device *dev; }; +void kcs_bmc_register_driver (struct kcs_bmc_driver *drv); +void kcs_bmc_unregister_driver (struct kcs_bmc_driver *drv); +int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client);void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client);
Signature · 1 line
2.27.0
PATCH v3 9/18 ipmi: kcs_bmc: Allow clients to control KCS IRQ state
In reply to Andrew JefferyAdd a mechanism for controlling whether the client associated with a KCS device will receive Input Buffer Full (IBF) and Output Buffer Empty (OBE) events. This enables an abstract implementation of poll() for KCS devices.
A wart in the implementation is that the ASPEED KCS devices don't support an OBE interrupt for the BMC. Instead we pretend it has one by polling the status register waiting for the Output Buffer Full (OBF) bit to clear, and generating an event when OBE is observed.
--- drivers/char/ipmi/kcs_bmc.c | 6 ++ drivers/char/ipmi/kcs_bmc.h | 3 + drivers/char/ipmi/kcs_bmc_aspeed.c | 150 ++++++++++++++++++---------- drivers/char/ipmi/kcs_bmc_client.h | 2 + drivers/char/ipmi/kcs_bmc_device.h | 1 + drivers/char/ipmi/kcs_bmc_npcm7xx.c | 25 ++++- 6 files changed, 130 insertions(+), 57 deletions(-)
drivers/char/ipmi/kcs_bmc.c +6 -0
@@ -176,6 +176,12 @@ void kcs_bmc_unregister_driver(struct kcs_bmc_driver *drv) }EXPORT_SYMBOL (kcs_bmc_unregister_driver); +void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events) +{ + kcs_bmc->ops->irq_mask_update (kcs_bmc, mask, events); +} +EXPORT_SYMBOL (kcs_bmc_update_event_mask); +MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" );MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );
drivers/char/ipmi/kcs_bmc.h +3 -0
@@ -8,6 +8,9 @@#include <linux/list.h> +#define KCS_BMC_EVENT_TYPE_OBE BIT(0) +#define KCS_BMC_EVENT_TYPE_IBF BIT(1) +#define KCS_BMC_STR_OBF BIT(0)#define KCS_BMC_STR_IBF BIT(1)#define KCS_BMC_STR_CMD_DAT BIT(3)
drivers/char/ipmi/kcs_bmc_aspeed.c +97 -53
193 lines not on this page · raw message
drivers/char/ipmi/kcs_bmc_client.h +2 -0
@@ -38,6 +38,8 @@ void kcs_bmc_unregister_driver(struct kcs_bmc_driver *drv);int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client);void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client); +void kcs_bmc_update_event_mask (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 events); +u8 kcs_bmc_read_data (struct kcs_bmc_device *kcs_bmc);void kcs_bmc_write_data (struct kcs_bmc_device *kcs_bmc,u8 data);u8 kcs_bmc_read_status (struct kcs_bmc_device *kcs_bmc);
drivers/char/ipmi/kcs_bmc_device.h +1 -0
@@ -9,6 +9,7 @@#include "kcs_bmc.h" struct kcs_bmc_device_ops { +void (*irq_mask_update)(struct kcs_bmc_device *kcs_bmc,u8 mask,u8 enable);u8 (*io_inputb)(struct kcs_bmc_device *kcs_bmc,u32 reg);void (*io_outputb)(struct kcs_bmc_device *kcs_bmc,u32 reg,u8 b);void (*io_updateb)(struct kcs_bmc_device *kcs_bmc,u32 reg,u8 mask,u8 b);
drivers/char/ipmi/kcs_bmc_npcm7xx.c +21 -4
@@ -38,6 +38,7 @@#define KCS2CTL 0x2A#define KCS3CTL 0x3C#define KCS_CTL_IBFIE BIT(0) +#define KCS_CTL_OBEIE BIT(0)#define KCS1IE 0x1C#define KCS2IE 0x2E
@@ -117,13 +118,23 @@ static void npcm7xx_kcs_enable_channel(struct kcs_bmc_device *kcs_bmc, bool enab {struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc); -regmap_update_bits (priv->map, priv->reg->ctl,KCS_CTL_IBFIE , - enable ?KCS_CTL_IBFIE :0 ); -regmap_update_bits (priv->map, priv->reg->ie,KCS_IE_IRQE |KCS_IE_HIRQE , enable ?KCS_IE_IRQE |KCS_IE_HIRQE :0 ); } +static void npcm7xx_kcs_irq_mask_update (struct kcs_bmc_device *kcs_bmc,u8 mask,u8 state) +{ +struct npcm7xx_kcs_bmc *priv =to_npcm7xx_kcs_bmc (kcs_bmc); + +if (KCS_BMC_EVENT_TYPE_OBE & mask) +regmap_update_bits (priv->map, priv->reg->ctl,KCS_CTL_OBEIE , + !!(KCS_BMC_EVENT_TYPE_OBE & state) *KCS_CTL_OBEIE ); + +if (KCS_BMC_EVENT_TYPE_IBF & mask) +regmap_update_bits (priv->map, priv->reg->ctl,KCS_CTL_IBFIE , + !!(state &KCS_BMC_EVENT_TYPE_IBF ) *KCS_CTL_IBFIE ); +} +static irqreturn_t npcm7xx_kcs_irq (int irq,void *arg) {struct kcs_bmc_device *kcs_bmc = arg;
@@ -146,6 +157,7 @@ static int npcm7xx_kcs_config_irq(struct kcs_bmc_device *kcs_bmc, }static const struct kcs_bmc_device_ops npcm7xx_kcs_ops = { + .irq_mask_update = npcm7xx_kcs_irq_mask_update, .io_inputb = npcm7xx_kcs_inb, .io_outputb = npcm7xx_kcs_outb, .io_updateb = npcm7xx_kcs_updateb,
@@ -186,11 +198,13 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev)platform_set_drvdata (pdev, priv); -npcm7xx_kcs_enable_channel (kcs_bmc, true); rc =npcm7xx_kcs_config_irq (kcs_bmc, pdev);if (rc)return rc; +npcm7xx_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ), +KCS_BMC_EVENT_TYPE_IBF ); +npcm7xx_kcs_enable_channel (kcs_bmc, true); pr_info("channel=%u idr=0x%x odr=0x%x str=0x%x\n", chan,
@@ -208,6 +222,9 @@ static int npcm7xx_kcs_remove(struct platform_device *pdev)kcs_bmc_remove_device (kcs_bmc); +npcm7xx_kcs_enable_channel (kcs_bmc, false); +npcm7xx_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ),0 ); +return 0 ; }
Signature · 1 line
2.27.0
RE: [PATCH linux dev-5.10 v3 09/18] ipmi: kcs_bmc: Allow clients to control KCS IRQ state
drivers/char/ipmi/kcs_bmc_npcm7xx.c line 41
Quoted 11 lines
-----Original Message-----
drivers/char/ipmi/kcs_bmc_npcm7xx.c +1
-0@@ -38,6 +38,7 @@#define KCS2CTL 0x2A#define KCS3CTL 0x3C#define KCS_CTL_IBFIE BIT(0) +#define KCS_CTL_OBEIE BIT(0)
Hi Andrew:
Appreciate all of your hard work and effort.
There is a wrong definition of kcs_bmc_npcm7xx.c.
The OBEIE bit offset in nuvoton BMC is 1.
#define KCS_CTL_OBEIE BIT(1)
Thanks,
Joseph
________________________________
________________________________
The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original sender of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such a person, please kindly reply to the sender indicating this fact and delete all copies of it from your computer and network server immediately. Your cooperation is highly appreciated. It is advised that any unauthorized use of confidential information of Nuvoton is strictly prohibited; and any information in this email irrelevant to the official business of Nuvoton shall be deemed as neither given nor endorsed by Nuvoton.
Re: [PATCH linux dev-5.10 v3 09/18] ipmi: kcs_bmc: Allow clients to control KCS IRQ state
Quoted 14 lines · On Fri, 14 May 2021, at 12:16, CS20 KWLiu wrote:
drivers/char/ipmi/kcs_bmc_npcm7xx.c line 41
Quoted 11 lines
-----Original Message-----
drivers/char/ipmi/kcs_bmc_npcm7xx.c +1
-0@@ -38,6 +38,7 @@#define KCS2CTL 0x2A#define KCS3CTL 0x3C#define KCS_CTL_IBFIE BIT(0) +#define KCS_CTL_OBEIE BIT(0)Hi Andrew:
Quoted 6 lines
Appreciate all of your hard work and effort.
There is a wrong definition of
kcs_bmc_npcm7xx.c.
The OBEIE bit offset in nuvoton BMC is 1.
#defineKCS_CTL_OBEIEBIT(1)
Ah! Good catch. Thanks Joseph.
I'll blame a copy/paste brain-fade and a lack of hardware* :)
Andrew
* Unfortunately qemu says:
$ grep -i kcs $(git ls-files | grep npcm)
hw/arm/npcm7xx.c: create_unimplemented_device("npcm7xx.kcs", 0xf0007000, 4 * KiB);PATCH v3 10/18 ipmi: kcs_bmc: Don't enforce single-open policy in the kernel
In reply to Andrew JefferySoon it will be possible for one KCS device to have multiple associated chardevs exposed to userspace (for IPMI and raw-style access). However, don't prevent userspace from:
1. Opening more than one chardev at a time, or
2. Opening the same chardev more than once.
System behaviour is undefined for both classes of multiple access, so userspace must manage itself accordingly.
The implementation delivers IBF and OBF events to the first chardev client to associate with the KCS device. An open on a related chardev cannot associate its client with the KCS device and so will not receive notification of events. However, any fd on any chardev may race their accesses to the data and status registers.
--- drivers/char/ipmi/kcs_bmc.c | 34 ++++++++++------------------- drivers/char/ipmi/kcs_bmc_aspeed.c | 3 +-- drivers/char/ipmi/kcs_bmc_npcm7xx.c | 3 +-- 3 files changed, 14 insertions(+), 26 deletions(-)
drivers/char/ipmi/kcs_bmc.c +12 -22
@@ -55,24 +55,12 @@ EXPORT_SYMBOL(kcs_bmc_update_status);irqreturn_t kcs_bmc_handle_event (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_client *client; -irqreturn_t rc; +irqreturn_t rc =IRQ_NONE ;spin_lock (&kcs_bmc->lock); client = kcs_bmc->client; -if (client) { +if (client) rc = client->ops->event (client); - }else { -u8 status; - - status =kcs_bmc_read_status (kcs_bmc); -if (status &KCS_BMC_STR_IBF ) { -/* Ack the event by reading the data */ -kcs_bmc_read_data (kcs_bmc); - rc =IRQ_HANDLED ; - }else { - rc =IRQ_NONE ; - } - }spin_unlock (&kcs_bmc->lock);return rc;
@@ -81,26 +69,28 @@ EXPORT_SYMBOL(kcs_bmc_handle_event);int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) { -int rc; -spin_lock_irq (&kcs_bmc->lock); -if (kcs_bmc->client) { - rc = -EBUSY ; - }else { +if (!kcs_bmc->client) { +u8 mask =KCS_BMC_EVENT_TYPE_IBF ; + kcs_bmc->client = client; - rc =0 ; +kcs_bmc_update_event_mask (kcs_bmc, mask, mask); }spin_unlock_irq (&kcs_bmc->lock); -return rc; +return 0 ; }EXPORT_SYMBOL (kcs_bmc_enable_device);void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) {spin_lock_irq (&kcs_bmc->lock); -if (client == kcs_bmc->client) +if (client == kcs_bmc->client) { +u8 mask =KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ; + +kcs_bmc_update_event_mask (kcs_bmc, mask,0 ); kcs_bmc->client =NULL ; + }spin_unlock_irq (&kcs_bmc->lock); }EXPORT_SYMBOL (kcs_bmc_disable_device);
drivers/char/ipmi/kcs_bmc_aspeed.c +1 -2
@@ -414,8 +414,7 @@ static int aspeed_kcs_probe(struct platform_device *pdev)platform_set_drvdata (pdev, priv); -aspeed_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ), -KCS_BMC_EVENT_TYPE_IBF ); +aspeed_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ),0 );aspeed_kcs_enable_channel (kcs_bmc, true);kcs_bmc_add_device (&priv->kcs_bmc);
drivers/char/ipmi/kcs_bmc_npcm7xx.c +1 -2
@@ -202,8 +202,7 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev)if (rc)return rc; -npcm7xx_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ), -KCS_BMC_EVENT_TYPE_IBF ); +npcm7xx_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ),0 );npcm7xx_kcs_enable_channel (kcs_bmc, true); pr_info("channel=%u idr=0x%x odr=0x%x str=0x%x\n",
Signature · 1 line
2.27.0
Re: [PATCH linux dev-5.10 v3 10/18] ipmi: kcs_bmc: Don't enforce single-open policy in the kernel
Why would we want to change this? I personally think the original mutual exclusion policy makes even more sense with multiple client types, so that the upstack programs actually know when they are being locked out. Having clients that are able to open the fd but remain broken if they don't do higher level synchronization just feels like a good way to have hard to understand behavior.
Quoted 123 lines · Andrew Jeffery
Soon it will be possible for one KCS device to have multiple associated chardevs exposed to userspace (for IPMI and raw-style access). However, don't prevent userspace from:
1. Opening more than one chardev at a time, or
2. Opening the same chardev more than once.System behaviour is undefined for both classes of multiple access, so userspace must manage itself accordingly.
The implementation delivers IBF and OBF events to the first chardev client to associate with the KCS device. An open on a related chardev cannot associate its client with the KCS device and so will not receive notification of events. However, any fd on any chardev may race their accesses to the data and status registers.
Signed-off-by Andrew Jeffery <[email protected]>--- drivers/char/ipmi/kcs_bmc.c | 34 ++++++++++------------------- drivers/char/ipmi/kcs_bmc_aspeed.c | 3 +-- drivers/char/ipmi/kcs_bmc_npcm7xx.c | 3 +-- 3 files changed, 14 insertions(+), 26 deletions(-)drivers/char/ipmi/kcs_bmc.c +12
-22@@ -55,24 +55,12 @@ EXPORT_SYMBOL(kcs_bmc_update_status);irqreturn_t kcs_bmc_handle_event (struct kcs_bmc_device *kcs_bmc) {struct kcs_bmc_client *client; -irqreturn_t rc; +irqreturn_t rc =IRQ_NONE ;spin_lock (&kcs_bmc->lock); client = kcs_bmc->client; -if (client) { +if (client) rc = client->ops->event (client); - }else { -u8 status; - - status =kcs_bmc_read_status (kcs_bmc); -if (status &KCS_BMC_STR_IBF ) { -/* Ack the event by reading the data */ -kcs_bmc_read_data (kcs_bmc); - rc =IRQ_HANDLED ; - }else { - rc =IRQ_NONE ; - } - }spin_unlock (&kcs_bmc->lock);return rc;@@ -81,26 +69,28 @@ EXPORT_SYMBOL(kcs_bmc_handle_event);int kcs_bmc_enable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) { -int rc; -spin_lock_irq (&kcs_bmc->lock); -if (kcs_bmc->client) { - rc = -EBUSY ; - }else { +if (!kcs_bmc->client) { +u8 mask =KCS_BMC_EVENT_TYPE_IBF ; + kcs_bmc->client = client; - rc =0 ; +kcs_bmc_update_event_mask (kcs_bmc, mask, mask); }spin_unlock_irq (&kcs_bmc->lock); -return rc; +return 0 ; }EXPORT_SYMBOL (kcs_bmc_enable_device);void kcs_bmc_disable_device (struct kcs_bmc_device *kcs_bmc,struct kcs_bmc_client *client) {spin_lock_irq (&kcs_bmc->lock); -if (client == kcs_bmc->client) +if (client == kcs_bmc->client) { +u8 mask =KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ; + +kcs_bmc_update_event_mask (kcs_bmc, mask,0 ); kcs_bmc->client =NULL ; + }spin_unlock_irq (&kcs_bmc->lock); }EXPORT_SYMBOL (kcs_bmc_disable_device);drivers/char/ipmi/kcs_bmc_aspeed.c +1
-2@@ -414,8 +414,7 @@ static int aspeed_kcs_probe(struct platform_device *pdev)platform_set_drvdata (pdev, priv); -aspeed_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ), -KCS_BMC_EVENT_TYPE_IBF ); +aspeed_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ),0 );aspeed_kcs_enable_channel (kcs_bmc, true);kcs_bmc_add_device (&priv->kcs_bmc);drivers/char/ipmi/kcs_bmc_npcm7xx.c +1
-3@@ -202,8 +202,7 @@ static int npcm7xx_kcs_probe(struct platform_device *pdev)if (rc)return rc; -npcm7xx_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ), -KCS_BMC_EVENT_TYPE_IBF ); +npcm7xx_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ),0 );npcm7xx_kcs_enable_channel (kcs_bmc, true); pr_info("channel=%u idr=0x%x odr=0x%x str=0x%x\n", --2.27.0
Re: [PATCH linux dev-5.10 v3 10/18] ipmi: kcs_bmc: Don't enforce single-open policy in the kernel
Quoted 6 lines · William Kennington
Why would we want to change this? I personally think the original mutual exclusion policy makes even more sense with multiple client types, so that the upstack programs actually know when they are being locked out. Having clients that are able to open the fd but remain broken if they don't do higher level synchronization just feels like a good way to have hard to understand behavior.
So there's this from Arnd:
https://lore.kernel.org/lkml/CAK8P3a2e3zNqMJSN-LAAjYmy8Gr=wjn5MMDMinxawOWcMgo7Ww@mail.gmail.com/
Quoted 9 lines · Alex Bennée
Review
Quoted 4 lines
(quoted diff) +3
-0+ /* the rpmb is single open! */ + if (test_and_set_bit(RPMB_DEV_OPEN, &rdev->status)) + return -EBUSY;open counters on device nodes are fundamentally broken, because they do not stop you from using
dup()or sharing the file descriptor across a fork. Just remove this.
Assuming Arnd's point didn't stand on its own, trying to prevent multiple opens requires two classes of checks in the kernel, where:
1. Multiple file descriptors can be associated with each device node
2. Multiple device nodes can be associated with each device.
Ensuring we don't have multiple-opens via multiple device nodes (2 above) can be done generally in kcs_bmc.c by associating the device node (client) context with the device driver instance and erroring out if an association already exists. But addressing 1. requires each client (chardev) implementation to enforce the 1-fd-per-node requirement as well, which isn't great.
If you squint, the IPMI KCS devices look like a simple UART and so we can look to the TTY layer for inspiration. TTYs suffer the same issue of hard to understand behaviour in the face of multiple opens, and define a single mechanism for avoiding both 1 and 2 above by way of userspace lock files:
https://tldp.org/HOWTO/Serial-HOWTO-13.html
The lock should be defined in terms of the underlying device as we can have multiple behaviours exposed through multiple chardevs for each device. The device can be derived by e.g:
``` # echo $(basename $(realpath /sys$(udevadm info --query=path /dev/raw-kcs4)/device)) 1e789114.kcs ```
Given that the kernel currently attempts to prevent multiple open we can assume this is something userspace isn't doing, thus it's safe to lift the restriction.
If we do have userspace competing for access then it needs to implement the locking scheme outlined above, which can be done regardless of whether the kernel supports multiple-open or not.
Andrew
Re: [PATCH linux dev-5.10 v3 10/18] ipmi: kcs_bmc: Don't enforce single-open policy in the kernel
Quoted 56 lines · Andrew Jeffery
Quoted 6 lines · William Kennington
Why would we want to change this? I personally think the original mutual exclusion policy makes even more sense with multiple client types, so that the upstack programs actually know when they are being locked out. Having clients that are able to open the fd but remain broken if they don't do higher level synchronization just feels like a good way to have hard to understand behavior.
So there's this from Arnd:
https://lore.kernel.org/lkml/CAK8P3a2e3zNqMJSN-LAAjYmy8Gr=wjn5MMDMinxawOWcMgo7Ww@mail.gmail.com/
Quoted 9 lines · Alex Bennée
Review
Quoted 4 lines
(quoted diff) +3
-0+ /* the rpmb is single open! */ + if (test_and_set_bit(RPMB_DEV_OPEN, &rdev->status)) + return -EBUSY;open counters on device nodes are fundamentally broken, because they do not stop you from using
dup()or sharing the file descriptor across a fork. Just remove this.Assuming Arnd's point didn't stand on its own, trying to prevent multiple opens requires two classes of checks in the kernel, where:
1. Multiple file descriptors can be associated with each device node
2. Multiple device nodes can be associated with each device.Ensuring we don't have multiple-opens via multiple device nodes (2 above) can be done generally in
kcs_bmc.cby associating the device node (client) context with the device driver instance and erroring out if an association already exists. But addressing 1. requires each client (chardev) implementation to enforce the 1-fd-per-node requirement as well, which isn't great.If you squint, the IPMI KCS devices look like a simple UART and so we can look to the TTY layer for inspiration. TTYs suffer the same issue of hard to understand behaviour in the face of multiple opens, and define a single mechanism for avoiding both 1 and 2 above by way of userspace lock files:
https://tldp.org/HOWTO/Serial-HOWTO-13.html
The lock should be defined in terms of the underlying device as we can have multiple behaviours exposed through multiple chardevs for each device. The device can be derived by e.g:
``` # echo $(basename $(realpath /sys$(udevadm info --query=path /dev/raw-kcs4)/device)) 1e789114.kcs ```Given that the kernel currently attempts to prevent multiple open we can assume this is something userspace isn't doing, thus it's safe to lift the restriction.
Quoted 5 lines
If we do have userspace competing for access then it needs to implement the locking scheme outlined above, which can be done regardless of whether the kernel supports multiple-open or not.
Andrew
I guess I'm kind of on the fence on this one, though maybe leaning slightly in William's direction. Certainly as Arnd's point illustrates it's never going to be a totally foolproof safeguard against userspace that's sufficiently determined to get itself into trouble (e.g. dup(), fork()), and yes, it does seem fairly analogous to the situation with ttys...but at the same time, it's not like it compromises the integrity of the kernel if it does end up happening anyway, and if as your commit message says multiple-access leads to undefined behavior, it seems like we might as well make that trap slightly harder to fall into instead of easier?
Zev
PATCH v3 11/18 ipmi: kcs_bmc: Add serio adaptor
In reply to Andrew Jefferykcs_bmc_serio acts as a bridge between the KCS drivers in the IPMI subsystem and the existing userspace interfaces available through the serio subsystem. This is useful when userspace would like to make use of the BMC KCS devices for purposes that aren't IPMI.
--- drivers/char/ipmi/Kconfig | 14 +++ drivers/char/ipmi/Makefile | 1 + drivers/char/ipmi/kcs_bmc_serio.c | 151 ++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 drivers/char/ipmi/kcs_bmc_serio.c
drivers/char/ipmi/Kconfig +14 -0
@@ -137,6 +137,20 @@ config IPMI_KCS_BMC_CDEV_IPMI This support is also available as a module. The module will be called kcs_bmc_cdev_ipmi. +config IPMI_KCS_BMC_SERIO + depends on IPMI_KCS_BMC && SERIO + tristate "SerIO adaptor for BMC KCS devices" + help + Adapts the BMC KCS device for the SerIO subsystem. This allows users + to take advantage of userspace interfaces provided by SerIO where + appropriate. + + Say YES if you wish to expose KCS devices on the BMC via SerIO + interfaces. + + This support is also available as a module. The module will be + called kcs_bmc_serio. + config ASPEED_BT_IPMI_BMC depends on ARCH_ASPEED || COMPILE_TEST depends on REGMAP && REGMAP_MMIO && MFD_SYSCON
drivers/char/ipmi/Makefile +1 -0
@@ -23,6 +23,7 @@ obj-$(CONFIG_IPMI_POWERNV) += ipmi_powernv.o obj-$(CONFIG_IPMI_WATCHDOG) += ipmi_watchdog.o obj-$(CONFIG_IPMI_POWEROFF) += ipmi_poweroff.o obj-$(CONFIG_IPMI_KCS_BMC) += kcs_bmc.o +obj-$(CONFIG_IPMI_KCS_BMC_SERIO) += kcs_bmc_serio.o obj-$(CONFIG_IPMI_KCS_BMC_CDEV_IPMI) += kcs_bmc_cdev_ipmi.o obj-$(CONFIG_ASPEED_BT_IPMI_BMC) += bt-bmc.o obj-$(CONFIG_ASPEED_KCS_IPMI_BMC) += kcs_bmc_aspeed.o
drivers/char/ipmi/kcs_bmc_serio.c new file +151 -0
151 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v3 12/18 dt-bindings: ipmi: Convert ASPEED KCS binding to schema
In reply to Andrew JefferyGiven the deprecated binding, improve the ability to detect issues in the platform devicetrees. Further, a subsequent patch will introduce a new interrupts property for specifying SerIRQ behaviour, so convert before we do any further additions.
--- .../bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml | 92 +++++++++++++++++++ .../bindings/ipmi/aspeed-kcs-bmc.txt | 33 ------- 2 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 Documentation/devicetree/bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml delete mode 100644 Documentation/devicetree/bindings/ipmi/aspeed-kcs-bmc.txt
Documentation/devicetree/bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml new file +92 -0
@@ -0,0 +1,92 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/ipmi/aspeed,ast2400-kcs-bmc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ASPEED BMC KCS Devices + +maintainers: + - Andrew Jeffery <[email protected]> + +description: | + The Aspeed BMC SoCs typically use the Keyboard-Controller-Style (KCS) + interfaces on the LPC bus for in-band IPMI communication with their host. + +properties: + compatible: + oneOf: + - description: Channel ID derived from reg + items: + enum: + - aspeed,ast2400-kcs-bmc-v2 + - aspeed,ast2500-kcs-bmc-v2 + - aspeed,ast2600-kcs-bmc + + - description: Old-style with explicit channel ID, no reg + deprecated: true + items: + enum: + - aspeed,ast2400-kcs-bmc + - aspeed,ast2500-kcs-bmc + + interrupts: + maxItems: 1 + + reg: + # maxItems: 3 + items: + - description: IDR register + - description: ODR register + - description: STR register + + aspeed,lpc-io-reg: + $ref: '/schemas/types.yaml#/definitions/uint32-array' + minItems: 1 + maxItems: 2 + description: | + The host CPU LPC IO data and status addresses for the device. For most + channels the status address is derived from the data address, but the + status address may be optionally provided. + + kcs_chan: + deprecated: true + $ref: '/schemas/types.yaml#/definitions/uint32' + description: The LPC channel number in the controller + + kcs_addr: + deprecated: true + $ref: '/schemas/types.yaml#/definitions/uint32' + description: The host CPU IO map address + +required: + - compatible + - interrupts + +additionalProperties: false + +allOf: + - if: + properties: + compatible: + contains: + enum: + - aspeed,ast2400-kcs-bmc + - aspeed,ast2500-kcs-bmc + then: + required: + - kcs_chan + - kcs_addr + else: + required: + - reg + - aspeed,lpc-io-reg + +examples: + - | + kcs3: kcs@24 { + compatible = "aspeed,ast2600-kcs-bmc"; + reg = <0x24 0x1>, <0x30 0x1>, <0x3c 0x1>; + aspeed,lpc-io-reg = <0xca2>; + interrupts = <8>; + };
Documentation/devicetree/bindings/ipmi/aspeed-kcs-bmc.txt deleted +0 -33
@@ -1,33 +0,0 @@ -# Aspeed KCS (Keyboard Controller Style) IPMI interface - -The Aspeed SOCs (AST2400 and AST2500) are commonly used as BMCs -(Baseboard Management Controllers) and the KCS interface can be -used to perform in-band IPMI communication with their host. - -## v1 -Required properties: -- compatible : should be one of - "aspeed,ast2400-kcs-bmc" - "aspeed,ast2500-kcs-bmc" -- interrupts : interrupt generated by the controller -- kcs_chan : The LPC channel number in the controller -- kcs_addr : The host CPU IO map address - -## v2 -Required properties: -- compatible : should be one of - "aspeed,ast2400-kcs-bmc-v2" - "aspeed,ast2500-kcs-bmc-v2" -- reg : The address and size of the IDR, ODR and STR registers -- interrupts : interrupt generated by the controller -- aspeed,lpc-io-reg : The host CPU LPC IO address for the device - -Example: - - kcs3: kcs@24 { - compatible = "aspeed,ast2500-kcs-bmc-v2"; - reg = <0x24 0x1>, <0x30 0x1>, <0x3c 0x1>; - aspeed,lpc-reg = <0xca2>; - interrupts = <8>; - status = "okay"; - };
Signature · 1 line
2.27.0
PATCH v3 13/18 dt-bindings: ipmi: Add optional SerIRQ property to ASPEED KCS devices
In reply to Andrew JefferyAllocating IO and IRQ resources to LPC devices is in-theory an operation for the host, however ASPEED don't appear to expose this capability outside the BMC (e.g. SuperIO). Instead, we are left with BMC-internal registers for managing these resources, so introduce a devicetree property for KCS devices to describe SerIRQ properties.
--- .../bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/ipmi/aspeed,ast2400-kcs-bmc.yaml +14 -0
@@ -49,6 +49,18 @@ properties: channels the status address is derived from the data address, but the status address may be optionally provided. + aspeed,lpc-interrupts: + $ref: "/schemas/types.yaml#/definitions/uint32-array" + minItems: 2 + maxItems: 2 + description: | + A 2-cell property expressing the LPC SerIRQ number and the interrupt + level/sense encoding (specified in the standard fashion). + + Note that the generated interrupt is issued from the BMC to the host, and + thus the target interrupt controller is not captured by the BMC's + devicetree. + kcs_chan: deprecated: true $ref: '/schemas/types.yaml#/definitions/uint32'
@@ -84,9 +96,11 @@ allOf: examples: - | + #include <dt-bindings/interrupt-controller/irq.h> kcs3: kcs@24 { compatible = "aspeed,ast2600-kcs-bmc"; reg = <0x24 0x1>, <0x30 0x1>, <0x3c 0x1>; aspeed,lpc-io-reg = <0xca2>; + aspeed,lpc-interrupts = <11 IRQ_TYPE_LEVEL_LOW>; interrupts = <8>; };
Signature · 1 line
2.27.0
PATCH v3 14/18 ipmi: kcs_bmc_aspeed: Implement KCS SerIRQ configuration
In reply to Andrew JefferyApply the SerIRQ ID and level/sense behaviours from the devicetree if provided.
--- drivers/char/ipmi/kcs_bmc_aspeed.c | 182 ++++++++++++++++++++++++++++- 1 file changed, 180 insertions(+), 2 deletions(-)
drivers/char/ipmi/kcs_bmc_aspeed.c +180 -2
@@ -9,6 +9,7 @@#include <linux/errno.h> #include <linux/interrupt.h> #include <linux/io.h> +#include <linux/irq.h> #include <linux/mfd/syscon.h> #include <linux/module.h> #include <linux/of.h>
@@ -28,6 +29,22 @@#define KCS_CHANNEL_MAX 4 +/* +* Field class descriptions +* +* LPCyE Enable LPC channel y +* IBFIEy Input Buffer Full IRQ Enable for LPC channel y +* IRQxEy Assert SerIRQ x for LPC channel y (Deprecated, use IDyIRQX, IRQXEy) +* IDyIRQX Use the specified 4-bit SerIRQ for LPC channel y +* SELyIRQX SerIRQ polarity for LPC channel y (low: 0, high: 1) +* IRQXEy Assert the SerIRQ specified in IDyIRQX for LPC channel y +*/ + +#define LPC_TYIRQX_LOW 0b00 +#define LPC_TYIRQX_HIGH 0b01 +#define LPC_TYIRQX_RSVD 0b10 +#define LPC_TYIRQX_RISING 0b11 +#define LPC_HICR0 0x000#define LPC_HICR0_LPC3E BIT(7)#define LPC_HICR0_LPC2E BIT(6)
@@ -39,6 +56,19 @@#define LPC_HICR4 0x010#define LPC_HICR4_LADR12AS BIT(7)#define LPC_HICR4_KCSENBL BIT(2) +#define LPC_SIRQCR0 0x070 +/* IRQ{12,1}E1 are deprecated as of AST2600 A3 but necessary for prior chips */ +#define LPC_SIRQCR0_IRQ12E1 BIT(1) +#define LPC_SIRQCR0_IRQ1E1 BIT(0) +#define LPC_HICR5 0x080 +#define LPC_HICR5_ID3IRQX_MASK GENMASK(23, 20) +#define LPC_HICR5_ID3IRQX_SHIFT 20 +#define LPC_HICR5_ID2IRQX_MASK GENMASK(19, 16) +#define LPC_HICR5_ID2IRQX_SHIFT 16 +#define LPC_HICR5_SEL3IRQX BIT(15) +#define LPC_HICR5_IRQXE3 BIT(14) +#define LPC_HICR5_SEL2IRQX BIT(13) +#define LPC_HICR5_IRQXE2 BIT(12)#define LPC_LADR3H 0x014#define LPC_LADR3L 0x018#define LPC_LADR12H 0x01C
@@ -55,6 +85,13 @@#define LPC_HICRB 0x100#define LPC_HICRB_IBFIF4 BIT(1)#define LPC_HICRB_LPC4E BIT(0) +#define LPC_HICRC 0x104 +#define LPC_HICRC_ID4IRQX_MASK GENMASK(7, 4) +#define LPC_HICRC_ID4IRQX_SHIFT 4 +#define LPC_HICRC_TY4IRQX_MASK GENMASK(3, 2) +#define LPC_HICRC_TY4IRQX_SHIFT 2 +#define LPC_HICRC_OBF4_AUTO_CLR BIT(1) +#define LPC_HICRC_IRQXE4 BIT(0)#define LPC_LADR4 0x110#define LPC_IDR4 0x114#define LPC_ODR4 0x118
@@ -62,11 +99,21 @@#define OBE_POLL_PERIOD (HZ / 2) +enum aspeed_kcs_irq_mode { + aspeed_kcs_irq_none, + aspeed_kcs_irq_serirq, +}; +struct aspeed_kcs_bmc {struct kcs_bmc_device kcs_bmc;struct regmap *map; +struct { +enum aspeed_kcs_irq_mode mode; +int id; + } upstream_irq; +struct {spinlock_t lock;bool remove;
@@ -103,6 +150,49 @@ static void aspeed_kcs_outb(struct kcs_bmc_device *kcs_bmc, u32 reg, u8 data) rc =regmap_write (priv->map, reg, data);WARN (rc !=0 ,"regmap_write() failed: %d\n" , rc); + +/* Trigger the upstream IRQ on ODR writes, if enabled */ + +switch (reg) { +case LPC_ODR1 : +case LPC_ODR2 : +case LPC_ODR3 : +case LPC_ODR4 : +break ; +default : +return ; + } + +if (priv->upstream_irq.mode != aspeed_kcs_irq_serirq) +return ; + +switch (kcs_bmc->channel) { +case 1 : +switch (priv->upstream_irq.id) { +case 12 : +regmap_update_bits (priv->map,LPC_SIRQCR0 ,LPC_SIRQCR0_IRQ12E1 , +LPC_SIRQCR0_IRQ12E1 ); +break ; +case 1 : +regmap_update_bits (priv->map,LPC_SIRQCR0 ,LPC_SIRQCR0_IRQ1E1 , +LPC_SIRQCR0_IRQ1E1 ); +break ; +default : +break ; + } +break ; +case 2 : +regmap_update_bits (priv->map,LPC_HICR5 ,LPC_HICR5_IRQXE2 ,LPC_HICR5_IRQXE2 ); +break ; +case 3 : +regmap_update_bits (priv->map,LPC_HICR5 ,LPC_HICR5_IRQXE3 ,LPC_HICR5_IRQXE3 ); +break ; +case 4 : +regmap_update_bits (priv->map,LPC_HICRC ,LPC_HICRC_IRQXE4 ,LPC_HICRC_IRQXE4 ); +break ; +default : +break ; + } }static void aspeed_kcs_updateb (struct kcs_bmc_device *kcs_bmc,u32 reg,u8 mask,u8 val)
@@ -161,6 +251,73 @@ static void aspeed_kcs_set_address(struct kcs_bmc_device *kcs_bmc, u16 addr) } } +static inline int aspeed_kcs_map_serirq_type (u32 dt_type) +{ +switch (dt_type) { +case IRQ_TYPE_EDGE_RISING : +return LPC_TYIRQX_RISING ; +case IRQ_TYPE_LEVEL_HIGH : +return LPC_TYIRQX_HIGH ; +case IRQ_TYPE_LEVEL_LOW : +return LPC_TYIRQX_LOW ; +default : +return -EINVAL ; + } +} + +static int aspeed_kcs_config_upstream_irq (struct aspeed_kcs_bmc *priv,u32 id,u32 dt_type) +{ +unsigned int mask, val, hw_type; + +if (id >15 ) +return -EINVAL ; + + hw_type =aspeed_kcs_map_serirq_type (dt_type); +if (hw_type <0 ) +return hw_type; + + priv->upstream_irq.mode = aspeed_kcs_irq_serirq; + priv->upstream_irq.id = id; + +switch (priv->kcs_bmc.channel) { +case 1 : +/* Needs IRQxE1 rather than (ID1IRQX, SEL1IRQX, IRQXE1) before AST2600 A3 */ +break ; +case 2 : +if (!(hw_type ==LPC_TYIRQX_LOW || hw_type ==LPC_TYIRQX_HIGH )) +return -EINVAL ; + + mask =LPC_HICR5_SEL2IRQX |LPC_HICR5_ID2IRQX_MASK ; + val = (id <<LPC_HICR5_ID2IRQX_SHIFT ); + val |= (hw_type ==LPC_TYIRQX_HIGH ) ?LPC_HICR5_SEL2IRQX :0 ; +regmap_update_bits (priv->map,LPC_HICR5 , mask, val); + +break ; +case 3 : +if (!(hw_type ==LPC_TYIRQX_LOW || hw_type ==LPC_TYIRQX_HIGH )) +return -EINVAL ; + + mask =LPC_HICR5_SEL3IRQX |LPC_HICR5_ID3IRQX_MASK ; + val = (id <<LPC_HICR5_ID3IRQX_SHIFT ); + val |= (hw_type ==LPC_TYIRQX_HIGH ) ?LPC_HICR5_SEL3IRQX :0 ; +regmap_update_bits (priv->map,LPC_HICR5 , mask, val); + +break ; +case 4 : + mask =LPC_HICRC_ID4IRQX_MASK |LPC_HICRC_TY4IRQX_MASK |LPC_HICRC_OBF4_AUTO_CLR ; + val = (id <<LPC_HICRC_ID4IRQX_SHIFT ) | (hw_type <<LPC_HICRC_TY4IRQX_SHIFT ); +regmap_update_bits (priv->map,LPC_HICRC , mask, val); +break ; +default : +dev_warn (priv->kcs_bmc.dev, +"SerIRQ configuration not supported on KCS channel %d\n" , + priv->kcs_bmc.channel); +return -EINVAL ; + } + +return 0 ; +} +static void aspeed_kcs_enable_channel (struct kcs_bmc_device *kcs_bmc,bool enable) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc);
@@ -262,7 +419,7 @@ static irqreturn_t aspeed_kcs_irq(int irq, void *arg)return kcs_bmc_handle_event (kcs_bmc); } -static int aspeed_kcs_config_irq (struct kcs_bmc_device *kcs_bmc, +static int aspeed_kcs_config_downstream_irq (struct kcs_bmc_device *kcs_bmc,struct platform_device *pdev) {struct device *dev = &pdev->dev;
@@ -366,6 +523,8 @@ static int aspeed_kcs_probe(struct platform_device *pdev)struct aspeed_kcs_bmc *priv;struct device_node *np;int rc, channel, addr; +bool have_upstream_irq; +u32 upstream_irq[2 ]; np = pdev->dev.of_node->parent; if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
@@ -374,6 +533,7 @@ static int aspeed_kcs_probe(struct platform_device *pdev)dev_err (&pdev->dev,"unsupported LPC device binding\n" );return -ENODEV ; } + ops =of_device_get_match_data (&pdev->dev);if (!ops)return -EINVAL ;
@@ -386,6 +546,13 @@ static int aspeed_kcs_probe(struct platform_device *pdev)if (addr <0 )return addr; + np = pdev->dev.of_node; + rc =of_property_read_u32_array (np,"aspeed,lpc-interrupts" , upstream_irq,2 ); +if ((rc && rc != -EINVAL )) +return -EINVAL ; + + have_upstream_irq = !rc; + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ;
@@ -408,10 +575,20 @@ static int aspeed_kcs_probe(struct platform_device *pdev)aspeed_kcs_set_address (kcs_bmc, addr); - rc =aspeed_kcs_config_irq (kcs_bmc, pdev); +/* Host to BMC IRQ */ + rc =aspeed_kcs_config_downstream_irq (kcs_bmc, pdev);if (rc)return rc; +/* BMC to Host IRQ */ +if (have_upstream_irq) { + rc =aspeed_kcs_config_upstream_irq (priv, upstream_irq[0 ], upstream_irq[1 ]); +if (rc <0 ) +return rc; + }else { + priv->upstream_irq.mode = aspeed_kcs_irq_none; + } +platform_set_drvdata (pdev, priv);aspeed_kcs_irq_mask_update (kcs_bmc, (KCS_BMC_EVENT_TYPE_IBF |KCS_BMC_EVENT_TYPE_OBE ),0 );
@@ -475,4 +652,5 @@ module_platform_driver(ast_kcs_bmc_driver);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Haiyue Wang <[email protected]>" ); +MODULE_AUTHOR ("Andrew Jeffery <[email protected]>" );MODULE_DESCRIPTION ("Aspeed device interface to the KCS BMC device" );
Signature · 1 line
2.27.0
PATCH v3 15/18 ipmi: kcs_bmc_aspeed: Fix IBFIE typo from datasheet
In reply to Andrew JefferyInput Buffer Full Interrupt Enable (IBFIE) is typoed as IBFIF for some registers in the datasheet. Fix the driver to use the sensible acronym.
--- drivers/char/ipmi/kcs_bmc_aspeed.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/char/ipmi/kcs_bmc_aspeed.c +12 -12
@@ -50,9 +50,9 @@#define LPC_HICR0_LPC2E BIT(6)#define LPC_HICR0_LPC1E BIT(5)#define LPC_HICR2 0x008 -#define LPC_HICR2_IBFIF3 BIT(3) -#define LPC_HICR2_IBFIF2 BIT(2) -#define LPC_HICR2_IBFIF1 BIT(1) +#define LPC_HICR2_IBFIE3 BIT(3) +#define LPC_HICR2_IBFIE2 BIT(2) +#define LPC_HICR2_IBFIE1 BIT(1)#define LPC_HICR4 0x010#define LPC_HICR4_LADR12AS BIT(7)#define LPC_HICR4_KCSENBL BIT(2)
@@ -83,7 +83,7 @@#define LPC_STR2 0x040#define LPC_STR3 0x044#define LPC_HICRB 0x100 -#define LPC_HICRB_IBFIF4 BIT(1) +#define LPC_HICRB_IBFIE4 BIT(1)#define LPC_HICRB_LPC4E BIT(0)#define LPC_HICRC 0x104#define LPC_HICRC_ID4IRQX_MASK GENMASK(7, 4)
@@ -383,20 +383,20 @@ static void aspeed_kcs_irq_mask_update(struct kcs_bmc_device *kcs_bmc, u8 mask,switch (kcs_bmc->channel) {case 1 : -regmap_update_bits (priv->map,LPC_HICR2 ,LPC_HICR2_IBFIF1 , - enable *LPC_HICR2_IBFIF1 ); +regmap_update_bits (priv->map,LPC_HICR2 ,LPC_HICR2_IBFIE1 , + enable *LPC_HICR2_IBFIE1 );return ;case 2 : -regmap_update_bits (priv->map,LPC_HICR2 ,LPC_HICR2_IBFIF2 , - enable *LPC_HICR2_IBFIF2 ); +regmap_update_bits (priv->map,LPC_HICR2 ,LPC_HICR2_IBFIE2 , + enable *LPC_HICR2_IBFIE2 );return ;case 3 : -regmap_update_bits (priv->map,LPC_HICR2 ,LPC_HICR2_IBFIF3 , - enable *LPC_HICR2_IBFIF3 ); +regmap_update_bits (priv->map,LPC_HICR2 ,LPC_HICR2_IBFIE3 , + enable *LPC_HICR2_IBFIE3 );return ;case 4 : -regmap_update_bits (priv->map,LPC_HICRB ,LPC_HICRB_IBFIF4 , - enable *LPC_HICRB_IBFIF4 ); +regmap_update_bits (priv->map,LPC_HICRB ,LPC_HICRB_IBFIE4 , + enable *LPC_HICRB_IBFIE4 );return ;default :pr_warn ("%s: Unsupported channel: %d" , __func__, kcs_bmc->channel);
Signature · 1 line
2.27.0
PATCH v3 16/18 ipmi: kcs_bmc_aspeed: Optionally apply status address
In reply to Andrew JefferySome Aspeed KCS devices can derive the status register address from the address of the data register. As such, the address of the status register can be implicit in the configuration if desired. On the other hand, sometimes address schemes might be requested that are incompatible with the default addressing scheme. Allow these requests where possible if the devicetree specifies the status register address.
--- drivers/char/ipmi/kcs_bmc_aspeed.c | 110 ++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 32 deletions(-)
drivers/char/ipmi/kcs_bmc_aspeed.c +78 -32
@@ -83,6 +83,8 @@#define LPC_STR2 0x040#define LPC_STR3 0x044#define LPC_HICRB 0x100 +#define LPC_HICRB_EN16LADR2 BIT(5) +#define LPC_HICRB_EN16LADR1 BIT(4)#define LPC_HICRB_IBFIE4 BIT(1)#define LPC_HICRB_LPC4E BIT(0)#define LPC_HICRC 0x104
@@ -96,6 +98,11 @@#define LPC_IDR4 0x114#define LPC_ODR4 0x118#define LPC_STR4 0x11C +#define LPC_LSADR12 0x120 +#define LPC_LSADR12_LSADR2_MASK GENMASK(31, 16) +#define LPC_LSADR12_LSADR2_SHIFT 16 +#define LPC_LSADR12_LSADR1_MASK GENMASK(15, 0) +#define LPC_LSADR12_LSADR1_SHIFT 0#define OBE_POLL_PERIOD (HZ / 2)
@@ -123,7 +130,7 @@ struct aspeed_kcs_bmc {struct aspeed_kcs_of_ops {int (*get_channel)(struct platform_device *pdev); -int (*get_io_address)(struct platform_device *pdev); +int (*get_io_address)(struct platform_device *pdev,u32 addrs[2 ]); };static inline struct aspeed_kcs_bmc *to_aspeed_kcs_bmc (struct kcs_bmc_device *kcs_bmc)
@@ -217,38 +224,64 @@ static void aspeed_kcs_updateb(struct kcs_bmc_device *kcs_bmc, u32 reg, u8 mask,* C. KCS4 * D / C : CA4h / CA5h */ -static void aspeed_kcs_set_address (struct kcs_bmc_device *kcs_bmc,u16 addr) +static int aspeed_kcs_set_address (struct kcs_bmc_device *kcs_bmc,u32 addrs[2 ],int nr_addrs) {struct aspeed_kcs_bmc *priv =to_aspeed_kcs_bmc (kcs_bmc); -switch (kcs_bmc->channel) { +if (WARN_ON (nr_addrs <1 || nr_addrs >2 )) +return -EINVAL ; + +switch (priv->kcs_bmc.channel) {case 1 : -regmap_update_bits (priv->map,LPC_HICR4 , -LPC_HICR4_LADR12AS ,0 ); -regmap_write (priv->map,LPC_LADR12H , addr >>8 ); -regmap_write (priv->map,LPC_LADR12L , addr &0xFF ); +regmap_update_bits (priv->map,LPC_HICR4 ,LPC_HICR4_LADR12AS ,0 ); +regmap_write (priv->map,LPC_LADR12H , addrs[0 ] >>8 ); +regmap_write (priv->map,LPC_LADR12L , addrs[0 ] &0xFF ); +if (nr_addrs ==2 ) { +regmap_update_bits (priv->map,LPC_LSADR12 ,LPC_LSADR12_LSADR1_MASK , + addrs[1 ] <<LPC_LSADR12_LSADR1_SHIFT ); + +regmap_update_bits (priv->map,LPC_HICRB ,LPC_HICRB_EN16LADR1 , +LPC_HICRB_EN16LADR1 ); + }break ;case 2 : -regmap_update_bits (priv->map,LPC_HICR4 , -LPC_HICR4_LADR12AS ,LPC_HICR4_LADR12AS ); -regmap_write (priv->map,LPC_LADR12H , addr >>8 ); -regmap_write (priv->map,LPC_LADR12L , addr &0xFF ); +regmap_update_bits (priv->map,LPC_HICR4 ,LPC_HICR4_LADR12AS ,LPC_HICR4_LADR12AS ); +regmap_write (priv->map,LPC_LADR12H , addrs[0 ] >>8 ); +regmap_write (priv->map,LPC_LADR12L , addrs[0 ] &0xFF ); +if (nr_addrs ==2 ) { +regmap_update_bits (priv->map,LPC_LSADR12 ,LPC_LSADR12_LSADR2_MASK , + addrs[1 ] <<LPC_LSADR12_LSADR2_SHIFT ); + +regmap_update_bits (priv->map,LPC_HICRB ,LPC_HICRB_EN16LADR2 , +LPC_HICRB_EN16LADR2 ); + }break ;case 3 : -regmap_write (priv->map,LPC_LADR3H , addr >>8 ); -regmap_write (priv->map,LPC_LADR3L , addr &0xFF ); +if (nr_addrs ==2 ) { +dev_err (priv->kcs_bmc.dev, +"Channel 3 only supports inferred status IO address\n" ); +return -EINVAL ; + } + +regmap_write (priv->map,LPC_LADR3H , addrs[0 ] >>8 ); +regmap_write (priv->map,LPC_LADR3L , addrs[0 ] &0xFF );break ;case 4 : -regmap_write (priv->map,LPC_LADR4 , ((addr +1 ) <<16 ) | - addr); +if (nr_addrs ==1 ) +regmap_write (priv->map,LPC_LADR4 , ((addrs[0 ] +1 ) <<16 ) | addrs[0 ]); +else +regmap_write (priv->map,LPC_LADR4 , (addrs[1 ] <<16 ) | addrs[0 ]); +break ;default : -break ; +return -EINVAL ; } + +return 0 ; }static inline int aspeed_kcs_map_serirq_type (u32 dt_type)
@@ -457,18 +490,18 @@ static int aspeed_kcs_of_v1_get_channel(struct platform_device *pdev)return channel; } -static int aspeed_kcs_of_v1_get_io_address (struct platform_device *pdev) +static int +aspeed_kcs_of_v1_get_io_address (struct platform_device *pdev,u32 addrs[2 ]) { -u32 slave;int rc; - rc =of_property_read_u32 (pdev->dev.of_node,"kcs_addr" , &slave); -if (rc || slave >0xffff ) { + rc =of_property_read_u32 (pdev->dev.of_node,"kcs_addr" , addrs); +if (rc || addrs[0 ] >0xffff ) {dev_err (&pdev->dev,"no valid 'kcs_addr' configured\n" );return -EINVAL ; } -return slave; +return 1 ; }static int aspeed_kcs_of_v2_get_channel (struct platform_device *pdev)
@@ -504,16 +537,24 @@ static int aspeed_kcs_of_v2_get_channel(struct platform_device *pdev)return -EINVAL ; } -static int aspeed_kcs_of_v2_get_io_address (struct platform_device *pdev) +static int +aspeed_kcs_of_v2_get_io_address (struct platform_device *pdev,u32 addrs[2 ]) { -uint32_t slave;int rc; - rc =of_property_read_u32 (pdev->dev.of_node,"aspeed,lpc-io-reg" , &slave); -if (rc || slave >0xffff ) + rc =of_property_read_variable_u32_array (pdev->dev.of_node, +"aspeed,lpc-io-reg" , + addrs,1 ,2 ); +if (rc <0 ) +return rc; + +if (addrs[0 ] >0xffff ) +return -EINVAL ; + +if (rc ==2 && addrs[1 ] >0xffff )return -EINVAL ; -return slave; +return rc; }static int aspeed_kcs_probe (struct platform_device *pdev)
@@ -522,9 +563,11 @@ static int aspeed_kcs_probe(struct platform_device *pdev)struct kcs_bmc_device *kcs_bmc;struct aspeed_kcs_bmc *priv;struct device_node *np; -int rc, channel, addr;bool have_upstream_irq;u32 upstream_irq[2 ]; +int rc, channel; +int nr_addrs; +u32 addrs[2 ]; np = pdev->dev.of_node->parent; if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
@@ -542,9 +585,9 @@ static int aspeed_kcs_probe(struct platform_device *pdev)if (channel <0 )return channel; - addr = ops->get_io_address (pdev); -if (addr <0 ) -return addr; + nr_addrs = ops->get_io_address (pdev, addrs); +if (nr_addrs <0 ) +return nr_addrs; np = pdev->dev.of_node; rc =of_property_read_u32_array (np,"aspeed,lpc-interrupts" , upstream_irq,2 );
@@ -573,7 +616,9 @@ static int aspeed_kcs_probe(struct platform_device *pdev) priv->obe.remove = false;timer_setup (&priv->obe.timer, aspeed_kcs_check_obe,0 ); -aspeed_kcs_set_address (kcs_bmc, addr); + rc =aspeed_kcs_set_address (kcs_bmc, addrs, nr_addrs); +if (rc) +return rc;/* Host to BMC IRQ */ rc =aspeed_kcs_config_downstream_irq (kcs_bmc, pdev);
@@ -596,7 +641,8 @@ static int aspeed_kcs_probe(struct platform_device *pdev)kcs_bmc_add_device (&priv->kcs_bmc); -dev_info (&pdev->dev,"Initialised channel %d at 0x%x\n" , kcs_bmc->channel, addr); +dev_info (&pdev->dev,"Initialised channel %d at 0x%x\n" , + kcs_bmc->channel, addrs[0 ]);return 0 ; }
Signature · 1 line
2.27.0
PATCH v3 17/18 ARM: dts: rainier: Enable KCS channel 2
In reply to Andrew JefferyKCS channel 2 will be used to attempt data capture if the host determines the BMC is unresponsive.
--- arch/arm/boot/dts/aspeed-bmc-ibm-rainier.dts | 5 +++++ 1 file changed, 5 insertions(+)
arch/arm/boot/dts/aspeed-bmc-ibm-rainier.dts +5 -0
@@ -1784,3 +1784,8 @@ &xdma { status = "okay"; memory-region = <&vga_memory>; }; + +&kcs2 { + status = "okay"; + aspeed,lpc-io-reg = <0xca8 0xcac>; +};
Signature · 1 line
2.27.0
PATCH v3 18/18 ipmi: kcs_bmc: Add a "raw" character device interface
In reply to Andrew JefferyThe existing IPMI chardev encodes IPMI behaviours as the name suggests. However, KCS devices are useful beyond IPMI (or keyboards), as they provide a means to generate IRQs and exchange arbitrary data between a BMC and its host system.
Implement a "raw" KCS character device that exposes the IDR, ODR and STR registers to userspace via read() and write() implemented on a character device:
+--------+--------+---------+ | Offset | read() | write() | +--------+--------+---------+ | 0 | IDR | ODR | +--------+--------+---------+ | 1 | STR | STR | +--------+--------+---------+
This interface allows userspace to implement arbitrary (though somewhat inefficient) protocols for exchanging information between a BMC and host firmware. Conceptually the KCS interface can be used as an out-of-band mechanism for interrupt-signaled control messages while bulk data transfers occur over more appropriate interfaces between the BMC and the host (which may lack their own interrupt mechanism, e.g. LPC FW cycles).
poll() is provided, which will wait for IBF or OBE conditions for data reads and writes respectively. Reads of STR on its own never blocks, though accessing both offsets in the one system call may block if the data registers are not ready.
--- Documentation/ABI/testing/dev-raw-kcs | 25 ++ drivers/char/ipmi/Kconfig | 17 + drivers/char/ipmi/Makefile | 1 + drivers/char/ipmi/kcs_bmc_cdev_raw.c | 440 ++++++++++++++++++++++++++ 4 files changed, 483 insertions(+) create mode 100644 Documentation/ABI/testing/dev-raw-kcs create mode 100644 drivers/char/ipmi/kcs_bmc_cdev_raw.c
Documentation/ABI/testing/dev-raw-kcs new file +25 -0
@@ -0,0 +1,25 @@ +What: /dev/raw-kcs* +Date: 2021-02-15 +KernelVersion: 5.13 +Contact: [email protected] +Contact: [email protected] +Contact: Andrew Jeffery <[email protected]> +Description: ``/dev/raw-kcs*`` exposes to userspace the data and + status registers of Keyboard-Controller-Style (KCS) IPMI + interfaces via read() and write() syscalls. Direct + exposure of the data and status registers enables + inefficient but arbitrary protocols to be implemented + over the device. A typical approach is to use KCS + devices for out-of-band signalling for bulk data + transfers over other interfaces between a Baseboard + Management Controller and its host. + + +--------+--------+---------+ + | Offset | read() | write() | + +--------+--------+---------+ + | 0 | IDR | ODR | + +--------+--------+---------+ + | 1 | STR | STR | + +--------+--------+---------+ + +Users: libmctp: https://github.com/openbmc/libmctp
drivers/char/ipmi/Kconfig +17 -0
@@ -151,6 +151,23 @@ config IPMI_KCS_BMC_SERIO This support is also available as a module. The module will be called kcs_bmc_serio. +config IPMI_KCS_BMC_CDEV_RAW + depends on IPMI_KCS_BMC + tristate "Raw character device interface for BMC KCS devices" + help + Provides a BMC-side character device directly exposing the + data and status registers of a KCS device to userspace. While + KCS devices are commonly used to implement IPMI message + passing, they provide a general interface for exchange of + interrupts, data and status information between the BMC and + its host. + + Say YES if you wish to use the KCS devices to implement + protocols that are not IPMI. + + This support is also available as a module. The module will be + called kcs_bmc_cdev_raw. + config ASPEED_BT_IPMI_BMC depends on ARCH_ASPEED || COMPILE_TEST depends on REGMAP && REGMAP_MMIO && MFD_SYSCON
drivers/char/ipmi/Makefile +1 -0
@@ -25,6 +25,7 @@ obj-$(CONFIG_IPMI_POWEROFF) += ipmi_poweroff.o obj-$(CONFIG_IPMI_KCS_BMC) += kcs_bmc.o obj-$(CONFIG_IPMI_KCS_BMC_SERIO) += kcs_bmc_serio.o obj-$(CONFIG_IPMI_KCS_BMC_CDEV_IPMI) += kcs_bmc_cdev_ipmi.o +obj-$(CONFIG_IPMI_KCS_BMC_CDEV_RAW) += kcs_bmc_cdev_raw.o obj-$(CONFIG_ASPEED_BT_IPMI_BMC) += bt-bmc.o obj-$(CONFIG_ASPEED_KCS_IPMI_BMC) += kcs_bmc_aspeed.o obj-$(CONFIG_NPCM7XX_KCS_IPMI_BMC) += kcs_bmc_npcm7xx.o
drivers/char/ipmi/kcs_bmc_cdev_raw.c new file +440 -0
440 lines not on this page · raw message
Signature · 1 line
2.27.0