Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Hemant Kumar

Hi Loic,

On 12/1/20 10:04 AM, Jeffrey Hugo wrote:

On 12/1/2020 11:05 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 18:51, Jeffrey Hugo  wrote:


On 12/1/2020 10:52 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 18:37, Jeffrey Hugo  wrote:


On 12/1/2020 10:36 AM, Loic Poulain wrote:
On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  
wrote:


Hi Loic,

On 11/30/20 10:22 AM, Loic Poulain wrote:
On Sat, 28 Nov 2020 at 04:26, Hemant Kumar 
 wrote:


This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file 
operations.
Driver instantiates UCI device object which is associated to 
device
file node. UCI device object instantiates UCI channel object 
when device
file node is opened. UCI channel object is used to manage MHI 
channels
by calling MHI core APIs for read and write operations. MHI 
channels

are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. 
Device

file node is created with format


[...]


+struct uci_chan {
+   struct uci_dev *udev;
+   wait_queue_head_t ul_wq;
+
+   /* ul channel lock to synchronize multiple writes */
+   struct mutex write_lock;
+
+   wait_queue_head_t dl_wq;
+
+   /* dl channel lock to synchronize multiple reads */
+   struct mutex read_lock;
+
+   /*
+    * protects pending list in bh context, channel 
release, read and

+    * poll
+    */
+   spinlock_t dl_pending_lock;
+
+   struct list_head dl_pending;
+   struct uci_buf *cur_buf;
+   size_t dl_size;
+   struct kref ref_count;
+};


[...]


