2013-04-19 19:02:59

by Tomas Winkler

[permalink] [raw]
Subject: [char-misc-next 1/3] mei: revamp mei_irq_read_client_message function

1. Rename the function and change parameters order,
so that first parameter is mei_device
2. Simplify the function code flow
3. Rename helper functions to more self descriptive names
4. Use helpers common functions where possible

Signed-off-by: Tomas Winkler <[email protected]>
---
drivers/misc/mei/interrupt.c | 124 +++++++++++++++++++++++--------------------
1 file changed, 66 insertions(+), 58 deletions(-)

diff --git a/drivers/misc/mei/interrupt.c b/drivers/misc/mei/interrupt.c
index 8e4db3d..9bf64c0 100644
--- a/drivers/misc/mei/interrupt.c
+++ b/drivers/misc/mei/interrupt.c
@@ -82,80 +82,90 @@ void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
}
}
EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
+
/**
- * _mei_irq_thread_state_ok - checks if mei header matches file private data
+ * mei_cl_hbm_equal - check if hbm is addressed to the client
*
- * @cl: private data of the file object
+ * @cl: host client
* @mei_hdr: header of mei client message
*
- * returns !=0 if matches, 0 if no match.
+ * returns true if matches, false otherwise
+ */
+static inline int mei_cl_hbm_equal(struct mei_cl *cl,
+ struct mei_msg_hdr *mei_hdr)
+{
+ return cl->host_client_id == mei_hdr->host_addr &&
+ cl->me_client_id == mei_hdr->me_addr;
+}
+/**
+ * mei_cl_is_reading - checks if the client
+ is the one to read this message
+ *
+ * @cl: mei client
+ * @mei_hdr: header of mei message
+ *
+ * returns true on match and false otherwise
*/
-static int _mei_irq_thread_state_ok(struct mei_cl *cl,
- struct mei_msg_hdr *mei_hdr)
+static bool mei_cl_is_reading(struct mei_cl *cl, struct mei_msg_hdr *mei_hdr)
{
- return (cl->host_client_id == mei_hdr->host_addr &&
- cl->me_client_id == mei_hdr->me_addr &&
+ return mei_cl_hbm_equal(cl, mei_hdr) &&
cl->state == MEI_FILE_CONNECTED &&
- MEI_READ_COMPLETE != cl->reading_state);
+ cl->reading_state != MEI_READ_COMPLETE;
}

