Skip to contentSkip to conversation

userspace MHI client interface driver

PATCH Superseded Hemant Kumar 4 participants 13 messages Oct 23, 2020 – Oct 27, 2020 1 message not in the archive

Subsystems (5)

v9 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v9 cover userspace MHI client interface driver

This patch series adds support for UCI driver. UCI driver enables userspace clients to communicate to external MHI devices like modem and WLAN. UCI driver probe creates standard character device file nodes for userspace clients to perform open, read, write, poll and release file operations. These file operations call MHI core layer APIs to perform data transfer using MHI bus to communicate with MHI device. Patch is tested using arm64 based platform.

V9:
- Renamed dl_lock to dl_pending _lock and pending list to dl_pending for clarity.
- Used read lock to protect cur_buf.
- Change transfer status check logic and only consider 0 and -EOVERFLOW as only success.
- Added __int to module init function.
- Print channel name instead of minor number upon successful probe.

V8:
- Fixed kernel test robot compilation error by changing %lu to %zu for size_t.
- Replaced uci with UCI in Kconfig, commit text, and comments in driver code.
- Fixed minor style related comments.

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.
- Use separate uci channel read and write lock to fine grain locking between reader and writer.
- Use uci device lock to synchronize open, release and driver remove.
- Optimize for downlink only or uplink only UCI device.

V6:
- Moved uci.c to mhi directory.
- Updated Kconfig to add module information.
- Updated Makefile to rename uci object file name as mhi_uci
- Removed kref for open count

V5:
- Removed mhi_uci_drv structure.
- Used idr instead of creating global list of uci devices.
- Used kref instead of local ref counting for uci device and open count.
- Removed unlikely macro.

V4:
- Fix locking to protect proper struct members.
- Updated documentation describing uci client driver use cases.
- Fixed uci ref counting in mhi_uci_open for error case.
- Addressed style related review comments.

V3: Added documentation for MHI UCI driver.

V2: Added mutex lock to prevent multiple readers to access same mhi buffer which can result into use after free.

Hemant Kumar (4):
bus: mhi: core: Add helper API to return number of free TREs
bus: mhi: core: Move MHI_MAX_MTU to external header file
docs: Add documentation for userspace client interface
bus: mhi: Add userspace client interface driver

 Documentation/mhi/index.rst     |   1 +
 Documentation/mhi/uci.rst       |  83 +++++
 drivers/bus/mhi/Kconfig         |  13 +
 drivers/bus/mhi/Makefile        |   4 +
 drivers/bus/mhi/core/internal.h |   1 -
 drivers/bus/mhi/core/main.c     |  12 +
 drivers/bus/mhi/uci.c           | 658 ++++++++++++++++++++++++++++++++++++++++
 include/linux/mhi.h             |  12 +
 8 files changed, 783 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/mhi/uci.rst
 create mode 100644 drivers/bus/mhi/uci.c
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw

PATCH v9 1/4 bus: mhi: core: Add helper API to return number of free TREs

Introduce mhi_get_free_desc_count() API to return number of TREs available to queue buffer. MHI clients can use this API to know before hand if ring is full without calling queue API.

---
 drivers/bus/mhi/core/main.c | 12 ++++++++++++
 include/linux/mhi.h         |  9 +++++++++
 2 files changed, 21 insertions(+)
drivers/bus/mhi/core/main.c +12 -0
index 2cff5dd..3950792 100644
@@ -258,6 +258,18 @@ int mhi_destroy_device(struct device *dev, void *data)
 	return 0;
 }
 
