Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-30 Thread Eduardo Valentin
On Wed, May 29, 2019 at 08:46:48PM -0700, Eduardo Valentin wrote:
> On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:
> > Some protocols over I2C are designed for bi-directional transferring
> > messages by using I2C Master Write protocol. Like the MCTP (Management
> > Component Transport Protocol) and IPMB (Intelligent Platform Management
> > Bus), they both require that the userspace can receive messages from
> > I2C dirvers under slave mode.
> > 
> > This new slave mqueue backend is used to receive and queue messages, it
> > will exposes these messages to userspace by sysfs bin file.
> > 
> > Signed-off-by: Haiyue Wang 
> > ---
> > v4 -> v5:
> >  - Typo: bellowing -> the below
> > 
> > v3 -> v4:
> >  - Drop the small message after receiving I2C STOP.
> > 
> > v2 -> v3:
> >  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().
> > 
> > v1 -> v2:
> >  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
> > ---
> >  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
> >  drivers/i2c/Kconfig|  25 
> >  drivers/i2c/Makefile   |   1 +
> >  drivers/i2c/i2c-slave-mqueue.c | 203 
> > +
> >  4 files changed, 354 insertions(+)
> >  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
> >  create mode 100644 drivers/i2c/i2c-slave-mqueue.c
> > 
> > diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
> > b/Documentation/i2c/slave-mqueue-backend.rst
> > new file mode 100644
> > index 000..3966cf0
> > --- /dev/null
> > +++ b/Documentation/i2c/slave-mqueue-backend.rst
> > @@ -0,0 +1,125 @@
> > +.. SPDX-License-Identifier: GPL-2.0
> > +
> > +=
> > +Linux I2C slave message queue backend
> > +=
> > +
> > +:Author: Haiyue Wang 
> > +
> > +Some protocols over I2C/SMBus are designed for bi-directional transferring
> > +messages by using I2C Master Write protocol. This requires that both sides
> > +of the communication have slave addresses.
> > +
> > +Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
> > +Platform Management Bus), they both require that the userspace can receive
> > +messages from i2c dirvers under slave mode.
> 
> s/dirvers/drivers/g
> 
> > +
> > +This I2C slave mqueue (message queue) backend is used to receive and queue
> > +messages from the remote i2c intelligent device; and it will add the target
> > +slave address (with R/W# bit is always 0) into the message at the first 
> > byte,
> > +so that userspace can use this byte to dispatch the messages into different
> > +handling modules. Also, like IPMB, the address byte is in its message 
> > format,
> > +it needs it to do checksum.
> > +
> > +For messages are time related, so this backend will flush the oldest 
> > message
> > +to queue the newest one.
> > +
> > +Link
> > +
> > +`Intelligent Platform Management Bus
> > +Communications Protocol Specification
> > +`_
> > +
> > +`Management Component Transport Protocol (MCTP)
> > +SMBus/I2C Transport Binding Specification
> > +`_
> > +
> > +How to use
> > +--
> > +For example, the I2C5 bus has slave address 0x10, the below command will 
> > create
> > +the related message queue interface:
> > +
> > +echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
> > +
> > +Then you can dump the messages like this:
> > +
> > +hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
> > +
> > +Code Example
> > +
> > +*Note: call 'lseek' before 'read', this is a requirement from kernfs' 
> > design.*
> > +
> > +::
> > +
> > +  #include 
> > +  #include 
> > +  #include 
> > +  #include 
> > +  #include 
> > +  #include 
> > +  #include 
> > +
> > +  int main(int argc, char *argv[])
> > +  {
> > +  int i, r;
> > +  struct pollfd pfd;
> > +  struct timespec ts;
> > +  unsigned char data[256];
> > +
> > +  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
> > +  if (pfd.fd < 0)
> > +  return -1;
> > +
> > +  pfd.events = POLLPRI;
> > +
> > +  while (1) {
> > +  r = poll(, 1, 5000);
> > +
> > +  if (r < 0)
> > +  break;
> > +
> > +  if (r == 0 || !(pfd.revents & POLLPRI))
> > +  continue;
> > +
> > +  lseek(pfd.fd, 0, SEEK_SET);
> > +  r = read(pfd.fd, data, sizeof(data));
> > +  if (r <= 0)
> > +  continue;
> > +
> > +  clock_gettime(CLOCK_MONOTONIC, );
> > +  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
> > +  for (i = 0; i < r; i++)
> > +   

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-29 Thread Eduardo Valentin
On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:
> Some protocols over I2C are designed for bi-directional transferring
> messages by using I2C Master Write protocol. Like the MCTP (Management
> Component Transport Protocol) and IPMB (Intelligent Platform Management
> Bus), they both require that the userspace can receive messages from
> I2C dirvers under slave mode.
> 
> This new slave mqueue backend is used to receive and queue messages, it
> will exposes these messages to userspace by sysfs bin file.
> 
> Signed-off-by: Haiyue Wang 
> ---
> v4 -> v5:
>  - Typo: bellowing -> the below
> 
> v3 -> v4:
>  - Drop the small message after receiving I2C STOP.
> 
> v2 -> v3:
>  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().
> 
> v1 -> v2:
>  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
> ---
>  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
>  drivers/i2c/Kconfig|  25 
>  drivers/i2c/Makefile   |   1 +
>  drivers/i2c/i2c-slave-mqueue.c | 203 
> +
>  4 files changed, 354 insertions(+)
>  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
>  create mode 100644 drivers/i2c/i2c-slave-mqueue.c
> 
> diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
> b/Documentation/i2c/slave-mqueue-backend.rst
> new file mode 100644
> index 000..3966cf0
> --- /dev/null
> +++ b/Documentation/i2c/slave-mqueue-backend.rst
> @@ -0,0 +1,125 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +=
> +Linux I2C slave message queue backend
> +=
> +
> +:Author: Haiyue Wang 
> +
> +Some protocols over I2C/SMBus are designed for bi-directional transferring
> +messages by using I2C Master Write protocol. This requires that both sides
> +of the communication have slave addresses.
> +
> +Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
> +Platform Management Bus), they both require that the userspace can receive
> +messages from i2c dirvers under slave mode.
> +
> +This I2C slave mqueue (message queue) backend is used to receive and queue
> +messages from the remote i2c intelligent device; and it will add the target
> +slave address (with R/W# bit is always 0) into the message at the first byte,
> +so that userspace can use this byte to dispatch the messages into different
> +handling modules. Also, like IPMB, the address byte is in its message format,
> +it needs it to do checksum.
> +
> +For messages are time related, so this backend will flush the oldest message
> +to queue the newest one.
> +
> +Link
> +
> +`Intelligent Platform Management Bus
> +Communications Protocol Specification
> +`_
> +
> +`Management Component Transport Protocol (MCTP)
> +SMBus/I2C Transport Binding Specification
> +`_
> +
> +How to use
> +--
> +For example, the I2C5 bus has slave address 0x10, the below command will 
> create
> +the related message queue interface:
> +
> +echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
> +
> +Then you can dump the messages like this:
> +
> +hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
> +
> +Code Example
> +
> +*Note: call 'lseek' before 'read', this is a requirement from kernfs' 
> design.*
> +
> +::
> +
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +
> +  int main(int argc, char *argv[])
> +  {
> +  int i, r;
> +  struct pollfd pfd;
> +  struct timespec ts;
> +  unsigned char data[256];
> +
> +  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
> +  if (pfd.fd < 0)
> +  return -1;
> +
> +  pfd.events = POLLPRI;
> +
> +  while (1) {
> +  r = poll(, 1, 5000);
> +
> +  if (r < 0)
> +  break;
> +
> +  if (r == 0 || !(pfd.revents & POLLPRI))
> +  continue;
> +
> +  lseek(pfd.fd, 0, SEEK_SET);
> +  r = read(pfd.fd, data, sizeof(data));
> +  if (r <= 0)
> +  continue;
> +
> +  clock_gettime(CLOCK_MONOTONIC, );
> +  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
> +  for (i = 0; i < r; i++)
> +  printf(" %02x", data[i]);
> +  printf("\n");
> +  }
> +
> +  close(pfd.fd);
> +
> +  return 0;
> +  }
> +
> +Result
> +--
> +*./a.out "/sys/bus/i2c/devices/5-1010/slave-mqueue"*
> +
> +::
> +
> +  [10183.232500449] : 20 18 c8 2c 78 01 5b
> +  [10183.479358348] : 20 18 c8 2c 78 01 5b
> +  [10183.726556812] : 20 18 c8 2c 78 01 5b

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-29 Thread Eduardo Valentin
On Thu, May 30, 2019 at 09:33:34AM +0800, Wang, Haiyue wrote:
> 
> 在 2019-05-30 07:11, Eduardo Valentin 写道:
> >>+
> >>+   case I2C_SLAVE_WRITE_RECEIVED:
> >>+   if (msg->len < MQ_MSGBUF_SIZE) {
> >>+   msg->buf[msg->len++] = *val;
> >Do we need to lock the accesses to msg->buf? how about to msg->len?
> >>>this code goes access and modify data here, e.g. msg->len and msg->buf.
> >>>
> >>>On this case (I2C_SLAVE_WRITE_RECEIVED), this code wont protect access.
> >>>
> >>>This can cause concurrence issues if you receive an IRQ when the user
> >>>is on your bin_read().
> >>User will not touch 'msg = mq->curr;', just touch 'msg =
> >>>queue[mq->out];'
> >What happens if mq->curr == mq->queue[mq->out]?
> >
> 1. The Read will check.
> 
> + spin_lock_irqsave(>lock, flags);
> + if (mq->out != mq->in) {
> + msg = >queue[mq->out];
> 
> 2. Flush the oldeast message. ^_^
> 
> + case I2C_SLAVE_STOP:
> + if (unlikely(mq->truncated || msg->len < 2))
> + break;
> +
> + spin_lock(>lock);
> + mq->in = MQ_QUEUE_NEXT(mq->in);
> + mq->curr = >queue[mq->in];
> + mq->curr->len = 0;
> +
> + /* Flush the oldest message */
> + if (mq->out == mq->in)
> + mq->out = MQ_QUEUE_NEXT(mq->out);

Yeah, I see. We keep on dropping messages (old ones) when the queue is full...

> + spin_unlock(>lock);
> 

-- 
All the best,
Eduardo Valentin


Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-29 Thread Eduardo Valentin
On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:
> Some protocols over I2C are designed for bi-directional transferring
> messages by using I2C Master Write protocol. Like the MCTP (Management
> Component Transport Protocol) and IPMB (Intelligent Platform Management
> Bus), they both require that the userspace can receive messages from
> I2C dirvers under slave mode.
> 
> This new slave mqueue backend is used to receive and queue messages, it
> will exposes these messages to userspace by sysfs bin file.
> 
> Signed-off-by: Haiyue Wang 
> ---
> v4 -> v5:
>  - Typo: bellowing -> the below
> 
> v3 -> v4:
>  - Drop the small message after receiving I2C STOP.
> 
> v2 -> v3:
>  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().
> 
> v1 -> v2:
>  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
> ---
>  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
>  drivers/i2c/Kconfig|  25 
>  drivers/i2c/Makefile   |   1 +
>  drivers/i2c/i2c-slave-mqueue.c | 203 
> +
>  4 files changed, 354 insertions(+)
>  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
>  create mode 100644 drivers/i2c/i2c-slave-mqueue.c
> 
> diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
> b/Documentation/i2c/slave-mqueue-backend.rst
> new file mode 100644
> index 000..3966cf0
> --- /dev/null
> +++ b/Documentation/i2c/slave-mqueue-backend.rst
> @@ -0,0 +1,125 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +=
> +Linux I2C slave message queue backend
> +=
> +
> +:Author: Haiyue Wang 
> +
> +Some protocols over I2C/SMBus are designed for bi-directional transferring
> +messages by using I2C Master Write protocol. This requires that both sides
> +of the communication have slave addresses.
> +
> +Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
> +Platform Management Bus), they both require that the userspace can receive
> +messages from i2c dirvers under slave mode.

s/dirvers/drivers/g

> +
> +This I2C slave mqueue (message queue) backend is used to receive and queue
> +messages from the remote i2c intelligent device; and it will add the target
> +slave address (with R/W# bit is always 0) into the message at the first byte,
> +so that userspace can use this byte to dispatch the messages into different
> +handling modules. Also, like IPMB, the address byte is in its message format,
> +it needs it to do checksum.
> +
> +For messages are time related, so this backend will flush the oldest message
> +to queue the newest one.
> +
> +Link
> +
> +`Intelligent Platform Management Bus
> +Communications Protocol Specification
> +`_
> +
> +`Management Component Transport Protocol (MCTP)
> +SMBus/I2C Transport Binding Specification
> +`_
> +
> +How to use
> +--
> +For example, the I2C5 bus has slave address 0x10, the below command will 
> create
> +the related message queue interface:
> +
> +echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
> +
> +Then you can dump the messages like this:
> +
> +hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
> +
> +Code Example
> +
> +*Note: call 'lseek' before 'read', this is a requirement from kernfs' 
> design.*
> +
> +::
> +
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +
> +  int main(int argc, char *argv[])
> +  {
> +  int i, r;
> +  struct pollfd pfd;
> +  struct timespec ts;
> +  unsigned char data[256];
> +
> +  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
> +  if (pfd.fd < 0)
> +  return -1;
> +
> +  pfd.events = POLLPRI;
> +
> +  while (1) {
> +  r = poll(, 1, 5000);
> +
> +  if (r < 0)
> +  break;
> +
> +  if (r == 0 || !(pfd.revents & POLLPRI))
> +  continue;
> +
> +  lseek(pfd.fd, 0, SEEK_SET);
> +  r = read(pfd.fd, data, sizeof(data));
> +  if (r <= 0)
> +  continue;
> +
> +  clock_gettime(CLOCK_MONOTONIC, );
> +  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
> +  for (i = 0; i < r; i++)
> +  printf(" %02x", data[i]);
> +  printf("\n");
> +  }
> +
> +  close(pfd.fd);
> +
> +  return 0;
> +  }
> +
> +Result
> +--
> +*./a.out "/sys/bus/i2c/devices/5-1010/slave-mqueue"*
> +
> +::
> +
> +  [10183.232500449] : 20 18 c8 2c 78 01 5b
> +  [10183.479358348] : 20 18 c8 2c 78 01 5b
> +  [10183.726556812] 

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-29 Thread Wang, Haiyue



在 2019-05-30 07:11, Eduardo Valentin 写道:

this code goes access and modify data here, e.g. msg->len and msg->buf.

On this case (I2C_SLAVE_WRITE_RECEIVED), this code wont protect access.

This can cause concurrence issues if you receive an IRQ when the user
is on your bin_read().

User will not touch 'msg = mq->curr;', just touch 'msg =
>queue[mq->out];'

What happens if mq->curr == mq->queue[mq->out]?


1. The Read will check.

+   spin_lock_irqsave(>lock, flags);
+   if (mq->out != mq->in) {
+   msg = >queue[mq->out];

2. Flush the oldeast message. ^_^

+   case I2C_SLAVE_STOP:
+   if (unlikely(mq->truncated || msg->len < 2))
+   break;
+
+   spin_lock(>lock);
+   mq->in = MQ_QUEUE_NEXT(mq->in);
+   mq->curr = >queue[mq->in];
+   mq->curr->len = 0;
+
+   /* Flush the oldest message */
+   if (mq->out == mq->in)
+   mq->out = MQ_QUEUE_NEXT(mq->out);
+   spin_unlock(>lock);




Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-29 Thread Eduardo Valentin
Hello,

On Sat, May 25, 2019 at 01:10:46PM +0800, Wang, Haiyue wrote:
> 
> 在 2019-05-25 01:33, Eduardo Valentin 写道:
> >Hey,
> >
> >On Fri, May 24, 2019 at 10:43:16AM +0800, Wang, Haiyue wrote:
> >>Thanks for interest, the design idea is from:
> >>
> >>https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/i2c/i2c-slave-eeprom.c?h=v5.2-rc1
> >>
> >>and
> >>
> >>https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/slave-interface
> >>
> >>Then you will get the answer. ;-)
> >Well, maybe :-) see further comments inline..
> Please see in line. And how about the test result in your real system ? It
> works as expected ?
> >>BR,
> >>
> >>Haiyue
> >>
> >>
> >>在 2019-05-24 06:03, Eduardo Valentin 写道:
> >>>Hey Wang,
> >>>
> >>>On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:
> Some protocols over I2C are designed for bi-directional transferring
> messages by using I2C Master Write protocol. Like the MCTP (Management
> Component Transport Protocol) and IPMB (Intelligent Platform Management
> Bus), they both require that the userspace can receive messages from
> I2C dirvers under slave mode.
> 
> This new slave mqueue backend is used to receive and queue messages, it
> will exposes these messages to userspace by sysfs bin file.
> 
> Signed-off-by: Haiyue Wang 
> ---
> v4 -> v5:
>   - Typo: bellowing -> the below
> 
> v3 -> v4:
>   - Drop the small message after receiving I2C STOP.
> 
> v2 -> v3:
>   - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().
> 
> v1 -> v2:
>   - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
> ---
>   Documentation/i2c/slave-mqueue-backend.rst | 125 ++
>   drivers/i2c/Kconfig|  25 
>   drivers/i2c/Makefile   |   1 +
>   drivers/i2c/i2c-slave-mqueue.c | 203 
>  +
>   4 files changed, 354 insertions(+)
>   create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
>   create mode 100644 drivers/i2c/i2c-slave-mqueue.c
> 
> diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
> b/Documentation/i2c/slave-mqueue-backend.rst
> new file mode 100644
> index 000..3966cf0
> --- /dev/null
> +++ b/Documentation/i2c/slave-mqueue-backend.rst
> @@ -0,0 +1,125 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +=
> +Linux I2C slave message queue backend
> +=
> +
> +:Author: Haiyue Wang 
> +
> +Some protocols over I2C/SMBus are designed for bi-directional 
> transferring
> +messages by using I2C Master Write protocol. This requires that both 
> sides
> +of the communication have slave addresses.
> +
> +Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
> +Platform Management Bus), they both require that the userspace can 
> receive
> +messages from i2c dirvers under slave mode.
> +
> +This I2C slave mqueue (message queue) backend is used to receive and 
> queue
> +messages from the remote i2c intelligent device; and it will add the 
> target
> +slave address (with R/W# bit is always 0) into the message at the first 
> byte,
> +so that userspace can use this byte to dispatch the messages into 
> different
> +handling modules. Also, like IPMB, the address byte is in its message 
> format,
> +it needs it to do checksum.
> +
> +For messages are time related, so this backend will flush the oldest 
> message
> +to queue the newest one.
> +
> +Link
> +
> +`Intelligent Platform Management Bus
> +Communications Protocol Specification
> +`_
> +
> +`Management Component Transport Protocol (MCTP)
> +SMBus/I2C Transport Binding Specification
> +`_
> +
> +How to use
> +--
> +For example, the I2C5 bus has slave address 0x10, the below command will 
> create
> +the related message queue interface:
> +
> +echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
> +
> +Then you can dump the messages like this:
> +
> +hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
> +
> +Code Example
> +
> +*Note: call 'lseek' before 'read', this is a requirement from kernfs' 
> design.*
> +
> +::
> +
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +
> +  int main(int argc, char *argv[])
> +  {
> + 

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-24 Thread Wang, Haiyue



在 2019-05-25 01:33, Eduardo Valentin 写道:

Hey,

On Fri, May 24, 2019 at 10:43:16AM +0800, Wang, Haiyue wrote:

Thanks for interest, the design idea is from:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/i2c/i2c-slave-eeprom.c?h=v5.2-rc1

and

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/slave-interface

Then you will get the answer. ;-)

Well, maybe :-) see further comments inline..
Please see in line. And how about the test result in your real system ? 
It works as expected ?

BR,

Haiyue


在 2019-05-24 06:03, Eduardo Valentin 写道:

Hey Wang,

On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:

Some protocols over I2C are designed for bi-directional transferring
messages by using I2C Master Write protocol. Like the MCTP (Management
Component Transport Protocol) and IPMB (Intelligent Platform Management
Bus), they both require that the userspace can receive messages from
I2C dirvers under slave mode.

This new slave mqueue backend is used to receive and queue messages, it
will exposes these messages to userspace by sysfs bin file.

Signed-off-by: Haiyue Wang 
---
v4 -> v5:
  - Typo: bellowing -> the below

v3 -> v4:
  - Drop the small message after receiving I2C STOP.

v2 -> v3:
  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().

v1 -> v2:
  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
---
  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
  drivers/i2c/Kconfig|  25 
  drivers/i2c/Makefile   |   1 +
  drivers/i2c/i2c-slave-mqueue.c | 203 +
  4 files changed, 354 insertions(+)
  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
  create mode 100644 drivers/i2c/i2c-slave-mqueue.c

diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
b/Documentation/i2c/slave-mqueue-backend.rst
new file mode 100644
index 000..3966cf0
--- /dev/null
+++ b/Documentation/i2c/slave-mqueue-backend.rst
@@ -0,0 +1,125 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=
+Linux I2C slave message queue backend
+=
+
+:Author: Haiyue Wang 
+
+Some protocols over I2C/SMBus are designed for bi-directional transferring
+messages by using I2C Master Write protocol. This requires that both sides
+of the communication have slave addresses.
+
+Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
+Platform Management Bus), they both require that the userspace can receive
+messages from i2c dirvers under slave mode.
+
+This I2C slave mqueue (message queue) backend is used to receive and queue
+messages from the remote i2c intelligent device; and it will add the target
+slave address (with R/W# bit is always 0) into the message at the first byte,
+so that userspace can use this byte to dispatch the messages into different
+handling modules. Also, like IPMB, the address byte is in its message format,
+it needs it to do checksum.
+
+For messages are time related, so this backend will flush the oldest message
+to queue the newest one.
+
+Link
+
+`Intelligent Platform Management Bus
+Communications Protocol Specification
+`_
+
+`Management Component Transport Protocol (MCTP)
+SMBus/I2C Transport Binding Specification
+`_
+
+How to use
+--
+For example, the I2C5 bus has slave address 0x10, the below command will create
+the related message queue interface:
+
+echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
+
+Then you can dump the messages like this:
+
+hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
+
+Code Example
+
+*Note: call 'lseek' before 'read', this is a requirement from kernfs' design.*
+
+::
+
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+
+  int main(int argc, char *argv[])
+  {
+  int i, r;
+  struct pollfd pfd;
+  struct timespec ts;
+  unsigned char data[256];
+
+  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
+  if (pfd.fd < 0)
+  return -1;
+
+  pfd.events = POLLPRI;
+
+  while (1) {
+  r = poll(, 1, 5000);
+
+  if (r < 0)
+  break;
+
+  if (r == 0 || !(pfd.revents & POLLPRI))
+  continue;
+
+  lseek(pfd.fd, 0, SEEK_SET);
+  r = read(pfd.fd, data, sizeof(data));
+  if (r <= 0)
+  continue;
+
+  clock_gettime(CLOCK_MONOTONIC, );
+  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
+  for (i = 0; i < r; i++)
+

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-24 Thread Eduardo Valentin
Hey,

On Fri, May 24, 2019 at 10:43:16AM +0800, Wang, Haiyue wrote:
> Thanks for interest, the design idea is from:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/i2c/i2c-slave-eeprom.c?h=v5.2-rc1
> 
> and
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/slave-interface
> 
> Then you will get the answer. ;-)

Well, maybe :-) see further comments inline..

> 
> BR,
> 
> Haiyue
> 
> 
> 在 2019-05-24 06:03, Eduardo Valentin 写道:
> >Hey Wang,
> >
> >On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:
> >>Some protocols over I2C are designed for bi-directional transferring
> >>messages by using I2C Master Write protocol. Like the MCTP (Management
> >>Component Transport Protocol) and IPMB (Intelligent Platform Management
> >>Bus), they both require that the userspace can receive messages from
> >>I2C dirvers under slave mode.
> >>
> >>This new slave mqueue backend is used to receive and queue messages, it
> >>will exposes these messages to userspace by sysfs bin file.
> >>
> >>Signed-off-by: Haiyue Wang 
> >>---
> >>v4 -> v5:
> >>  - Typo: bellowing -> the below
> >>
> >>v3 -> v4:
> >>  - Drop the small message after receiving I2C STOP.
> >>
> >>v2 -> v3:
> >>  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().
> >>
> >>v1 -> v2:
> >>  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
> >>---
> >>  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
> >>  drivers/i2c/Kconfig|  25 
> >>  drivers/i2c/Makefile   |   1 +
> >>  drivers/i2c/i2c-slave-mqueue.c | 203 
> >> +
> >>  4 files changed, 354 insertions(+)
> >>  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
> >>  create mode 100644 drivers/i2c/i2c-slave-mqueue.c
> >>
> >>diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
> >>b/Documentation/i2c/slave-mqueue-backend.rst
> >>new file mode 100644
> >>index 000..3966cf0
> >>--- /dev/null
> >>+++ b/Documentation/i2c/slave-mqueue-backend.rst
> >>@@ -0,0 +1,125 @@
> >>+.. SPDX-License-Identifier: GPL-2.0
> >>+
> >>+=
> >>+Linux I2C slave message queue backend
> >>+=
> >>+
> >>+:Author: Haiyue Wang 
> >>+
> >>+Some protocols over I2C/SMBus are designed for bi-directional transferring
> >>+messages by using I2C Master Write protocol. This requires that both sides
> >>+of the communication have slave addresses.
> >>+
> >>+Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
> >>+Platform Management Bus), they both require that the userspace can receive
> >>+messages from i2c dirvers under slave mode.
> >>+
> >>+This I2C slave mqueue (message queue) backend is used to receive and queue
> >>+messages from the remote i2c intelligent device; and it will add the target
> >>+slave address (with R/W# bit is always 0) into the message at the first 
> >>byte,
> >>+so that userspace can use this byte to dispatch the messages into different
> >>+handling modules. Also, like IPMB, the address byte is in its message 
> >>format,
> >>+it needs it to do checksum.
> >>+
> >>+For messages are time related, so this backend will flush the oldest 
> >>message
> >>+to queue the newest one.
> >>+
> >>+Link
> >>+
> >>+`Intelligent Platform Management Bus
> >>+Communications Protocol Specification
> >>+`_
> >>+
> >>+`Management Component Transport Protocol (MCTP)
> >>+SMBus/I2C Transport Binding Specification
> >>+`_
> >>+
> >>+How to use
> >>+--
> >>+For example, the I2C5 bus has slave address 0x10, the below command will 
> >>create
> >>+the related message queue interface:
> >>+
> >>+echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
> >>+
> >>+Then you can dump the messages like this:
> >>+
> >>+hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
> >>+
> >>+Code Example
> >>+
> >>+*Note: call 'lseek' before 'read', this is a requirement from kernfs' 
> >>design.*
> >>+
> >>+::
> >>+
> >>+  #include 
> >>+  #include 
> >>+  #include 
> >>+  #include 
> >>+  #include 
> >>+  #include 
> >>+  #include 
> >>+
> >>+  int main(int argc, char *argv[])
> >>+  {
> >>+  int i, r;
> >>+  struct pollfd pfd;
> >>+  struct timespec ts;
> >>+  unsigned char data[256];
> >>+
> >>+  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
> >>+  if (pfd.fd < 0)
> >>+  return -1;
> >>+
> >>+  pfd.events = POLLPRI;
> >>+
> >>+  while (1) {
> >>+  r = poll(, 1, 5000);
> >>+
> >>+  if (r < 0)
> >>+  break;
> >>+
> >>+  if (r == 0 || !(pfd.revents & 

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-23 Thread Wang, Haiyue

Thanks for interest, the design idea is from:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/i2c/i2c-slave-eeprom.c?h=v5.2-rc1

and

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/slave-interface

Then you will get the answer. ;-)

BR,

Haiyue


在 2019-05-24 06:03, Eduardo Valentin 写道:

Hey Wang,

On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:

Some protocols over I2C are designed for bi-directional transferring
messages by using I2C Master Write protocol. Like the MCTP (Management
Component Transport Protocol) and IPMB (Intelligent Platform Management
Bus), they both require that the userspace can receive messages from
I2C dirvers under slave mode.

This new slave mqueue backend is used to receive and queue messages, it
will exposes these messages to userspace by sysfs bin file.

Signed-off-by: Haiyue Wang 
---
v4 -> v5:
  - Typo: bellowing -> the below

v3 -> v4:
  - Drop the small message after receiving I2C STOP.

v2 -> v3:
  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().

v1 -> v2:
  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
---
  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
  drivers/i2c/Kconfig|  25 
  drivers/i2c/Makefile   |   1 +
  drivers/i2c/i2c-slave-mqueue.c | 203 +
  4 files changed, 354 insertions(+)
  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
  create mode 100644 drivers/i2c/i2c-slave-mqueue.c

diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
b/Documentation/i2c/slave-mqueue-backend.rst
new file mode 100644
index 000..3966cf0
--- /dev/null
+++ b/Documentation/i2c/slave-mqueue-backend.rst
@@ -0,0 +1,125 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=
+Linux I2C slave message queue backend
+=
+
+:Author: Haiyue Wang 
+
+Some protocols over I2C/SMBus are designed for bi-directional transferring
+messages by using I2C Master Write protocol. This requires that both sides
+of the communication have slave addresses.
+
+Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
+Platform Management Bus), they both require that the userspace can receive
+messages from i2c dirvers under slave mode.
+
+This I2C slave mqueue (message queue) backend is used to receive and queue
+messages from the remote i2c intelligent device; and it will add the target
+slave address (with R/W# bit is always 0) into the message at the first byte,
+so that userspace can use this byte to dispatch the messages into different
+handling modules. Also, like IPMB, the address byte is in its message format,
+it needs it to do checksum.
+
+For messages are time related, so this backend will flush the oldest message
+to queue the newest one.
+
+Link
+
+`Intelligent Platform Management Bus
+Communications Protocol Specification
+`_
+
+`Management Component Transport Protocol (MCTP)
+SMBus/I2C Transport Binding Specification
+`_
+
+How to use
+--
+For example, the I2C5 bus has slave address 0x10, the below command will create
+the related message queue interface:
+
+echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
+
+Then you can dump the messages like this:
+
+hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
+
+Code Example
+
+*Note: call 'lseek' before 'read', this is a requirement from kernfs' design.*
+
+::
+
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+
+  int main(int argc, char *argv[])
+  {
+  int i, r;
+  struct pollfd pfd;
+  struct timespec ts;
+  unsigned char data[256];
+
+  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
+  if (pfd.fd < 0)
+  return -1;
+
+  pfd.events = POLLPRI;
+
+  while (1) {
+  r = poll(, 1, 5000);
+
+  if (r < 0)
+  break;
+
+  if (r == 0 || !(pfd.revents & POLLPRI))
+  continue;
+
+  lseek(pfd.fd, 0, SEEK_SET);
+  r = read(pfd.fd, data, sizeof(data));
+  if (r <= 0)
+  continue;
+
+  clock_gettime(CLOCK_MONOTONIC, );
+  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
+  for (i = 0; i < r; i++)
+  printf(" %02x", data[i]);
+  printf("\n");
+  }
+
+  close(pfd.fd);
+
+  return 0;
+  }
+
+Result
+--
+*./a.out "/sys/bus/i2c/devices/5-1010/slave-mqueue"*
+
+::
+
+  [10183.232500449] : 20 18 c8 2c 78 01 5b
+ 

Re: [PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2019-05-23 Thread Eduardo Valentin
Hey Wang,

On Tue, Apr 24, 2018 at 01:06:32AM +0800, Haiyue Wang wrote:
> Some protocols over I2C are designed for bi-directional transferring
> messages by using I2C Master Write protocol. Like the MCTP (Management
> Component Transport Protocol) and IPMB (Intelligent Platform Management
> Bus), they both require that the userspace can receive messages from
> I2C dirvers under slave mode.
> 
> This new slave mqueue backend is used to receive and queue messages, it
> will exposes these messages to userspace by sysfs bin file.
> 
> Signed-off-by: Haiyue Wang 
> ---
> v4 -> v5:
>  - Typo: bellowing -> the below
> 
> v3 -> v4:
>  - Drop the small message after receiving I2C STOP.
> 
> v2 -> v3:
>  - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().
> 
> v1 -> v2:
>  - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
> ---
>  Documentation/i2c/slave-mqueue-backend.rst | 125 ++
>  drivers/i2c/Kconfig|  25 
>  drivers/i2c/Makefile   |   1 +
>  drivers/i2c/i2c-slave-mqueue.c | 203 
> +
>  4 files changed, 354 insertions(+)
>  create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
>  create mode 100644 drivers/i2c/i2c-slave-mqueue.c
> 
> diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
> b/Documentation/i2c/slave-mqueue-backend.rst
> new file mode 100644
> index 000..3966cf0
> --- /dev/null
> +++ b/Documentation/i2c/slave-mqueue-backend.rst
> @@ -0,0 +1,125 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +
> +=
> +Linux I2C slave message queue backend
> +=
> +
> +:Author: Haiyue Wang 
> +
> +Some protocols over I2C/SMBus are designed for bi-directional transferring
> +messages by using I2C Master Write protocol. This requires that both sides
> +of the communication have slave addresses.
> +
> +Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
> +Platform Management Bus), they both require that the userspace can receive
> +messages from i2c dirvers under slave mode.
> +
> +This I2C slave mqueue (message queue) backend is used to receive and queue
> +messages from the remote i2c intelligent device; and it will add the target
> +slave address (with R/W# bit is always 0) into the message at the first byte,
> +so that userspace can use this byte to dispatch the messages into different
> +handling modules. Also, like IPMB, the address byte is in its message format,
> +it needs it to do checksum.
> +
> +For messages are time related, so this backend will flush the oldest message
> +to queue the newest one.
> +
> +Link
> +
> +`Intelligent Platform Management Bus
> +Communications Protocol Specification
> +`_
> +
> +`Management Component Transport Protocol (MCTP)
> +SMBus/I2C Transport Binding Specification
> +`_
> +
> +How to use
> +--
> +For example, the I2C5 bus has slave address 0x10, the below command will 
> create
> +the related message queue interface:
> +
> +echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
> +
> +Then you can dump the messages like this:
> +
> +hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
> +
> +Code Example
> +
> +*Note: call 'lseek' before 'read', this is a requirement from kernfs' 
> design.*
> +
> +::
> +
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +  #include 
> +
> +  int main(int argc, char *argv[])
> +  {
> +  int i, r;
> +  struct pollfd pfd;
> +  struct timespec ts;
> +  unsigned char data[256];
> +
> +  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
> +  if (pfd.fd < 0)
> +  return -1;
> +
> +  pfd.events = POLLPRI;
> +
> +  while (1) {
> +  r = poll(, 1, 5000);
> +
> +  if (r < 0)
> +  break;
> +
> +  if (r == 0 || !(pfd.revents & POLLPRI))
> +  continue;
> +
> +  lseek(pfd.fd, 0, SEEK_SET);
> +  r = read(pfd.fd, data, sizeof(data));
> +  if (r <= 0)
> +  continue;
> +
> +  clock_gettime(CLOCK_MONOTONIC, );
> +  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
> +  for (i = 0; i < r; i++)
> +  printf(" %02x", data[i]);
> +  printf("\n");
> +  }
> +
> +  close(pfd.fd);
> +
> +  return 0;
> +  }
> +
> +Result
> +--
> +*./a.out "/sys/bus/i2c/devices/5-1010/slave-mqueue"*
> +
> +::
> +
> +  [10183.232500449] : 20 18 c8 2c 78 01 5b
> +  [10183.479358348] : 20 18 c8 2c 78 01 5b
> +  [10183.726556812] : 20 18 c8 

[PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2018-04-23 Thread Haiyue Wang
Some protocols over I2C are designed for bi-directional transferring
messages by using I2C Master Write protocol. Like the MCTP (Management
Component Transport Protocol) and IPMB (Intelligent Platform Management
Bus), they both require that the userspace can receive messages from
I2C dirvers under slave mode.

This new slave mqueue backend is used to receive and queue messages, it
will exposes these messages to userspace by sysfs bin file.

Signed-off-by: Haiyue Wang 
---
v4 -> v5:
 - Typo: bellowing -> the below

v3 -> v4:
 - Drop the small message after receiving I2C STOP.

v2 -> v3:
 - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().

v1 -> v2:
 - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
---
 Documentation/i2c/slave-mqueue-backend.rst | 125 ++
 drivers/i2c/Kconfig|  25 
 drivers/i2c/Makefile   |   1 +
 drivers/i2c/i2c-slave-mqueue.c | 203 +
 4 files changed, 354 insertions(+)
 create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
 create mode 100644 drivers/i2c/i2c-slave-mqueue.c

diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
b/Documentation/i2c/slave-mqueue-backend.rst
new file mode 100644
index 000..3966cf0
--- /dev/null
+++ b/Documentation/i2c/slave-mqueue-backend.rst
@@ -0,0 +1,125 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=
+Linux I2C slave message queue backend
+=
+
+:Author: Haiyue Wang 
+
+Some protocols over I2C/SMBus are designed for bi-directional transferring
+messages by using I2C Master Write protocol. This requires that both sides
+of the communication have slave addresses.
+
+Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
+Platform Management Bus), they both require that the userspace can receive
+messages from i2c dirvers under slave mode.
+
+This I2C slave mqueue (message queue) backend is used to receive and queue
+messages from the remote i2c intelligent device; and it will add the target
+slave address (with R/W# bit is always 0) into the message at the first byte,
+so that userspace can use this byte to dispatch the messages into different
+handling modules. Also, like IPMB, the address byte is in its message format,
+it needs it to do checksum.
+
+For messages are time related, so this backend will flush the oldest message
+to queue the newest one.
+
+Link
+
+`Intelligent Platform Management Bus
+Communications Protocol Specification
+`_
+
+`Management Component Transport Protocol (MCTP)
+SMBus/I2C Transport Binding Specification
+`_
+
+How to use
+--
+For example, the I2C5 bus has slave address 0x10, the below command will create
+the related message queue interface:
+
+echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
+
+Then you can dump the messages like this:
+
+hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
+
+Code Example
+
+*Note: call 'lseek' before 'read', this is a requirement from kernfs' design.*
+
+::
+
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+
+  int main(int argc, char *argv[])
+  {
+  int i, r;
+  struct pollfd pfd;
+  struct timespec ts;
+  unsigned char data[256];
+
+  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
+  if (pfd.fd < 0)
+  return -1;
+
+  pfd.events = POLLPRI;
+
+  while (1) {
+  r = poll(, 1, 5000);
+
+  if (r < 0)
+  break;
+
+  if (r == 0 || !(pfd.revents & POLLPRI))
+  continue;
+
+  lseek(pfd.fd, 0, SEEK_SET);
+  r = read(pfd.fd, data, sizeof(data));
+  if (r <= 0)
+  continue;
+
+  clock_gettime(CLOCK_MONOTONIC, );
+  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
+  for (i = 0; i < r; i++)
+  printf(" %02x", data[i]);
+  printf("\n");
+  }
+
+  close(pfd.fd);
+
+  return 0;
+  }
+
+Result
+--
+*./a.out "/sys/bus/i2c/devices/5-1010/slave-mqueue"*
+
+::
+
+  [10183.232500449] : 20 18 c8 2c 78 01 5b
+  [10183.479358348] : 20 18 c8 2c 78 01 5b
+  [10183.726556812] : 20 18 c8 2c 78 01 5b
+  [10183.972605863] : 20 18 c8 2c 78 01 5b
+  [10184.220124772] : 20 18 c8 2c 78 01 5b
+  [10184.467764166] : 20 18 c8 2c 78 01 5b
+  [10193.233421784] : 20 18 c8 2c 7c 01 57
+  [10193.480273460] : 20 18 c8 2c 7c 01 57
+  [10193.726788733] : 20 18 c8 2c 7c 01 57
+  [10193.972781945] : 20 18 c8 2c 7c 01 

[PATCH i2c/slave-mqueue v5] i2c: slave-mqueue: add a slave backend to receive and queue messages

2018-04-23 Thread Haiyue Wang
Some protocols over I2C are designed for bi-directional transferring
messages by using I2C Master Write protocol. Like the MCTP (Management
Component Transport Protocol) and IPMB (Intelligent Platform Management
Bus), they both require that the userspace can receive messages from
I2C dirvers under slave mode.

This new slave mqueue backend is used to receive and queue messages, it
will exposes these messages to userspace by sysfs bin file.

Signed-off-by: Haiyue Wang 
---
v4 -> v5:
 - Typo: bellowing -> the below

v3 -> v4:
 - Drop the small message after receiving I2C STOP.

v2 -> v3:
 - Just remove the ';' after the end '}' of i2c_slave_mqueue_probe().

v1 -> v2:
 - Change MQ_MSGBUF_SIZE and MQ_QUEUE_SIZE to be configurable by Kconfig.
---
 Documentation/i2c/slave-mqueue-backend.rst | 125 ++
 drivers/i2c/Kconfig|  25 
 drivers/i2c/Makefile   |   1 +
 drivers/i2c/i2c-slave-mqueue.c | 203 +
 4 files changed, 354 insertions(+)
 create mode 100644 Documentation/i2c/slave-mqueue-backend.rst
 create mode 100644 drivers/i2c/i2c-slave-mqueue.c

diff --git a/Documentation/i2c/slave-mqueue-backend.rst 
b/Documentation/i2c/slave-mqueue-backend.rst
new file mode 100644
index 000..3966cf0
--- /dev/null
+++ b/Documentation/i2c/slave-mqueue-backend.rst
@@ -0,0 +1,125 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=
+Linux I2C slave message queue backend
+=
+
+:Author: Haiyue Wang 
+
+Some protocols over I2C/SMBus are designed for bi-directional transferring
+messages by using I2C Master Write protocol. This requires that both sides
+of the communication have slave addresses.
+
+Like MCTP (Management Component Transport Protocol) and IPMB (Intelligent
+Platform Management Bus), they both require that the userspace can receive
+messages from i2c dirvers under slave mode.
+
+This I2C slave mqueue (message queue) backend is used to receive and queue
+messages from the remote i2c intelligent device; and it will add the target
+slave address (with R/W# bit is always 0) into the message at the first byte,
+so that userspace can use this byte to dispatch the messages into different
+handling modules. Also, like IPMB, the address byte is in its message format,
+it needs it to do checksum.
+
+For messages are time related, so this backend will flush the oldest message
+to queue the newest one.
+
+Link
+
+`Intelligent Platform Management Bus
+Communications Protocol Specification
+`_
+
+`Management Component Transport Protocol (MCTP)
+SMBus/I2C Transport Binding Specification
+`_
+
+How to use
+--
+For example, the I2C5 bus has slave address 0x10, the below command will create
+the related message queue interface:
+
+echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
+
+Then you can dump the messages like this:
+
+hexdump -C /sys/bus/i2c/devices/5-1010/slave-mqueue
+
+Code Example
+
+*Note: call 'lseek' before 'read', this is a requirement from kernfs' design.*
+
+::
+
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+  #include 
+
+  int main(int argc, char *argv[])
+  {
+  int i, r;
+  struct pollfd pfd;
+  struct timespec ts;
+  unsigned char data[256];
+
+  pfd.fd = open(argv[1], O_RDONLY | O_NONBLOCK);
+  if (pfd.fd < 0)
+  return -1;
+
+  pfd.events = POLLPRI;
+
+  while (1) {
+  r = poll(, 1, 5000);
+
+  if (r < 0)
+  break;
+
+  if (r == 0 || !(pfd.revents & POLLPRI))
+  continue;
+
+  lseek(pfd.fd, 0, SEEK_SET);
+  r = read(pfd.fd, data, sizeof(data));
+  if (r <= 0)
+  continue;
+
+  clock_gettime(CLOCK_MONOTONIC, );
+  printf("[%ld.%.9ld] :", ts.tv_sec, ts.tv_nsec);
+  for (i = 0; i < r; i++)
+  printf(" %02x", data[i]);
+  printf("\n");
+  }
+
+  close(pfd.fd);
+
+  return 0;
+  }
+
+Result
+--
+*./a.out "/sys/bus/i2c/devices/5-1010/slave-mqueue"*
+
+::
+
+  [10183.232500449] : 20 18 c8 2c 78 01 5b
+  [10183.479358348] : 20 18 c8 2c 78 01 5b
+  [10183.726556812] : 20 18 c8 2c 78 01 5b
+  [10183.972605863] : 20 18 c8 2c 78 01 5b
+  [10184.220124772] : 20 18 c8 2c 78 01 5b
+  [10184.467764166] : 20 18 c8 2c 78 01 5b
+  [10193.233421784] : 20 18 c8 2c 7c 01 57
+  [10193.480273460] : 20 18 c8 2c 7c 01 57
+  [10193.726788733] : 20 18 c8 2c 7c 01 57
+  [10193.972781945] : 20 18 c8 2c 7c 01 57
+  [10194.220487360] : 20 18 c8 2c 7c 01 57
+