Skip to content

Commit

Permalink
ipmi: kcs_bmc: Strip private client data from struct kcs_bmc
Browse files Browse the repository at this point in the history
Move 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`.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
  • Loading branch information
amboar authored and intel-lab-lkp committed Feb 19, 2021
1 parent 4185e7a commit abe0bf8
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 292 deletions.
68 changes: 57 additions & 11 deletions drivers/char/ipmi/kcs_bmc.c
Expand Up @@ -4,6 +4,7 @@
* Copyright (c) 2021, IBM Corp.
*/

#include <linux/device.h>
#include <linux/module.h>

#include "kcs_bmc.h"
Expand All @@ -14,51 +15,96 @@

/* 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);

int kcs_bmc_handle_event(struct kcs_bmc *kcs_bmc)
int kcs_bmc_handle_event(struct kcs_bmc_device *kcs_bmc)
{
return kcs_bmc->client.ops->event(&kcs_bmc->client);
struct kcs_bmc_client *client;
int 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 = KCS_BMC_EVENT_HANDLED;
} else {
rc = KCS_BMC_EVENT_NONE;
}
}
spin_unlock(&kcs_bmc->lock);

return rc;
}
EXPORT_SYMBOL(kcs_bmc_handle_event);

int kcs_bmc_ipmi_attach_cdev(struct kcs_bmc *kcs_bmc);
int 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_add_device(struct kcs_bmc_device *kcs_bmc)
{
return kcs_bmc_ipmi_attach_cdev(kcs_bmc);
}
EXPORT_SYMBOL(kcs_bmc_add_device);

int kcs_bmc_ipmi_detach_cdev(struct kcs_bmc *kcs_bmc);
int kcs_bmc_remove_device(struct kcs_bmc *kcs_bmc)
int kcs_bmc_remove_device(struct kcs_bmc_device *kcs_bmc)
{
return kcs_bmc_ipmi_detach_cdev(kcs_bmc);
}
Expand Down
86 changes: 12 additions & 74 deletions drivers/char/ipmi/kcs_bmc.h
Expand Up @@ -6,9 +6,7 @@
#ifndef __KCS_BMC_H__
#define __KCS_BMC_H__

#include <linux/miscdevice.h>

#include "kcs_bmc_client.h"
#include <linux/list.h>

#define KCS_BMC_EVENT_NONE 0
#define KCS_BMC_EVENT_HANDLED 1
Expand All @@ -17,53 +15,6 @@
#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.
* KCS_PHASE_WRITE_START:
* BMC is receiving a WRITE_START command from system software.
* KCS_PHASE_WRITE_DATA:
* BMC is receiving a data byte from system software.
* KCS_PHASE_WRITE_END_CMD:
* BMC is waiting a last data byte from system software.
* KCS_PHASE_WRITE_DONE:
* BMC has received the whole request from system software.
* KCS_PHASE_WAIT_READ:
* BMC is waiting the response from the upper IPMI service.
* KCS_PHASE_READ:
* BMC is transferring the response to system software.
* KCS_PHASE_ABORT_ERROR1:
* BMC is waiting error status request from system software.
* KCS_PHASE_ABORT_ERROR2:
* BMC is waiting for idle status afer error from system software.
* KCS_PHASE_ERROR:
* BMC has detected a protocol violation at the interface level.
*/
enum kcs_phases {
KCS_PHASE_IDLE,

KCS_PHASE_WRITE_START,
KCS_PHASE_WRITE_DATA,
KCS_PHASE_WRITE_END_CMD,
KCS_PHASE_WRITE_DONE,

KCS_PHASE_WAIT_READ,
KCS_PHASE_READ,

KCS_PHASE_ABORT_ERROR1,
KCS_PHASE_ABORT_ERROR2,
KCS_PHASE_ERROR
};

/* IPMI 2.0 - Table 9-4, KCS Interface Status Codes */
enum kcs_errors {
KCS_NO_ERROR = 0x00,
KCS_ABORTED_BY_COMMAND = 0x01,
KCS_ILLEGAL_CONTROL_CODE = 0x02,
KCS_LENGTH_ERROR = 0x06,
KCS_UNSPECIFIED_ERROR = 0xFF
};

/* IPMI 2.0 - 9.5, KCS Interface Registers
* @idr: Input Data Register
* @odr: Output Data Register
Expand All @@ -76,36 +27,23 @@ struct kcs_ioreg {
};

struct kcs_bmc_device_ops;
struct kcs_bmc_client;

struct kcs_bmc {
struct device *dev;

const struct kcs_bmc_device_ops *ops;

struct kcs_bmc_client client;

spinlock_t lock;
struct kcs_bmc_device {
struct list_head entry;

struct device *dev;
u32 channel;
int running;

struct kcs_ioreg ioreg;

enum kcs_phases phase;
enum kcs_errors error;

wait_queue_head_t queue;
bool data_in_avail;
int data_in_idx;
u8 *data_in;

int data_out_idx;
int data_out_len;
u8 *data_out;

struct mutex mutex;
u8 *kbuffer;
const struct kcs_bmc_device_ops *ops;

struct miscdevice miscdev;
spinlock_t lock;
struct kcs_bmc_client *client;
};

/* Temporary exports while refactoring */
int kcs_bmc_ipmi_attach_cdev(struct kcs_bmc_device *kcs_bmc);
int kcs_bmc_ipmi_detach_cdev(struct kcs_bmc_device *kcs_bmc);
#endif /* __KCS_BMC_H__ */
22 changes: 11 additions & 11 deletions drivers/char/ipmi/kcs_bmc_aspeed.c
Expand Up @@ -61,7 +61,7 @@
#define LPC_STR4 0x11C

struct aspeed_kcs_bmc {
struct kcs_bmc kcs_bmc;
struct kcs_bmc_device kcs_bmc;

struct regmap *map;
};
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -228,7 +228,7 @@ 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;
int rc;

rc = kcs_bmc_handle_event(kcs_bmc);
Expand All @@ -238,7 +238,7 @@ static irqreturn_t aspeed_kcs_irq(int irq, void *arg)
return rc == KCS_BMC_EVENT_HANDLED ? IRQ_HANDLED : IRQ_NONE;
}

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;
Expand Down Expand Up @@ -338,8 +338,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;

Expand Down Expand Up @@ -400,7 +400,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);

Expand Down

0 comments on commit abe0bf8

Please sign in to comment.