+int mhi_get_free_desc_count(struct mhi_device *mhi_dev,
+				enum dma_data_direction dir)
+{
+	struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl;
+	struct mhi_chan *mhi_chan = (dir == DMA_TO_DEVICE) ?
+		mhi_dev->ul_chan : mhi_dev->dl_chan;
+	struct mhi_ring *tre_ring = &mhi_chan->tre_ring;
+
+	return get_nr_avail_ring_elements(mhi_cntrl, tre_ring);
+}
+EXPORT_SYMBOL_GPL(mhi_get_free_desc_count);
+
 void mhi_notify(struct mhi_device *mhi_dev, enum mhi_callback cb_reason)
 {
 	struct mhi_driver *mhi_drv;
include/linux/mhi.h +9 -0
index d4841e5..7829b1d 100644
@@ -597,6 +597,15 @@ void mhi_set_mhi_state(struct mhi_controller *mhi_cntrl,
 void mhi_notify(struct mhi_device *mhi_dev, enum mhi_callback cb_reason);
 
 /**
+ * mhi_get_free_desc_count - Get transfer ring length
+ * Get # of TD available to queue buffers
+ * @mhi_dev: Device associated with the channels
+ * @dir: Direction of the channel
+ */
+int mhi_get_free_desc_count(struct mhi_device *mhi_dev,
+				enum dma_data_direction dir);
+
+/**
  * mhi_prepare_for_power_up - Do pre-initialization before power up.
  *                            This is optional, call this before power up if
  *                            the controller does not want bus framework to
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

PATCH v9 2/4 bus: mhi: core: Move MHI_MAX_MTU to external header file

In reply to Hemant Kumar

Currently this macro is defined in internal MHI header as a TRE length mask. Moving it to external header allows MHI client drivers to set this upper bound for the transmit buffer size.

---
 drivers/bus/mhi/core/internal.h | 1 -
 include/linux/mhi.h             | 3 +++
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/bus/mhi/core/internal.h +0 -1
index 7989269..4abf0cf 100644
@@ -453,7 +453,6 @@ enum mhi_pm_state {
 #define CMD_EL_PER_RING			128
 #define PRIMARY_CMD_RING		0
 #define MHI_DEV_WAKE_DB			127
-#define MHI_MAX_MTU			0xffff
 #define MHI_RANDOM_U32_NONZERO(bmsk)	(prandom_u32_max(bmsk) + 1)
 
 enum mhi_er_type {
include/linux/mhi.h +3 -0
index 7829b1d..6e1122c 100644
@@ -15,6 +15,9 @@
 #include <linux/wait.h>
 #include <linux/workqueue.h>
 
+/* MHI client drivers to set this upper bound for tx buffer */
+#define MHI_MAX_MTU 0xffff
+
 #define MHI_MAX_OEM_PK_HASH_SEGMENTS 16
 
 struct mhi_chan;
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#3 lore raw

PATCH v9 3/4 docs: Add documentation for userspace client interface

In reply to Hemant Kumar

MHI userspace client driver is creating device file node for user application to perform file operations. File operations are handled by MHI core driver. Currently Loopback MHI channel is supported by this driver.

---
 Documentation/mhi/index.rst |  1 +
 Documentation/mhi/uci.rst   | 83 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 Documentation/mhi/uci.rst
Documentation/mhi/index.rst +1 -0
index 1d8dec3..c75a371 100644
@@ -9,6 +9,7 @@ MHI
 
    mhi
    topology
+   uci
 
 .. only::  subproject and html
 
Documentation/mhi/uci.rst new file +83 -0

83 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
maintainer
#4 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

Documentation/mhi/uci.rst line 10

Quoted 4 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:
Documentation/mhi/uci.rst new file +4 -0
+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.

What's the user space that talks to this?

#5 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

Quoted 8 lines · Jakub Kicinski

Documentation/mhi/uci.rst line 10

Quoted 4 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:
Documentation/mhi/uci.rst new file +4 -0
+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.

What's the user space that talks to this?

Multiple.

Each channel has a different purpose. There it is expected that a different userspace application would be using it.

Hemant implemented the loopback channel, which is a simple channel that just sends you back anything you send it. Typically this is consumed by a test application.

Diag is a typical channel to be consumed by userspace. This is consumed by various applications that talk to the remote device for diagnostic information (logs and such).

Sahara is another common channel that is usually used for the multistage firmware loading process.

Signature · 3 lines
Jeffrey Hugo
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#6 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

Quoted 24 lines · Jeffrey Hugo
Quoted 11 lines · Jakub Kicinski
Quoted 7 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:

+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.

What's the user space that talks to this?

Multiple.

Each channel has a different purpose. There it is expected that a different userspace application would be using it.

Hemant implemented the loopback channel, which is a simple channel that
just sends you back anything you send it. Typically this is consumed by
a test application.

Quoted 5 lines

Diag is a typical channel to be consumed by userspace. This is consumed
by various applications that talk to the remote device for diagnostic
information (logs and such).

QMI too?
Dan

Quoted 4 lines

Sahara is another common channel that is usually used for the multistage
firmware loading process.

#7 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

Quoted 25 lines · Dan Williams
Quoted 24 lines · Jeffrey Hugo
Quoted 11 lines · Jakub Kicinski
Quoted 7 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:

+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.

What's the user space that talks to this?

Multiple.

Each channel has a different purpose. There it is expected that a different userspace application would be using it.

Hemant implemented the loopback channel, which is a simple channel that
just sends you back anything you send it. Typically this is consumed by
a test application.

Quoted 8 lines
Quoted 5 lines

Diag is a typical channel to be consumed by userspace. This is consumed
by various applications that talk to the remote device for diagnostic
information (logs and such).

QMI too?
Dan

Interesting question. My product doesn't use QMI. I would expect that all QMI runs through Router these days, but I am seeing some QMI channels in the downstream source.

Hemant, Do you know what is the usecase for the QMI0/QMI1 channels?

Signature · 3 lines
Jeffrey Hugo
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#8 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

Quoted 32 lines · Jeffrey Hugo
Quoted 31 lines · Dan Williams
Quoted 29 lines · Jeffrey Hugo
Quoted 11 lines · Jakub Kicinski
Quoted 7 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:

+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.

What's the user space that talks to this?

Multiple.

Each channel has a different purpose.  There it is expected that a different userspace application would be using it.

Hemant implemented the loopback channel, which is a simple channel that
just sends you back anything you send it.  Typically this is consumed by
a test application.

Diag is a typical channel to be consumed by userspace.  This is consumed
by various applications that talk to the remote device for diagnostic
information (logs and such).

Quoted 7 lines
Quoted 2 lines

QMI too?
Dan

Interesting question.  My product doesn't use QMI.  I would expect that all QMI runs through Router these days, but I am seeing some QMI channels in the downstream source.

Hemant, Do you know what is the usecase for the QMI0/QMI1 channels?

QMI0/QMI1 is used to send QMI message (control path) to bring the qmi rmnet data call.

Thanks,
Hemant

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
maintainer
#9 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

In reply to Jeffrey Hugo
Quoted 18 lines · On Mon, 26 Oct 2020 07:38:46 -0600 Jeffrey Hugo wrote:
Quoted 7 lines · Jakub Kicinski

Documentation/mhi/uci.rst line 10

Quoted 4 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:
Documentation/mhi/uci.rst new file +4 -0
+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.  

What's the user space that talks to this?

Multiple.

Each channel has a different purpose. There it is expected that a different userspace application would be using it.

Hemant implemented the loopback channel, which is a simple channel that just sends you back anything you send it. Typically this is consumed by a test application.

Quoted 6 lines

Diag is a typical channel to be consumed by userspace. This is consumed by various applications that talk to the remote device for diagnostic information (logs and such).

Sahara is another common channel that is usually used for the multistage firmware loading process.

Thanks for the info, are there any open source tests based on the loopback channel (perhaps even in tree?)

Since that's the only channel enabled in this set its the only one we can comment on.

#10 lore raw

Re: [PATCH v9 3/4] docs: Add documentation for userspace client interface

Hi Jakub,

Quoted 23 lines · Jakub Kicinski
Quoted 22 lines · On Mon, 26 Oct 2020 07:38:46 -0600 Jeffrey Hugo wrote:
Quoted 7 lines · Jakub Kicinski

Documentation/mhi/uci.rst line 10

Quoted 4 lines · On Fri, 23 Oct 2020 16:17:54 -0700 Hemant Kumar wrote:
Documentation/mhi/uci.rst new file +4 -0
+UCI driver enables userspace clients to communicate to external MHI devices
+like modem and WLAN. UCI driver probe creates standard character device file
+nodes for userspace clients to perform open, read, write, poll and release file
+operations.

What's the user space that talks to this?

Multiple.

Each channel has a different purpose. There it is expected that a different userspace application would be using it.

Hemant implemented the loopback channel, which is a simple channel that just sends you back anything you send it. Typically this is consumed by a test application.

Diag is a typical channel to be consumed by userspace. This is consumed by various applications that talk to the remote device for diagnostic information (logs and such).

Quoted 9 lines
Quoted 2 lines

Sahara is another common channel that is usually used for the multistage firmware loading process.

Thanks for the info, are there any open source tests based on the loopback channel (perhaps even in tree?)

Since that's the only channel enabled in this set its the only one we can comment on.

i am not aware of any open source tests based on loopback channel. My testing includes multiple sessions of echo, cat etc using adb to confirm what is sent is received back. Loic is using UCI driver for his use case too.

Loic, in case you have any use case (which is part of open source) which can use UCI driver, pls share that info ?

I think as soon as UCI becomes part of the tree, more and more channels would get added to the driver having open source code for that from other folks in community (Loic would be one of them i guess).

Thanks,
Hemant

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#11 lore raw

PATCH v9 4/4 bus: mhi: Add userspace client interface driver

In reply to 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_<controller_name>_<mhi_device_name>

Currently it supports LOOPBACK channel.

---
 drivers/bus/mhi/Kconfig  |  13 +
 drivers/bus/mhi/Makefile |   4 +
 drivers/bus/mhi/uci.c    | 658 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 675 insertions(+)
 create mode 100644 drivers/bus/mhi/uci.c
drivers/bus/mhi/Kconfig +13 -0
index e841c10..476cc55 100644
@@ -20,3 +20,16 @@ config MHI_BUS_DEBUG
 	  Enable debugfs support for use with the MHI transport. Allows
 	  reading and/or modifying some values within the MHI controller
 	  for debug and test purposes.
+
+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, 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.
drivers/bus/mhi/Makefile +4 -0
index 19e6443..80feefb 100644
@@ -1,2 +1,6 @@
 # core layer
 obj-y += core/
+
+# MHI client
+mhi_uci-y := uci.o
+obj-$(CONFIG_MHI_UCI) += mhi_uci.o
drivers/bus/mhi/uci.c new file +658 -0

658 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project

A message that is not in the archive (sent off-list or not yet received): CAMZdPi_MQ0SqK7s6h_1_9yEDD0vuAOpCTjSHTd1PBsGjvXukiA@mail.gmail.com

#13 lore raw

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

Hi Loic,

Quoted 4 lines · Loic Poulain

Hi Hemant,

That looks better IMHO, just small comments on locking.

[..]

Quoted 21 lines
    +static ssize_t mhi_uci_write(struct file *file,
    +                            const char __user *buf,
    +                            size_t count,
    +                            loff_t *offp)
    +{
    +       struct uci_dev *udev = file->private_data;
    +       struct mhi_device *mhi_dev = udev->mhi_dev;
    +       struct device *dev = &mhi_dev->dev;
    +       struct uci_chan *uchan = udev->uchan;
    +       size_t bytes_xfered = 0;
    +       int ret, nr_avail = 0;
    +
    +       /* if ul channel is not supported return error */
    +       if (!buf || !count || !mhi_dev->ul_chan)
    +               return -EINVAL;
    +
    +       dev_dbg(dev, "%s: to xfer: %zu bytes\n", __func__, count);
    +
    +       mutex_lock(&uchan->write_lock);
Quoted 1 line

Maybe mutex_lock_interruptible is more appropriate here (same in read fops).

i agree, will return -EINTR if mutex_lock_interruptible returns < 0.

Quoted 1 line

[..]

Quoted 32 lines
    +static ssize_t mhi_uci_read(struct file *file,
    +                           char __user *buf,
    +                           size_t count,
    +                           loff_t *ppos)
    +{
    +       struct uci_dev *udev = file->private_data;
    +       struct mhi_device *mhi_dev = udev->mhi_dev;
    +       struct uci_chan *uchan = udev->uchan;
    +       struct device *dev = &mhi_dev->dev;
    +       struct uci_buf *ubuf;
    +       size_t rx_buf_size;
    +       char *ptr;
    +       size_t to_copy;
    +       int ret = 0;
    +
    +       /* if dl channel is not supported return error */
    +       if (!buf || !mhi_dev->dl_chan)
    +               return -EINVAL;
    +
    +       mutex_lock(&uchan->read_lock);
    +       spin_lock_bh(&uchan->dl_pending_lock);
    +       /* No data available to read, wait */
    +       if (!uchan->cur_buf && list_empty(&uchan->dl_pending)) {
    +               dev_dbg(dev, "No data available to read, waiting\n");
    +
    +               spin_unlock_bh(&uchan->dl_pending_lock);
    +               ret = wait_event_interruptible(uchan->dl_wq,
    +                                              (!udev->enabled ||
    +                                           
      !list_empty(&uchan->dl_pending)));
Quoted 1 line

If you need to protect dl_pending list against concurent access, you need to do it in wait_event as well. I would suggest to lookg at wait_event_interruptible_lock_irq function, that allows to pass a locked spinlock as parameter. That would be safer and prevent this lock/unlock dance.

When using this API difference is, first we take spin_lock_bh() and then wait API is using spin_unlock_irq()/spin_lock_irq(). I am getting "BUG: scheduling while atomic" when i use this way. When i changed spin_lock_bh to spin_lock_irq then we got rid of the kernel BUG.

Thanks,
Hemant

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project