/**
- * mei_irq_thread_read_client_message - bottom half read routine after ISR to
- * handle the read mei client message data processing.
+ * mei_irq_read_client_message - process client message
*
- * @complete_list: An instance of our list structure
* @dev: the device structure
* @mei_hdr: header of mei client message
+ * @complete_list: An instance of our list structure
*
* returns 0 on success, <0 on failure.
*/
-static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
- struct mei_device *dev,
- struct mei_msg_hdr *mei_hdr)
+static int mei_cl_irq_read_msg(struct mei_device *dev,
+ struct mei_msg_hdr *mei_hdr,
+ struct mei_cl_cb *complete_list)
{
struct mei_cl *cl;
- struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
+ struct mei_cl_cb *cb, *next;
unsigned char *buffer = NULL;

- dev_dbg(&dev->pdev->dev, "start client msg\n");
- if (list_empty(&dev->read_list.list))
- goto quit;
-
- list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
- cl = cb_pos->cl;
- if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
- cl->reading_state = MEI_READING;
- buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
-
- if (cb_pos->response_buffer.size <
- mei_hdr->length + cb_pos->buf_idx) {
- dev_dbg(&dev->pdev->dev, "message overflow.\n");
- list_del(&cb_pos->list);
- return -ENOMEM;
- }
- if (buffer)
- mei_read_slots(dev, buffer, mei_hdr->length);
-
- cb_pos->buf_idx += mei_hdr->length;
- if (mei_hdr->msg_complete) {
- cl->status = 0;
- list_del(&cb_pos->list);
- dev_dbg(&dev->pdev->dev,
- "completed read H cl = %d, ME cl = %d, length = %lu\n",
- cl->host_client_id,
- cl->me_client_id,
- cb_pos->buf_idx);
-
- list_add_tail(&cb_pos->list,
- &complete_list->list);
- }
+ list_for_each_entry_safe(cb, next, &dev->read_list.list, list) {
+ cl = cb->cl;
+ if (!cl || !mei_cl_is_reading(cl, mei_hdr))
+ continue;

- break;
+ cl->reading_state = MEI_READING;
+
+ if (cb->response_buffer.size == 0 ||
+ cb->response_buffer.data == NULL) {
+ dev_err(&dev->pdev->dev, "response buffer is not allocated.\n");
+ list_del(&cb->list);
+ return -ENOMEM;
+ }
+
+ if (cb->response_buffer.size < mei_hdr->length + cb->buf_idx) {
+ dev_warn(&dev->pdev->dev, "message overflow.\n");
+ list_del(&cb->list);
+ return -ENOMEM;
}

+ buffer = cb->response_buffer.data + cb->buf_idx;
+ mei_read_slots(dev, buffer, mei_hdr->length);
+
+ cb->buf_idx += mei_hdr->length;
+ if (mei_hdr->msg_complete) {
+ cl->status = 0;
+ list_del(&cb->list);
+ dev_dbg(&dev->pdev->dev, "completed read H cl = %d, ME cl = %d, length = %lu\n",
+ cl->host_client_id,
+ cl->me_client_id,
+ cb->buf_idx);
+ list_add_tail(&cb->list, &complete_list->list);
+ }
+ break;
}

-quit:
dev_dbg(&dev->pdev->dev, "message read\n");
if (!buffer) {
mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
@@ -386,8 +396,7 @@ int mei_irq_read_handler(struct mei_device *dev,
" client = %d, ME client = %d\n",
cl_pos->host_client_id,
cl_pos->me_client_id);
- if (cl_pos->host_client_id == mei_hdr->host_addr &&
- cl_pos->me_client_id == mei_hdr->me_addr)
+ if (mei_cl_hbm_equal(cl_pos, mei_hdr))
break;
}

@@ -398,7 +407,7 @@ int mei_irq_read_handler(struct mei_device *dev,
}
}
if (((*slots) * sizeof(u32)) < mei_hdr->length) {
- dev_dbg(&dev->pdev->dev,
+ dev_err(&dev->pdev->dev,
"we can't read the message slots =%08x.\n",
*slots);
/* we can't read the message */
@@ -414,20 +423,19 @@ int mei_irq_read_handler(struct mei_device *dev,
} else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
(MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
(dev->iamthif_state == MEI_IAMTHIF_READING)) {
- dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");

+ dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));

ret = mei_amthif_irq_read_msg(dev, mei_hdr, cmpl_list);
if (ret)
goto end;
} else {
- dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
- ret = mei_irq_thread_read_client_message(cmpl_list,
- dev, mei_hdr);
+ dev_dbg(&dev->pdev->dev, "call mei_cl_irq_read_msg.\n");
+ dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
+ ret = mei_cl_irq_read_msg(dev, mei_hdr, cmpl_list);
if (ret)
goto end;
-
}