+ * struct uci_dev - MHI UCI device
+ * @minor: UCI device node minor number
+ * @mhi_dev: associated mhi device object
+ * @uchan: UCI uplink and downlink channel object
+ * @mtu: max TRE buffer length
+ * @enabled: Flag to track the state of the UCI device
+ * @lock: mutex lock to manage uchan object
+ * @ref_count: uci_dev reference count
+ */
+struct uci_dev {
+   unsigned int minor;
+   struct mhi_device *mhi_dev;
+   struct uci_chan *uchan;


Why a pointer to uci_chan and not just plainly integrating the
structure here, AFAIU uci_chan describes the channels and is just a
subpart of uci_dev. That would reduce the number of dynamic
allocations you manage and the extra kref. do you even need a 
separate

structure for this?


This goes back to one of my patch versions i tried to address 
concern

from Greg. Since we need to ref count the channel as well as the uci
device i decoupled the two objects and used two reference counts 
for two

different objects.


What Greg complained about is the two kref in the same structure and
that you were using kref as an open() counter. But splitting your
struct in two in order to keep the two kref does not make the much
code better (and simpler). I'm still a bit puzzled about the driver
complexity, it's supposed to be just a passthrough interface to MHI
after all.

I would suggest several changes, that IMHO would simplify reviewing:
- Use only one structure representing the 'uci' context (uci_dev)
- Keep the read path simple (mhi_uci_read), do no use an intermediate
cur_buf pointer, only dequeue the buffer when it is fully consumed.
- As I commented before, take care of the dl_pending list access
concurrency, even in wait_event.
- You don't need to count the number of open() calls, AFAIK,
mhi_prepare_for_transfer() simply fails if channels are already
started...


Unless I missed something, you seem to have ignored the root issue 
that

Hemant needs to solve, which is when to call
mhi_unprepare_for_transfer().  You can't just call that when 
close() is

called because there might be multiple users, and each one is going to
trigger a close(), so you need to know how many close() instances to
expect, and only call mhi_unprepare_for_transfer() for the last one.


That one part of his problem, yes, but if you unconditionally call
mhi_prepare_for_transfer in open(), it should fail for subsequent
users, and so only one user will successfully open the device.


I'm pretty sure that falls under "trying to prevent users from opening a
device" which Greg gave a pretty strong NACK to.  So, that's not a 
solution.


That would deserve clarification since other drivers like
virtio_console clearly prevent that (guest_connected).


Quoting Greg from the source - https://lkml.org/lkml/2020/9/17/873

"
I told you before, do not try to keep a device node from being opened
multiple times, as it will always fail (think about passing file handles
around between programs...)

If userspace wants to do this, it will do it.  If your driver can't
handle that, that's fine, userspace will learn not to do that.  But the
kernel can not prevent this from happening.
"





So, the complete problem is -

N users need to be able to use the device (and by proxy, the channel)
concurrently, but prepare needs to be called on the first user coming
into the picture, and unprepare n

Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Jeffrey Hugo

On 12/1/2020 11:05 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 18:51, Jeffrey Hugo  wrote:


On 12/1/2020 10:52 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 18:37, Jeffrey Hugo  wrote:


On 12/1/2020 10:36 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  wrote:


Hi Loic,

On 11/30/20 10:22 AM, Loic Poulain wrote:

On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:


This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file operations.
Driver instantiates UCI device object which is associated to device
file node. UCI device object instantiates UCI channel object when device
file node is opened. UCI channel object is used to manage MHI channels
by calling MHI core APIs for read and write operations. MHI channels
are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. Device
file node is created with format


[...]


+struct uci_chan {
+   struct uci_dev *udev;
+   wait_queue_head_t ul_wq;
+
+   /* ul channel lock to synchronize multiple writes */
+   struct mutex write_lock;
+
+   wait_queue_head_t dl_wq;
+
+   /* dl channel lock to synchronize multiple reads */
+   struct mutex read_lock;
+
+   /*
+* protects pending list in bh context, channel release, read and
+* poll
+*/
+   spinlock_t dl_pending_lock;
+
+   struct list_head dl_pending;
+   struct uci_buf *cur_buf;
+   size_t dl_size;
+   struct kref ref_count;
+};


[...]


+ * struct uci_dev - MHI UCI device
+ * @minor: UCI device node minor number
+ * @mhi_dev: associated mhi device object
+ * @uchan: UCI uplink and downlink channel object
+ * @mtu: max TRE buffer length
+ * @enabled: Flag to track the state of the UCI device
+ * @lock: mutex lock to manage uchan object
+ * @ref_count: uci_dev reference count
+ */
+struct uci_dev {
+   unsigned int minor;
+   struct mhi_device *mhi_dev;
+   struct uci_chan *uchan;


Why a pointer to uci_chan and not just plainly integrating the
structure here, AFAIU uci_chan describes the channels and is just a
subpart of uci_dev. That would reduce the number of dynamic
allocations you manage and the extra kref. do you even need a separate
structure for this?


This goes back to one of my patch versions i tried to address concern
from Greg. Since we need to ref count the channel as well as the uci
device i decoupled the two objects and used two reference counts for two
different objects.


What Greg complained about is the two kref in the same structure and
that you were using kref as an open() counter. But splitting your
struct in two in order to keep the two kref does not make the much
code better (and simpler). I'm still a bit puzzled about the driver
complexity, it's supposed to be just a passthrough interface to MHI
after all.

I would suggest several changes, that IMHO would simplify reviewing:
- Use only one structure representing the 'uci' context (uci_dev)
- Keep the read path simple (mhi_uci_read), do no use an intermediate
cur_buf pointer, only dequeue the buffer when it is fully consumed.
- As I commented before, take care of the dl_pending list access
concurrency, even in wait_event.
- You don't need to count the number of open() calls, AFAIK,
mhi_prepare_for_transfer() simply fails if channels are already
started...


Unless I missed something, you seem to have ignored the root issue that
Hemant needs to solve, which is when to call
mhi_unprepare_for_transfer().  You can't just call that when close() is
called because there might be multiple users, and each one is going to
trigger a close(), so you need to know how many close() instances to
expect, and only call mhi_unprepare_for_transfer() for the last one.


That one part of his problem, yes, but if you unconditionally call
mhi_prepare_for_transfer in open(), it should fail for subsequent
users, and so only one user will successfully open the device.


I'm pretty sure that falls under "trying to prevent users from opening a
device" which Greg gave a pretty strong NACK to.  So, that's not a solution.


That would deserve clarification since other drivers like
virtio_console clearly prevent that (guest_connected).


Quoting Greg from the source - https://lkml.org/lkml/2020/9/17/873

"
I told you before, do not try to keep a device node from being opened
multiple times, as it will always fail (think about passing file handles
around between programs...)

If userspace wants to do this, it will do it.  If your driver can't
handle that, that's fine, userspace will learn not to do that.  But the
kernel can not prevent this from happening.
"





So, the complete problem is -

N users need to be able to use the device (and by proxy, the channel)
concurrently, but prepare needs to be called on the first user coming
into the picture, and unprepare needs to be called on the last user
exiting the picture.

Hemant has suppo

Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Loic Poulain
On Tue, 1 Dec 2020 at 18:51, Jeffrey Hugo  wrote:
>
> On 12/1/2020 10:52 AM, Loic Poulain wrote:
> > On Tue, 1 Dec 2020 at 18:37, Jeffrey Hugo  wrote:
> >>
> >> On 12/1/2020 10:36 AM, Loic Poulain wrote:
> >>> On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  wrote:
> 
>  Hi Loic,
> 
>  On 11/30/20 10:22 AM, Loic Poulain wrote:
> > On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  
> > wrote:
> >>
> >> This MHI client driver allows userspace clients to transfer
> >> raw data between MHI device and host using standard file operations.
> >> Driver instantiates UCI device object which is associated to device
> >> file node. UCI device object instantiates UCI channel object when 
> >> device
> >> file node is opened. UCI channel object is used to manage MHI channels
> >> by calling MHI core APIs for read and write operations. MHI channels
> >> are started as part of device open(). MHI channels remain in start
> >> state until last release() is called on UCI device file node. Device
> >> file node is created with format
> >
> > [...]
> >
> >> +struct uci_chan {
> >> +   struct uci_dev *udev;
> >> +   wait_queue_head_t ul_wq;
> >> +
> >> +   /* ul channel lock to synchronize multiple writes */
> >> +   struct mutex write_lock;
> >> +
> >> +   wait_queue_head_t dl_wq;
> >> +
> >> +   /* dl channel lock to synchronize multiple reads */
> >> +   struct mutex read_lock;
> >> +
> >> +   /*
> >> +* protects pending list in bh context, channel release, read 
> >> and
> >> +* poll
> >> +*/
> >> +   spinlock_t dl_pending_lock;
> >> +
> >> +   struct list_head dl_pending;
> >> +   struct uci_buf *cur_buf;
> >> +   size_t dl_size;
> >> +   struct kref ref_count;
> >> +};
> >
> > [...]
> >
> >> + * struct uci_dev - MHI UCI device
> >> + * @minor: UCI device node minor number
> >> + * @mhi_dev: associated mhi device object
> >> + * @uchan: UCI uplink and downlink channel object
> >> + * @mtu: max TRE buffer length
> >> + * @enabled: Flag to track the state of the UCI device
> >> + * @lock: mutex lock to manage uchan object
> >> + * @ref_count: uci_dev reference count
> >> + */
> >> +struct uci_dev {
> >> +   unsigned int minor;
> >> +   struct mhi_device *mhi_dev;
> >> +   struct uci_chan *uchan;
> >
> > Why a pointer to uci_chan and not just plainly integrating the
> > structure here, AFAIU uci_chan describes the channels and is just a
> > subpart of uci_dev. That would reduce the number of dynamic
> > allocations you manage and the extra kref. do you even need a separate
> > structure for this?
> 
>  This goes back to one of my patch versions i tried to address concern
>  from Greg. Since we need to ref count the channel as well as the uci
>  device i decoupled the two objects and used two reference counts for two
>  different objects.
> >>>
> >>> What Greg complained about is the two kref in the same structure and
> >>> that you were using kref as an open() counter. But splitting your
> >>> struct in two in order to keep the two kref does not make the much
> >>> code better (and simpler). I'm still a bit puzzled about the driver
> >>> complexity, it's supposed to be just a passthrough interface to MHI
> >>> after all.
> >>>
> >>> I would suggest several changes, that IMHO would simplify reviewing:
> >>> - Use only one structure representing the 'uci' context (uci_dev)
> >>> - Keep the read path simple (mhi_uci_read), do no use an intermediate
> >>> cur_buf pointer, only dequeue the buffer when it is fully consumed.
> >>> - As I commented before, take care of the dl_pending list access
> >>> concurrency, even in wait_event.
> >>> - You don't need to count the number of open() calls, AFAIK,
> >>> mhi_prepare_for_transfer() simply fails if channels are already
> >>> started...
> >>
> >> Unless I missed something, you seem to have ignored the root issue that
> >> Hemant needs to solve, which is when to call
> >> mhi_unprepare_for_transfer().  You can't just call that when close() is
> >> called because there might be multiple users, and each one is going to
> >> trigger a close(), so you need to know how many close() instances to
> >> expect, and only call mhi_unprepare_for_transfer() for the last one.
> >
> > That one part of his problem, yes, but if you unconditionally call
> > mhi_prepare_for_transfer in open(), it should fail for subsequent
> > users, and so only one user will successfully open the device.
>
> I'm pretty sure that falls under "trying to prevent users from opening a
> device" which Greg gave a pretty strong NACK to.  So, that's not a solution.

That would deserve clarification since other drivers like
virtio_console clearly prevent that (guest_connected)

Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Jeffrey Hugo

On 12/1/2020 10:52 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 18:37, Jeffrey Hugo  wrote:


On 12/1/2020 10:36 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  wrote:


Hi Loic,

On 11/30/20 10:22 AM, Loic Poulain wrote:

On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:


This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file operations.
Driver instantiates UCI device object which is associated to device
file node. UCI device object instantiates UCI channel object when device
file node is opened. UCI channel object is used to manage MHI channels
by calling MHI core APIs for read and write operations. MHI channels
are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. Device
file node is created with format


[...]


+struct uci_chan {
+   struct uci_dev *udev;
+   wait_queue_head_t ul_wq;
+
+   /* ul channel lock to synchronize multiple writes */
+   struct mutex write_lock;
+
+   wait_queue_head_t dl_wq;
+
+   /* dl channel lock to synchronize multiple reads */
+   struct mutex read_lock;
+
+   /*
+* protects pending list in bh context, channel release, read and
+* poll
+*/
+   spinlock_t dl_pending_lock;
+
+   struct list_head dl_pending;
+   struct uci_buf *cur_buf;
+   size_t dl_size;
+   struct kref ref_count;
+};


[...]


+ * struct uci_dev - MHI UCI device
+ * @minor: UCI device node minor number
+ * @mhi_dev: associated mhi device object
+ * @uchan: UCI uplink and downlink channel object
+ * @mtu: max TRE buffer length
+ * @enabled: Flag to track the state of the UCI device
+ * @lock: mutex lock to manage uchan object
+ * @ref_count: uci_dev reference count
+ */
+struct uci_dev {
+   unsigned int minor;
+   struct mhi_device *mhi_dev;
+   struct uci_chan *uchan;


Why a pointer to uci_chan and not just plainly integrating the
structure here, AFAIU uci_chan describes the channels and is just a
subpart of uci_dev. That would reduce the number of dynamic
allocations you manage and the extra kref. do you even need a separate
structure for this?


This goes back to one of my patch versions i tried to address concern
from Greg. Since we need to ref count the channel as well as the uci
device i decoupled the two objects and used two reference counts for two
different objects.


What Greg complained about is the two kref in the same structure and
that you were using kref as an open() counter. But splitting your
struct in two in order to keep the two kref does not make the much
code better (and simpler). I'm still a bit puzzled about the driver
complexity, it's supposed to be just a passthrough interface to MHI
after all.

I would suggest several changes, that IMHO would simplify reviewing:
- Use only one structure representing the 'uci' context (uci_dev)
- Keep the read path simple (mhi_uci_read), do no use an intermediate
cur_buf pointer, only dequeue the buffer when it is fully consumed.
- As I commented before, take care of the dl_pending list access
concurrency, even in wait_event.
- You don't need to count the number of open() calls, AFAIK,
mhi_prepare_for_transfer() simply fails if channels are already
started...


Unless I missed something, you seem to have ignored the root issue that
Hemant needs to solve, which is when to call
mhi_unprepare_for_transfer().  You can't just call that when close() is
called because there might be multiple users, and each one is going to
trigger a close(), so you need to know how many close() instances to
expect, and only call mhi_unprepare_for_transfer() for the last one.


That one part of his problem, yes, but if you unconditionally call
mhi_prepare_for_transfer in open(), it should fail for subsequent
users, and so only one user will successfully open the device.


I'm pretty sure that falls under "trying to prevent users from opening a 
device" which Greg gave a pretty strong NACK to.  So, that's not a solution.


So, the complete problem is -

N users need to be able to use the device (and by proxy, the channel) 
concurrently, but prepare needs to be called on the first user coming 
into the picture, and unprepare needs to be called on the last user 
exiting the picture.


Hemant has supported this usecase in every rev of this series I've 
looked at, but apparently every solution he has proposed to handle this 
has caused confusion.


--
Jeffrey Hugo
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.


Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Loic Poulain
On Tue, 1 Dec 2020 at 18:37, Jeffrey Hugo  wrote:
>
> On 12/1/2020 10:36 AM, Loic Poulain wrote:
> > On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  wrote:
> >>
> >> Hi Loic,
> >>
> >> On 11/30/20 10:22 AM, Loic Poulain wrote:
> >>> On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:
> 
>  This MHI client driver allows userspace clients to transfer
>  raw data between MHI device and host using standard file operations.
>  Driver instantiates UCI device object which is associated to device
>  file node. UCI device object instantiates UCI channel object when device
>  file node is opened. UCI channel object is used to manage MHI channels
>  by calling MHI core APIs for read and write operations. MHI channels
>  are started as part of device open(). MHI channels remain in start
>  state until last release() is called on UCI device file node. Device
>  file node is created with format
> >>>
> >>> [...]
> >>>
>  +struct uci_chan {
>  +   struct uci_dev *udev;
>  +   wait_queue_head_t ul_wq;
>  +
>  +   /* ul channel lock to synchronize multiple writes */
>  +   struct mutex write_lock;
>  +
>  +   wait_queue_head_t dl_wq;
>  +
>  +   /* dl channel lock to synchronize multiple reads */
>  +   struct mutex read_lock;
>  +
>  +   /*
>  +* protects pending list in bh context, channel release, read and
>  +* poll
>  +*/
>  +   spinlock_t dl_pending_lock;
>  +
>  +   struct list_head dl_pending;
>  +   struct uci_buf *cur_buf;
>  +   size_t dl_size;
>  +   struct kref ref_count;
>  +};
> >>>
> >>> [...]
> >>>
>  + * struct uci_dev - MHI UCI device
>  + * @minor: UCI device node minor number
>  + * @mhi_dev: associated mhi device object
>  + * @uchan: UCI uplink and downlink channel object
>  + * @mtu: max TRE buffer length
>  + * @enabled: Flag to track the state of the UCI device
>  + * @lock: mutex lock to manage uchan object
>  + * @ref_count: uci_dev reference count
>  + */
>  +struct uci_dev {
>  +   unsigned int minor;
>  +   struct mhi_device *mhi_dev;
>  +   struct uci_chan *uchan;
> >>>
> >>> Why a pointer to uci_chan and not just plainly integrating the
> >>> structure here, AFAIU uci_chan describes the channels and is just a
> >>> subpart of uci_dev. That would reduce the number of dynamic
> >>> allocations you manage and the extra kref. do you even need a separate
> >>> structure for this?
> >>
> >> This goes back to one of my patch versions i tried to address concern
> >> from Greg. Since we need to ref count the channel as well as the uci
> >> device i decoupled the two objects and used two reference counts for two
> >> different objects.
> >
> > What Greg complained about is the two kref in the same structure and
> > that you were using kref as an open() counter. But splitting your
> > struct in two in order to keep the two kref does not make the much
> > code better (and simpler). I'm still a bit puzzled about the driver
> > complexity, it's supposed to be just a passthrough interface to MHI
> > after all.
> >
> > I would suggest several changes, that IMHO would simplify reviewing:
> > - Use only one structure representing the 'uci' context (uci_dev)
> > - Keep the read path simple (mhi_uci_read), do no use an intermediate
> > cur_buf pointer, only dequeue the buffer when it is fully consumed.
> > - As I commented before, take care of the dl_pending list access
> > concurrency, even in wait_event.
> > - You don't need to count the number of open() calls, AFAIK,
> > mhi_prepare_for_transfer() simply fails if channels are already
> > started...
>
> Unless I missed something, you seem to have ignored the root issue that
> Hemant needs to solve, which is when to call
> mhi_unprepare_for_transfer().  You can't just call that when close() is
> called because there might be multiple users, and each one is going to
> trigger a close(), so you need to know how many close() instances to
> expect, and only call mhi_unprepare_for_transfer() for the last one.

That one part of his problem, yes, but if you unconditionally call
mhi_prepare_for_transfer in open(), it should fail for subsequent
users, and so only one user will successfully open the device.

Regards,
Loic


Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Jeffrey Hugo

On 12/1/2020 10:36 AM, Loic Poulain wrote:

On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  wrote:


Hi Loic,

On 11/30/20 10:22 AM, Loic Poulain wrote:

On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:


This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file operations.
Driver instantiates UCI device object which is associated to device
file node. UCI device object instantiates UCI channel object when device
file node is opened. UCI channel object is used to manage MHI channels
by calling MHI core APIs for read and write operations. MHI channels
are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. Device
file node is created with format


[...]


+struct uci_chan {
+   struct uci_dev *udev;
+   wait_queue_head_t ul_wq;
+
+   /* ul channel lock to synchronize multiple writes */
+   struct mutex write_lock;
+
+   wait_queue_head_t dl_wq;
+
+   /* dl channel lock to synchronize multiple reads */
+   struct mutex read_lock;
+
+   /*
+* protects pending list in bh context, channel release, read and
+* poll
+*/
+   spinlock_t dl_pending_lock;
+
+   struct list_head dl_pending;
+   struct uci_buf *cur_buf;
+   size_t dl_size;
+   struct kref ref_count;
+};


[...]


+ * struct uci_dev - MHI UCI device
+ * @minor: UCI device node minor number
+ * @mhi_dev: associated mhi device object
+ * @uchan: UCI uplink and downlink channel object
+ * @mtu: max TRE buffer length
+ * @enabled: Flag to track the state of the UCI device
+ * @lock: mutex lock to manage uchan object
+ * @ref_count: uci_dev reference count
+ */
+struct uci_dev {
+   unsigned int minor;
+   struct mhi_device *mhi_dev;
+   struct uci_chan *uchan;


Why a pointer to uci_chan and not just plainly integrating the
structure here, AFAIU uci_chan describes the channels and is just a
subpart of uci_dev. That would reduce the number of dynamic
allocations you manage and the extra kref. do you even need a separate
structure for this?


This goes back to one of my patch versions i tried to address concern
from Greg. Since we need to ref count the channel as well as the uci
device i decoupled the two objects and used two reference counts for two
different objects.


What Greg complained about is the two kref in the same structure and
that you were using kref as an open() counter. But splitting your
struct in two in order to keep the two kref does not make the much
code better (and simpler). I'm still a bit puzzled about the driver
complexity, it's supposed to be just a passthrough interface to MHI
after all.

I would suggest several changes, that IMHO would simplify reviewing:
- Use only one structure representing the 'uci' context (uci_dev)
- Keep the read path simple (mhi_uci_read), do no use an intermediate
cur_buf pointer, only dequeue the buffer when it is fully consumed.
- As I commented before, take care of the dl_pending list access
concurrency, even in wait_event.
- You don't need to count the number of open() calls, AFAIK,
mhi_prepare_for_transfer() simply fails if channels are already
started...


Unless I missed something, you seem to have ignored the root issue that 
Hemant needs to solve, which is when to call 
mhi_unprepare_for_transfer().  You can't just call that when close() is 
called because there might be multiple users, and each one is going to 
trigger a close(), so you need to know how many close() instances to 
expect, and only call mhi_unprepare_for_transfer() for the last one.


--
Jeffrey Hugo
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.


Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-12-01 Thread Loic Poulain
On Tue, 1 Dec 2020 at 02:16, Hemant Kumar  wrote:
>
> Hi Loic,
>
> On 11/30/20 10:22 AM, Loic Poulain wrote:
> > On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:
> >>
> >> This MHI client driver allows userspace clients to transfer
> >> raw data between MHI device and host using standard file operations.
> >> Driver instantiates UCI device object which is associated to device
> >> file node. UCI device object instantiates UCI channel object when device
> >> file node is opened. UCI channel object is used to manage MHI channels
> >> by calling MHI core APIs for read and write operations. MHI channels
> >> are started as part of device open(). MHI channels remain in start
> >> state until last release() is called on UCI device file node. Device
> >> file node is created with format
> >
> > [...]
> >
> >> +struct uci_chan {
> >> +   struct uci_dev *udev;
> >> +   wait_queue_head_t ul_wq;
> >> +
> >> +   /* ul channel lock to synchronize multiple writes */
> >> +   struct mutex write_lock;
> >> +
> >> +   wait_queue_head_t dl_wq;
> >> +
> >> +   /* dl channel lock to synchronize multiple reads */
> >> +   struct mutex read_lock;
> >> +
> >> +   /*
> >> +* protects pending list in bh context, channel release, read and
> >> +* poll
> >> +*/
> >> +   spinlock_t dl_pending_lock;
> >> +
> >> +   struct list_head dl_pending;
> >> +   struct uci_buf *cur_buf;
> >> +   size_t dl_size;
> >> +   struct kref ref_count;
> >> +};
> >
> > [...]
> >
> >> + * struct uci_dev - MHI UCI device
> >> + * @minor: UCI device node minor number
> >> + * @mhi_dev: associated mhi device object
> >> + * @uchan: UCI uplink and downlink channel object
> >> + * @mtu: max TRE buffer length
> >> + * @enabled: Flag to track the state of the UCI device
> >> + * @lock: mutex lock to manage uchan object
> >> + * @ref_count: uci_dev reference count
> >> + */
> >> +struct uci_dev {
> >> +   unsigned int minor;
> >> +   struct mhi_device *mhi_dev;
> >> +   struct uci_chan *uchan;
> >
> > Why a pointer to uci_chan and not just plainly integrating the
> > structure here, AFAIU uci_chan describes the channels and is just a
> > subpart of uci_dev. That would reduce the number of dynamic
> > allocations you manage and the extra kref. do you even need a separate
> > structure for this?
>
> This goes back to one of my patch versions i tried to address concern
> from Greg. Since we need to ref count the channel as well as the uci
> device i decoupled the two objects and used two reference counts for two
> different objects.

What Greg complained about is the two kref in the same structure and
that you were using kref as an open() counter. But splitting your
struct in two in order to keep the two kref does not make the much
code better (and simpler). I'm still a bit puzzled about the driver
complexity, it's supposed to be just a passthrough interface to MHI
after all.

I would suggest several changes, that IMHO would simplify reviewing:
- Use only one structure representing the 'uci' context (uci_dev)
- Keep the read path simple (mhi_uci_read), do no use an intermediate
cur_buf pointer, only dequeue the buffer when it is fully consumed.
- As I commented before, take care of the dl_pending list access
concurrency, even in wait_event.
- You don't need to count the number of open() calls, AFAIK,
mhi_prepare_for_transfer() simply fails if channels are already
started...

For testing purpose, I've implemented those changes on my side (based
on your driver):
https://git.linaro.org/landing-teams/working/telit/linux.git/commit/?h=mhi_uci_test&id=45ff60703cc26913061a26260e39cf3ab3e57c2b

Feel free to pick (or not), I'm not going to 'block' that series if
others are fine with the current implementation.

Anyway, I've tested your series and it works on my side with
libqmi/qmicli (controlling SDX55 modem):
Tested-by: Loic Poulain 

Regards,
Loic


Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-11-30 Thread Hemant Kumar

Hi Loic,

On 11/30/20 10:22 AM, Loic Poulain wrote:

On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:


This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file operations.
Driver instantiates UCI device object which is associated to device
file node. UCI device object instantiates UCI channel object when device
file node is opened. UCI channel object is used to manage MHI channels
by calling MHI core APIs for read and write operations. MHI channels
are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. Device
file node is created with format


[...]


+struct uci_chan {
+   struct uci_dev *udev;
+   wait_queue_head_t ul_wq;
+
+   /* ul channel lock to synchronize multiple writes */
+   struct mutex write_lock;
+
+   wait_queue_head_t dl_wq;
+
+   /* dl channel lock to synchronize multiple reads */
+   struct mutex read_lock;
+
+   /*
+* protects pending list in bh context, channel release, read and
+* poll
+*/
+   spinlock_t dl_pending_lock;
+
+   struct list_head dl_pending;
+   struct uci_buf *cur_buf;
+   size_t dl_size;
+   struct kref ref_count;
+};


[...]


+ * struct uci_dev - MHI UCI device
+ * @minor: UCI device node minor number
+ * @mhi_dev: associated mhi device object
+ * @uchan: UCI uplink and downlink channel object
+ * @mtu: max TRE buffer length
+ * @enabled: Flag to track the state of the UCI device
+ * @lock: mutex lock to manage uchan object
+ * @ref_count: uci_dev reference count
+ */
+struct uci_dev {
+   unsigned int minor;
+   struct mhi_device *mhi_dev;
+   struct uci_chan *uchan;


Why a pointer to uci_chan and not just plainly integrating the
structure here, AFAIU uci_chan describes the channels and is just a
subpart of uci_dev. That would reduce the number of dynamic
allocations you manage and the extra kref. do you even need a separate
structure for this?


This goes back to one of my patch versions i tried to address concern 
from Greg. Since we need to ref count the channel as well as the uci 
device i decoupled the two objects and used two reference counts for two 
different objects.


Copying from V7 patch history

V7:
- Decoupled uci device and uci channel objects. uci device is
  associated with device file node. uci channel is associated
  with MHI channels. uci device refers to uci channel to perform
  MHI channel operations for device file operations like read()
  and write(). uci device increments its reference count for
  every open(). uci device calls mhi_uci_dev_start_chan() to start
  the MHI channel. uci channel object is tracking number of times
  MHI channel is referred. This allows to keep the MHI channel in
  start state until last release() is called. After that uci channel
  reference count goes to 0 and uci channel clean up is performed
  which stops the MHI channel. After the last call to release() if
  driver is removed uci reference count becomes 0 and uci object is
  cleaned up.



[...]


+static int mhi_uci_dev_start_chan(struct uci_dev *udev)
+{
+   int ret = 0;
+   struct uci_chan *uchan;
+
+   mutex_lock(&udev->lock);
+   if (!udev->uchan || !kref_get_unless_zero(&udev->uchan->ref_count)) {


This test is suspicious,  kref_get_unless_zero should be enough to test, right?
kref_get_unless_zero is de-referencing uchan->ref_count for the first 
open uchan is set to NULL, for that NULL check is added for uchan.


if (kref_get_unless_zero(&udev->ref))
 goto skip_init;


+   uchan = kzalloc(sizeof(*uchan), GFP_KERNEL);
+   if (!uchan) {
+   ret = -ENOMEM;
+   goto error_chan_start;
+   }
+
+   udev->uchan = uchan;
+   uchan->udev = udev;
+   init_waitqueue_head(&uchan->ul_wq);
+   init_waitqueue_head(&uchan->dl_wq);
+   mutex_init(&uchan->write_lock);
+   mutex_init(&uchan->read_lock);
+   spin_lock_init(&uchan->dl_pending_lock);
+   INIT_LIST_HEAD(&uchan->dl_pending);
+
+   ret = mhi_prepare_for_transfer(udev->mhi_dev);
+   if (ret) {
+   dev_err(&udev->mhi_dev->dev, "Error starting transfer 
channels\n");
+   goto error_chan_cleanup;
+   }
+
+   ret = mhi_queue_inbound(udev);
+   if (ret)
+   goto error_chan_cleanup;
+
+   kref_init(&uchan->ref_count);
+   }
+
+   mutex_unlock(&udev->lock);
+
+   return 0;
+
+error_chan_cleanup:
+   mhi_uci_dev_chan_release(&uchan->ref_count);
+error_chan_start:
+   mutex_unlock(&udev->lock);
+   return ret;
+}


Regards,
Loic



Thanks,
Hemant
--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative P

Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-11-30 Thread Hemant Kumar

Hi Mani,

On 11/27/20 10:11 PM, Manivannan Sadhasivam wrote:

Hi Hemant,

On Fri, Nov 27, 2020 at 07:26:06PM -0800, Hemant Kumar wrote:

This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file operations.
Driver instantiates UCI device object which is associated to device
file node. UCI device object instantiates UCI channel object when device
file node is opened. UCI channel object is used to manage MHI channels
by calling MHI core APIs for read and write operations. MHI channels
are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. Device
file node is created with format

/dev/mhi_

Currently it supports QMI channel.



Thanks for the update. This patch looks good to me. But as I'm going to
apply Loic's "bus: mhi: core: Indexed MHI controller name" patch, you
need to update the documentation accordingly.


This is what i added in documentation on v13


Device file node is created with format:-

/dev/mhi_

mhi_device_name includes mhi controller name and the name of the MHI
channel being used by MHI client in userspace to send or receive data
using MHI protocol.

​
Loic's patch is going to update the controller name as indexed 
controller name, which goes fine with or without his change going first.


For example:   With Loic's change name of device node would be 
/dev/mhi_mhi0_QMI


Without Loic's change it would be

/dev/mhi_:00:01.2_QMI

Please let me know if i am missing something.

Thanks,
Hemant




Signed-off-by: Hemant Kumar 


Reviewed-by: Manivannan Sadhasivam 

Thanks,
Mani


[..]
--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project


Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-11-30 Thread Loic Poulain
On Sat, 28 Nov 2020 at 04:26, Hemant Kumar  wrote:
>
> This MHI client driver allows userspace clients to transfer
> raw data between MHI device and host using standard file operations.
> Driver instantiates UCI device object which is associated to device
> file node. UCI device object instantiates UCI channel object when device
> file node is opened. UCI channel object is used to manage MHI channels
> by calling MHI core APIs for read and write operations. MHI channels
> are started as part of device open(). MHI channels remain in start
> state until last release() is called on UCI device file node. Device
> file node is created with format

[...]

> +struct uci_chan {
> +   struct uci_dev *udev;
> +   wait_queue_head_t ul_wq;
> +
> +   /* ul channel lock to synchronize multiple writes */
> +   struct mutex write_lock;
> +
> +   wait_queue_head_t dl_wq;
> +
> +   /* dl channel lock to synchronize multiple reads */
> +   struct mutex read_lock;
> +
> +   /*
> +* protects pending list in bh context, channel release, read and
> +* poll
> +*/
> +   spinlock_t dl_pending_lock;
> +
> +   struct list_head dl_pending;
> +   struct uci_buf *cur_buf;
> +   size_t dl_size;
> +   struct kref ref_count;
> +};

[...]

> + * struct uci_dev - MHI UCI device
> + * @minor: UCI device node minor number
> + * @mhi_dev: associated mhi device object
> + * @uchan: UCI uplink and downlink channel object
> + * @mtu: max TRE buffer length
> + * @enabled: Flag to track the state of the UCI device
> + * @lock: mutex lock to manage uchan object
> + * @ref_count: uci_dev reference count
> + */
> +struct uci_dev {
> +   unsigned int minor;
> +   struct mhi_device *mhi_dev;
> +   struct uci_chan *uchan;

Why a pointer to uci_chan and not just plainly integrating the
structure here, AFAIU uci_chan describes the channels and is just a
subpart of uci_dev. That would reduce the number of dynamic
allocations you manage and the extra kref. do you even need a separate
structure for this?

[...]

> +static int mhi_uci_dev_start_chan(struct uci_dev *udev)
> +{
> +   int ret = 0;
> +   struct uci_chan *uchan;
> +
> +   mutex_lock(&udev->lock);
> +   if (!udev->uchan || !kref_get_unless_zero(&udev->uchan->ref_count)) {

This test is suspicious,  kref_get_unless_zero should be enough to test, right?

if (kref_get_unless_zero(&udev->ref))
goto skip_init;

> +   uchan = kzalloc(sizeof(*uchan), GFP_KERNEL);
> +   if (!uchan) {
> +   ret = -ENOMEM;
> +   goto error_chan_start;
> +   }
> +
> +   udev->uchan = uchan;
> +   uchan->udev = udev;
> +   init_waitqueue_head(&uchan->ul_wq);
> +   init_waitqueue_head(&uchan->dl_wq);
> +   mutex_init(&uchan->write_lock);
> +   mutex_init(&uchan->read_lock);
> +   spin_lock_init(&uchan->dl_pending_lock);
> +   INIT_LIST_HEAD(&uchan->dl_pending);
> +
> +   ret = mhi_prepare_for_transfer(udev->mhi_dev);
> +   if (ret) {
> +   dev_err(&udev->mhi_dev->dev, "Error starting transfer 
> channels\n");
> +   goto error_chan_cleanup;
> +   }
> +
> +   ret = mhi_queue_inbound(udev);
> +   if (ret)
> +   goto error_chan_cleanup;
> +
> +   kref_init(&uchan->ref_count);
> +   }
> +
> +   mutex_unlock(&udev->lock);
> +
> +   return 0;
> +
> +error_chan_cleanup:
> +   mhi_uci_dev_chan_release(&uchan->ref_count);
> +error_chan_start:
> +   mutex_unlock(&udev->lock);
> +   return ret;
> +}

Regards,
Loic


Re: [PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-11-28 Thread Manivannan Sadhasivam
Hi Hemant,

On Fri, Nov 27, 2020 at 07:26:06PM -0800, Hemant Kumar wrote:
> This MHI client driver allows userspace clients to transfer
> raw data between MHI device and host using standard file operations.
> Driver instantiates UCI device object which is associated to device
> file node. UCI device object instantiates UCI channel object when device
> file node is opened. UCI channel object is used to manage MHI channels
> by calling MHI core APIs for read and write operations. MHI channels
> are started as part of device open(). MHI channels remain in start
> state until last release() is called on UCI device file node. Device
> file node is created with format
> 
> /dev/mhi_
> 
> Currently it supports QMI channel.
> 

Thanks for the update. This patch looks good to me. But as I'm going to
apply Loic's "bus: mhi: core: Indexed MHI controller name" patch, you
need to update the documentation accordingly.

> Signed-off-by: Hemant Kumar 

Reviewed-by: Manivannan Sadhasivam 

Thanks,
Mani

> ---
>  drivers/bus/mhi/Kconfig  |  13 +
>  drivers/bus/mhi/Makefile |   3 +
>  drivers/bus/mhi/uci.c| 665 
> +++
>  3 files changed, 681 insertions(+)
>  create mode 100644 drivers/bus/mhi/uci.c
> 
> diff --git a/drivers/bus/mhi/Kconfig b/drivers/bus/mhi/Kconfig
> index da5cd0c..5194e8e 100644
> --- a/drivers/bus/mhi/Kconfig
> +++ b/drivers/bus/mhi/Kconfig
> @@ -29,3 +29,16 @@ config MHI_BUS_PCI_GENERIC
> This driver provides MHI PCI controller driver for devices such as
> Qualcomm SDX55 based PCIe modems.
>  
> +config MHI_UCI
> + tristate "MHI UCI"
> + depends on MHI_BUS
> + help
> +   MHI based Userspace Client Interface (UCI) driver is used for
> +   transferring raw data between host and device using standard file
> +   operations from userspace. Open, read, write, poll and close
> +   operations are supported by this driver. Please check
> +   mhi_uci_match_table for all supported channels that are exposed to
> +   userspace.
> +
> +   To compile this driver as a module, choose M here: the module will be
> +   called mhi_uci.
> diff --git a/drivers/bus/mhi/Makefile b/drivers/bus/mhi/Makefile
> index 0a2d778..69f2111 100644
> --- a/drivers/bus/mhi/Makefile
> +++ b/drivers/bus/mhi/Makefile
> @@ -4,3 +4,6 @@ obj-y += core/
>  obj-$(CONFIG_MHI_BUS_PCI_GENERIC) += mhi_pci_generic.o
>  mhi_pci_generic-y += pci_generic.o
>  
> +# MHI client
> +mhi_uci-y := uci.o
> +obj-$(CONFIG_MHI_UCI) += mhi_uci.o
> diff --git a/drivers/bus/mhi/uci.c b/drivers/bus/mhi/uci.c
> new file mode 100644
> index 000..fb9c183
> --- /dev/null
> +++ b/drivers/bus/mhi/uci.c
> @@ -0,0 +1,665 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.*/
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define MHI_DEVICE_NAME "mhi"
> +#define MHI_UCI_DRIVER_NAME "mhi_uci"
> +#define MHI_MAX_UCI_MINORS 128
> +
> +static DEFINE_IDR(uci_idr);
> +static DEFINE_MUTEX(uci_drv_mutex);
> +static struct class *uci_dev_class;
> +static int uci_dev_major;
> +
> +/**
> + * struct uci_chan - MHI channel for a UCI device
> + * @udev: associated UCI device object
> + * @ul_wq: wait queue for writer
> + * @write_lock: mutex write lock for ul channel
> + * @dl_wq: wait queue for reader
> + * @read_lock: mutex read lock for dl channel
> + * @dl_pending_lock: spin lock for dl_pending list
> + * @dl_pending: list of dl buffers userspace is waiting to read
> + * @cur_buf: current buffer userspace is reading
> + * @dl_size: size of the current dl buffer userspace is reading
> + * @ref_count: uci_chan reference count
> + */
> +struct uci_chan {
> + struct uci_dev *udev;
> + wait_queue_head_t ul_wq;
> +
> + /* ul channel lock to synchronize multiple writes */
> + struct mutex write_lock;
> +
> + wait_queue_head_t dl_wq;
> +
> + /* dl channel lock to synchronize multiple reads */
> + struct mutex read_lock;
> +
> + /*
> +  * protects pending list in bh context, channel release, read and
> +  * poll
> +  */
> + spinlock_t dl_pending_lock;
> +
> + struct list_head dl_pending;
> + struct uci_buf *cur_buf;
> + size_t dl_size;
> + struct kref ref_count;
> +};
> +
> +/**
> + * struct uci_buf - UCI buffer
> + * @data: data buffer
> + * @len: length of data buffer
> + * @node: list node of the UCI buffer
> + */
> +struct uci_buf {
> + void *data;
> + size_t len;
> + struct list_head node;
> +};
> +
> +/**
> + * struct uci_dev - MHI UCI device
> + * @minor: UCI device node minor number
> + * @mhi_dev: associated mhi device object
> + * @uchan: UCI uplink and downlink channel object
> + * @mtu: max TRE buffer length
> + * @enabled: Flag to track the state of the UCI device
> + * @lock: mutex lock to manage uchan object
> + * @ref_count: uci_dev reference count
> + */
> +struct uci_dev {
> + unsigned int minor;
>

[PATCH v13 4/4] bus: mhi: Add userspace client interface driver

2020-11-27 Thread Hemant Kumar
This MHI client driver allows userspace clients to transfer
raw data between MHI device and host using standard file operations.
Driver instantiates UCI device object which is associated to device
file node. UCI device object instantiates UCI channel object when device
file node is opened. UCI channel object is used to manage MHI channels
by calling MHI core APIs for read and write operations. MHI channels
are started as part of device open(). MHI channels remain in start
state until last release() is called on UCI device file node. Device
file node is created with format

/dev/mhi_

Currently it supports QMI channel.

Signed-off-by: Hemant Kumar 
---
 drivers/bus/mhi/Kconfig  |  13 +
 drivers/bus/mhi/Makefile |   3 +
 drivers/bus/mhi/uci.c| 665 +++
 3 files changed, 681 insertions(+)
 create mode 100644 drivers/bus/mhi/uci.c

diff --git a/drivers/bus/mhi/Kconfig b/drivers/bus/mhi/Kconfig
index da5cd0c..5194e8e 100644
--- a/drivers/bus/mhi/Kconfig
+++ b/drivers/bus/mhi/Kconfig
@@ -29,3 +29,16 @@ config MHI_BUS_PCI_GENERIC
  This driver provides MHI PCI controller driver for devices such as
  Qualcomm SDX55 based PCIe modems.
 
+config MHI_UCI
+   tristate "MHI UCI"
+   depends on MHI_BUS
+   help
+ MHI based Userspace Client Interface (UCI) driver is used for
+ transferring raw data between host and device using standard file
+ operations from userspace. Open, read, write, poll and close
+ operations are supported by this driver. Please check
+ mhi_uci_match_table for all supported channels that are exposed to
+ userspace.
+
+ To compile this driver as a module, choose M here: the module will be
+ called mhi_uci.
diff --git a/drivers/bus/mhi/Makefile b/drivers/bus/mhi/Makefile
index 0a2d778..69f2111 100644
--- a/drivers/bus/mhi/Makefile
+++ b/drivers/bus/mhi/Makefile
@@ -4,3 +4,6 @@ obj-y += core/
 obj-$(CONFIG_MHI_BUS_PCI_GENERIC) += mhi_pci_generic.o
 mhi_pci_generic-y += pci_generic.o
 
+# MHI client
+mhi_uci-y := uci.o
+obj-$(CONFIG_MHI_UCI) += mhi_uci.o
diff --git a/drivers/bus/mhi/uci.c b/drivers/bus/mhi/uci.c
new file mode 100644
index 000..fb9c183
--- /dev/null
+++ b/drivers/bus/mhi/uci.c
@@ -0,0 +1,665 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.*/
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MHI_DEVICE_NAME "mhi"
+#define MHI_UCI_DRIVER_NAME "mhi_uci"
+#define MHI_MAX_UCI_MINORS 128
+
+static DEFINE_IDR(uci_idr);
+static DEFINE_MUTEX(uci_drv_mutex);
+static struct class *uci_dev_class;
+static int uci_dev_major;
+
+/**
+ * struct uci_chan - MHI channel for a UCI device
+ * @udev: associated UCI device object
+ * @ul_wq: wait queue for writer
+ * @write_lock: mutex write lock for ul channel
+ * @dl_wq: wait queue for reader
+ * @read_lock: mutex read lock for dl channel
+ * @dl_pending_lock: spin lock for dl_pending list
+ * @dl_pending: list of dl buffers userspace is waiting to read
+ * @cur_buf: current buffer userspace is reading
+ * @dl_size: size of the current dl buffer userspace is reading
+ * @ref_count: uci_chan reference count
+ */
+struct uci_chan {
+   struct uci_dev *udev;
+   wait_queue_head_t ul_wq;
+
+   /* ul channel lock to synchronize multiple writes */
+   struct mutex write_lock;
+
+   wait_queue_head_t dl_wq;
+
+   /* dl channel lock to synchronize multiple reads */
+   struct mutex read_lock;
+
+   /*
+* protects pending list in bh context, channel release, read and
+* poll
+*/
+   spinlock_t dl_pending_lock;
+
+   struct list_head dl_pending;
+   struct uci_buf *cur_buf;
+   size_t dl_size;
+   struct kref ref_count;
+};
+
+/**
+ * struct uci_buf - UCI buffer
+ * @data: data buffer
+ * @len: length of data buffer
+ * @node: list node of the UCI buffer
+ */
+struct uci_buf {
+   void *data;
+   size_t len;
+   struct list_head node;
+};
+
+/**
+ * struct uci_dev - MHI UCI device
+ * @minor: UCI device node minor number
+ * @mhi_dev: associated mhi device object
+ * @uchan: UCI uplink and downlink channel object
+ * @mtu: max TRE buffer length
+ * @enabled: Flag to track the state of the UCI device
+ * @lock: mutex lock to manage uchan object
+ * @ref_count: uci_dev reference count
+ */
+struct uci_dev {
+   unsigned int minor;
+   struct mhi_device *mhi_dev;
+   struct uci_chan *uchan;
+   size_t mtu;
+   bool enabled;
+
+   /* synchronize open, release and driver remove */
+   struct mutex lock;
+   struct kref ref_count;
+};
+
+static void mhi_uci_dev_chan_release(struct kref *ref)
+{
+   struct uci_buf *buf_itr, *tmp;
+   struct uci_chan *uchan =
+   container_of(ref, struct uci_chan, ref_count);
+
+   if (uchan->udev->enabled)
+   mhi_unprepare_from_transfer(uchan->udev->mhi_dev);
+