/* reset the number of slots and header */
@@ -436,7 +444,7 @@ int mei_irq_read_handler(struct mei_device *dev,

if (*slots == -EOVERFLOW) {
/* overflow - reset */
- dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
+ dev_err(&dev->pdev->dev, "resetting due to slots overflow.\n");
/* set the event since message has been read */
ret = -ERANGE;
goto end;
--
1.8.1.2


2013-04-19 19:03:04

by Tomas Winkler

[permalink] [raw]
Subject: [char-misc-next 2/3 RESEND] mei: fix reading large reposnes

While writting to device is limitted to max_msg_length advertized
in client properites the read can be much longer delivered consequiting chunks.

We use krealloc to enlarge the buffer when needed.

Signed-off-by: Tomas Winkler <[email protected]>
---
drivers/misc/mei/bus.c | 8 ++++----
drivers/misc/mei/client.c | 7 ++++---
drivers/misc/mei/client.h | 2 +-
drivers/misc/mei/interrupt.c | 18 +++++++++++++++---
drivers/misc/mei/main.c | 7 +++----
5 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/drivers/misc/mei/bus.c b/drivers/misc/mei/bus.c
index 834ceeb..1e935ea 100644
--- a/drivers/misc/mei/bus.c
+++ b/drivers/misc/mei/bus.c
@@ -286,7 +286,7 @@ int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
mutex_lock(&dev->device_lock);

if (!cl->read_cb) {
- err = mei_cl_read_start(cl);
+ err = mei_cl_read_start(cl, length);
if (err < 0) {
mutex_unlock(&dev->device_lock);
return err;
@@ -378,7 +378,7 @@ static void mei_bus_event_work(struct work_struct *work)
device->events = 0;

/* Prepare for the next read */
- mei_cl_read_start(device->cl);
+ mei_cl_read_start(device->cl, 0);
}

int mei_cl_register_event_cb(struct mei_cl_device *device,
@@ -392,7 +392,7 @@ int mei_cl_register_event_cb(struct mei_cl_device *device,
device->event_context = context;
INIT_WORK(&device->event_work, mei_bus_event_work);

- mei_cl_read_start(device->cl);
+ mei_cl_read_start(device->cl, 0);

return 0;
}
@@ -436,7 +436,7 @@ int mei_cl_enable_device(struct mei_cl_device *device)
mutex_unlock(&dev->device_lock);

if (device->event_cb && !cl->read_cb)
- mei_cl_read_start(device->cl);
+ mei_cl_read_start(device->cl, 0);

if (!device->ops || !device->ops->enable)
return 0;
diff --git a/drivers/misc/mei/client.c b/drivers/misc/mei/client.c
index 9541aa9..7189274 100644
--- a/drivers/misc/mei/client.c
+++ b/drivers/misc/mei/client.c
@@ -624,7 +624,7 @@ int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
*
* returns 0 on success, <0 on failure.
*/
-int mei_cl_read_start(struct mei_cl *cl)
+int mei_cl_read_start(struct mei_cl *cl, size_t length)
{
struct mei_device *dev;
struct mei_cl_cb *cb;
@@ -657,8 +657,9 @@ int mei_cl_read_start(struct mei_cl *cl)
if (!cb)
return -ENOMEM;

- rets = mei_io_cb_alloc_resp_buf(cb,
- dev->me_clients[i].props.max_msg_length);
+ /* always allocate at least client max message */
+ length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
+ rets = mei_io_cb_alloc_resp_buf(cb, length);
if (rets)
goto err;

diff --git a/drivers/misc/mei/client.h b/drivers/misc/mei/client.h
index e890c8b..cfdb144 100644
--- a/drivers/misc/mei/client.h
+++ b/drivers/misc/mei/client.h
@@ -87,7 +87,7 @@ int mei_cl_flow_ctrl_reduce(struct mei_cl *cl);
bool mei_cl_is_other_connecting(struct mei_cl *cl);
int mei_cl_disconnect(struct mei_cl *cl);
int mei_cl_connect(struct mei_cl *cl, struct file *file);
-int mei_cl_read_start(struct mei_cl *cl);
+int mei_cl_read_start(struct mei_cl *cl, size_t length);
int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking);

void mei_host_client_init(struct work_struct *work);
diff --git a/drivers/misc/mei/interrupt.c b/drivers/misc/mei/interrupt.c
index 9bf64c0..7473071 100644
--- a/drivers/misc/mei/interrupt.c
+++ b/drivers/misc/mei/interrupt.c
@@ -145,9 +145,21 @@ static int mei_cl_irq_read_msg(struct mei_device *dev,
}

if (cb->response_buffer.size < mei_hdr->length + cb->buf_idx) {
- dev_warn(&dev->pdev->dev, "message overflow.\n");
- list_del(&cb->list);
- return -ENOMEM;
+ dev_dbg(&dev->pdev->dev, "message overflow. size %d len %d idx %ld\n",
+ cb->response_buffer.size,
+ mei_hdr->length, cb->buf_idx);
+ cb->response_buffer.data =
+ krealloc(cb->response_buffer.data,
+ mei_hdr->length + cb->buf_idx,
+ GFP_KERNEL);
+
+ if (!cb->response_buffer.data) {
+ dev_err(&dev->pdev->dev, "allocation failed.\n");
+ list_del(&cb->list);
+ return -ENOMEM;
+ }
+ cb->response_buffer.size =
+ mei_hdr->length + cb->buf_idx;
}

buffer = cb->response_buffer.data + cb->buf_idx;
diff --git a/drivers/misc/mei/main.c b/drivers/misc/mei/main.c
index 78b4da5..7c44c8d 100644
--- a/drivers/misc/mei/main.c
+++ b/drivers/misc/mei/main.c
@@ -244,7 +244,7 @@ static ssize_t mei_read(struct file *file, char __user *ubuf,
goto out;
}

- err = mei_cl_read_start(cl);
+ err = mei_cl_read_start(cl, length);
if (err && err != -EBUSY) {
dev_dbg(&dev->pdev->dev,
"mei start read failure with status = %d\n", err);
@@ -292,9 +292,8 @@ static ssize_t mei_read(struct file *file, char __user *ubuf,
}
/* now copy the data to user space */
copy_buffer:
- dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
- cb->response_buffer.size);
- dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
+ dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
+ cb->response_buffer.size, cb->buf_idx);
if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
rets = -EMSGSIZE;
goto free;
--
1.8.1.2

2013-04-19 19:03:21

by Tomas Winkler

[permalink] [raw]
Subject: [char-misc-next 3/3 V2 RESEND] mei: reseting -> resetting

From: Bill Nottingham <[email protected]>

This enum leaks out to userspace via error messages, so fix the spelling.

Signed-off-by: Bill Nottingham <[email protected]>
Signed-off-by: Tomas Winkler <[email protected]>
---
V2: rebased
drivers/misc/mei/hbm.c | 8 ++++----
drivers/misc/mei/hw-me.c | 2 +-
drivers/misc/mei/init.c | 4 ++--
drivers/misc/mei/mei_dev.h | 2 +-
4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/mei/hbm.c b/drivers/misc/mei/hbm.c
index 4de80d9..db605f5 100644
--- a/drivers/misc/mei/hbm.c
+++ b/drivers/misc/mei/hbm.c
@@ -52,7 +52,7 @@ static void mei_hbm_me_cl_allocate(struct mei_device *dev)
sizeof(struct mei_me_client), GFP_KERNEL);
if (!clients) {
dev_err(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
- dev->dev_state = MEI_DEV_RESETING;
+ dev->dev_state = MEI_DEV_RESETTING;
mei_reset(dev, 1);
return;
}
@@ -167,7 +167,7 @@ int mei_hbm_start_req(struct mei_device *dev)
dev->hbm_state = MEI_HBM_IDLE;
if (mei_write_message(dev, mei_hdr, dev->wr_msg.data)) {
dev_err(&dev->pdev->dev, "version message writet failed\n");
- dev->dev_state = MEI_DEV_RESETING;
+ dev->dev_state = MEI_DEV_RESETTING;
mei_reset(dev, 1);
return -ENODEV;
}
@@ -196,7 +196,7 @@ static void mei_hbm_enum_clients_req(struct mei_device *dev)
enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;

if (mei_write_message(dev, mei_hdr, dev->wr_msg.data)) {
- dev->dev_state = MEI_DEV_RESETING;
+ dev->dev_state = MEI_DEV_RESETTING;
dev_err(&dev->pdev->dev, "enumeration request write failed.\n");
mei_reset(dev, 1);
}
@@ -249,7 +249,7 @@ static int mei_hbm_prop_req(struct mei_device *dev)
prop_req->address = next_client_index;

if (mei_write_message(dev, mei_hdr, dev->wr_msg.data)) {
- dev->dev_state = MEI_DEV_RESETING;
+ dev->dev_state = MEI_DEV_RESETTING;
dev_err(&dev->pdev->dev, "properties request write failed\n");
mei_reset(dev, 1);

diff --git a/drivers/misc/mei/hw-me.c b/drivers/misc/mei/hw-me.c
index 3d6dfa3..fc03227 100644
--- a/drivers/misc/mei/hw-me.c
+++ b/drivers/misc/mei/hw-me.c
@@ -481,7 +481,7 @@ irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)

/* check if ME wants a reset */
if (!mei_hw_is_ready(dev) &&
- dev->dev_state != MEI_DEV_RESETING &&
+ dev->dev_state != MEI_DEV_RESETTING &&
dev->dev_state != MEI_DEV_INITIALIZING) {
dev_dbg(&dev->pdev->dev, "FW not ready.\n");
mei_reset(dev, 1);
diff --git a/drivers/misc/mei/init.c b/drivers/misc/mei/init.c
index 59159e0..713d89f 100644
--- a/drivers/misc/mei/init.c
+++ b/drivers/misc/mei/init.c
@@ -33,7 +33,7 @@ const char *mei_dev_state_str(int state)
MEI_DEV_STATE(INITIALIZING);
MEI_DEV_STATE(INIT_CLIENTS);
MEI_DEV_STATE(ENABLED);
- MEI_DEV_STATE(RESETING);
+ MEI_DEV_STATE(RESETTING);
MEI_DEV_STATE(DISABLED);
MEI_DEV_STATE(POWER_DOWN);
MEI_DEV_STATE(POWER_UP);
@@ -146,7 +146,7 @@ void mei_reset(struct mei_device *dev, int interrupts_enabled)
if (dev->dev_state != MEI_DEV_INITIALIZING) {
if (dev->dev_state != MEI_DEV_DISABLED &&
dev->dev_state != MEI_DEV_POWER_DOWN)
- dev->dev_state = MEI_DEV_RESETING;
+ dev->dev_state = MEI_DEV_RESETTING;

mei_cl_all_disconnect(dev);

diff --git a/drivers/misc/mei/mei_dev.h b/drivers/misc/mei/mei_dev.h
index c91c492..4de5140 100644
--- a/drivers/misc/mei/mei_dev.h
+++ b/drivers/misc/mei/mei_dev.h
@@ -97,7 +97,7 @@ enum mei_dev_state {
MEI_DEV_INITIALIZING = 0,
MEI_DEV_INIT_CLIENTS,
MEI_DEV_ENABLED,
- MEI_DEV_RESETING,
+ MEI_DEV_RESETTING,
MEI_DEV_DISABLED,
MEI_DEV_POWER_DOWN,
MEI_DEV_POWER_UP
--
1.8.1.2