Skip to contentSkip to conversation

Add QMI Device Service Request Rate-limit Option

PATCH Superseded Grant Erickson 2 participants 10 messages Feb 14, 2025

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add QMI Device Service Request Rate-limit Option

Certain modems, among them the Quectel Wireless Solutions Co., Ltd. (2c7c) BG96 CAT-M1/NB-IoT modem (0296), have a firmware issue where they can lock up and hang (not responding to subsequent commands) due to high QMI service request arrival rates.

QMI service request rate limiting is achieved by:

1. Adding a new, optional options structure to qmi_qmux_device_new that has its rate-limit "quirk" and minimum request period fields set when the modem "RequestThrottleTimeUs" integer property is set.

2. Adding logic to the QMI driver that, when a minimum request period is set, limits the QMI service request rate accordingly.

(1) and (2) are used by the udevng and gobi plugins such that when the modem vendor and model match 2c7c/0296, then the option to rate limit QMI service requests is set to no more than one every 2,000 us.

Grant Erickson (7):
qmi: Added enumeration for device quirk flags.
qmi: Added structure for device options.
qmi: Added device options parameter to 'qmi_qmux_device_new'.
qmi: Implement QMI service request rate limiting in 'can_write_data'.
qmi: Handle request rate limit option in 'qmi_qmux_device_new'.
udevng: Set the QMI minimum service request period for Quectel BG96
modems.
gobi: Pass QMI qmux device options to 'qmi_qmux_device_new'.

 drivers/qmimodem/qmi.c | 48 +++++++++++++++++++++++++++++++++++++++++-
 drivers/qmimodem/qmi.h | 40 ++++++++++++++++++++++++++++++++++-
 plugins/gobi.c         | 22 ++++++++++++++++++-
 plugins/udevng.c       | 14 ++++++++++++
 4 files changed, 121 insertions(+), 3 deletions(-)
Signature · 1 line
2.45.0
#1 lore raw

PATCH v4 1/7 qmi: Added enumeration for device quirk flags.

Defines device-specific "quirks" that may alter the default behavior of the QMI driver for a specific, instantiated device.

---
 drivers/qmimodem/qmi.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/qmimodem/qmi.h +18 -0
index 69698ee049c6..e74ff3feaeec 100644
@@ -56,6 +56,24 @@ enum qmi_error {
 	QMI_ERROR_INVALID_QMI_COMMAND				= 71,
 };
 
+/**
+ *	This defines device-specific "quirks" that may alter the default
+ *	behavior of the QMI driver for a specific, instantiated device.
+ */
+enum qmi_qmux_device_quirk {
+	/**
+	 *	The device has no "quirks".
+	 */
+	QMI_QMUX_DEVICE_QUIRK_NONE			= 0x00,
+
+	/**
+	 *	The device has a "quirk" where it can lock up and hang (not
+	 *	responding to subsequent commands) due to high QMI service
+	 *	request arrival rates.
+	 */
+	QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT = 0x01
+};
+
 typedef void (*qmi_destroy_func_t)(void *user_data);
 
 struct qmi_service;
Signature · 1 line
2.45.0
#2 lore raw

PATCH v4 2/7 qmi: Added structure for device options.

In reply to Grant Erickson

Defines options that may alter the default behavior of the QMI driver for a specific, instantiated device.

---
 drivers/qmimodem/qmi.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/qmimodem/qmi.h +19 -0
index e74ff3feaeec..4ba27847176f 100644
@@ -74,6 +74,25 @@ enum qmi_qmux_device_quirk {
 	QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT = 0x01
 };
 
+/**
+ *	Defines options that may alter the default behavior of the QMI
+ *	driver for a specific, instantiated device.
+ */
+struct qmi_qmux_device_options {
+	/**
+	 *	Device-specific "quirk".
+	 */
+	enum qmi_qmux_device_quirk quirks;
+
+	/**
+	 *	If quirks has #QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT set, this
+	 *	is the minimum period, in microseconds, in which back-to-back
+	 *	QMI service requests may be sent to avoid triggering a
+	 *	firmware lock up and hang.
+	 */
+	unsigned int min_req_period_us;
+};
+
 typedef void (*qmi_destroy_func_t)(void *user_data);
 
 struct qmi_service;
Signature · 1 line
2.45.0
#3 lore raw

PATCH v4 3/7 qmi: Added device options parameter to 'qmi_qmux_device_new'.

In reply to Grant Erickson

An optional pointer to immutable device options that may alter the default behavior of the QMI driver for the instantiated device.

---
 drivers/qmimodem/qmi.c | 3 ++-
 drivers/qmimodem/qmi.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/qmimodem/qmi.c +2 -1
index 10dbdaac8bf6..bbe6440abe0f 100644
@@ -1567,7 +1567,8 @@ static const struct qmi_transport_ops qmux_ops = {
 	.write = qmi_qmux_device_write,
 };
 
-struct qmi_qmux_device *qmi_qmux_device_new(const char *device)
+struct qmi_qmux_device *qmi_qmux_device_new(const char *device,
+	const struct qmi_qmux_device_options *options)
 {
 	struct qmi_qmux_device *qmux;
 	int fd;
drivers/qmimodem/qmi.h +2 -1
index 4ba27847176f..09b551d74823 100644
@@ -107,7 +107,8 @@ typedef void (*qmi_qrtr_node_lookup_done_func_t)(void *);
 
 typedef void (*qmi_service_result_func_t)(struct qmi_result *, void *);
 
-struct qmi_qmux_device *qmi_qmux_device_new(const char *device);
+struct qmi_qmux_device *qmi_qmux_device_new(const char *device,
+				const struct qmi_qmux_device_options *options);
 void qmi_qmux_device_free(struct qmi_qmux_device *qmux);
 void qmi_qmux_device_set_debug(struct qmi_qmux_device *qmux,
 				qmi_debug_func_t func, void *user_data);
Signature · 1 line
2.45.0
#4 lore raw

PATCH v4 4/7 qmi: Implement QMI service request rate limiting in 'can_write_data'.

In reply to Grant Erickson

Determine if we need to rate-limit QMI services requests to a transport-specific minimum request period. If so, return true so that the queue can be retried again later.

Adds the following data members to 'qmi_transport':

* Minimum request period
- The minimum period, in microseconds, for back-to-back QMI service requests.

* Last request time
- Time, in microseconds, when the last QMI service request was sent.

to support the implementation.

---
 drivers/qmimodem/qmi.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
drivers/qmimodem/qmi.c +35 -0
index bbe6440abe0f..7b20dc3f85dd 100644
@@ -92,6 +92,18 @@ struct qmi_transport {
 	const struct qmi_transport_ops *ops;
 	struct debug_data debug;
 	bool writer_active : 1;
+
+	/**
+	 *  If specified via qmi_qmux_device_options, the minimum period
+	 *  for back-to-back QMI service requests.
+	 */
+	unsigned int min_req_period_us;
+
+	/**
+	 *  The time, in microseconds, when the last QMI service request
+	 *  was sent.
+	 */
+	uint64_t last_req_sent_time_us;
 };
 
 struct qmi_qmux_device {
@@ -653,8 +665,28 @@ static bool can_write_data(struct l_io *io, void *user_data)
 {
 	struct qmi_transport *transport = user_data;
 	struct qmi_request *req;
+	const bool throttle_enabled = transport->min_req_period_us > 0;
+	uint64_t now;
 	int r;
 
+	/*
+	 * Determine if we need to rate-limit commands to a
+	 * transport-specific minimum request period. If so,
+	 * return true so that the queue can be retried again
+	 * later.
+	 */
+	if (throttle_enabled) {
+		now = l_time_now();
+
+		if (transport->last_req_sent_time_us != 0) {
+			const uint64_t delta = l_time_diff(now,
+				transport->last_req_sent_time_us);
+
+			if (delta < transport->min_req_period_us)
+				return true;
+		}
+	}
+
 	req = l_queue_pop_head(transport->req_queue);
 	if (!req)
 		return false;
@@ -665,6 +697,9 @@ static bool can_write_data(struct l_io *io, void *user_data)
 		return false;
 	}
 
+	if (throttle_enabled)
+		transport->last_req_sent_time_us = now;
+
 	if (l_queue_length(transport->req_queue) > 0)
 		return true;
 
Signature · 1 line
2.45.0
#5 lore raw

Re: [PATCH v4 4/7] qmi: Implement QMI service request rate limiting in 'can_write_data'.

Hi Grant,

Quoted 10 lines · Grant Erickson

Determine if we need to rate-limit QMI services requests to a transport-specific minimum request period. If so, return true so that the queue can be retried again later.

Adds the following data members to 'qmi_transport':

* Minimum request period
- The minimum period, in microseconds, for back-to-back QMI service requests.

Quoted 9 lines

* Last request time
- Time, in microseconds, when the last QMI service request was sent.

to support the implementation.

---
  drivers/qmimodem/qmi.c | 35 +++++++++++++++++++++++++++++++++++
  1 file changed, 35 insertions(+)

The CI doesn't like this version either:

Alpine (musl) gcc optimized
===========================
Configure: PASS
Build: FAIL
drivers/qmimodem/qmi.c: In function 'can_write_data':
drivers/qmimodem/qmi.c:701:50: error: 'now' may be used uninitialized
[-Werror=maybe-uninitialized]

      701 |                 transport->last_req_sent_time_us = now;
          |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~

drivers/qmimodem/qmi.c:669:18: note: 'now' was declared here
669 | uint64_t now;
| ^~~
cc1: all warnings being treated as errors
make[1]: *** [Makefile:4090: drivers/qmimodem/qmi.o] Error 1 make[1]: Target 'all-am' not remade because of errors.
make: *** [Makefile:2405: all] Error 2

Regards,
-Denis

#6 lore raw

Re: [PATCH v4 4/7] qmi: Implement QMI service request rate limiting in 'can_write_data'.

Quoted 38 lines · Denis Kenzior

Hi Grant,

Quoted 11 lines · Grant Erickson

Determine if we need to rate-limit QMI services requests to a transport-specific minimum request period. If so, return true so that the queue can be retried again later.
Adds the following data members to 'qmi_transport':
* Minimum request period
- The minimum period, in microseconds, for back-to-back QMI service requests.
* Last request time
- Time, in microseconds, when the last QMI service request was sent.
to support the implementation.

Quoted 3 lines
---
 drivers/qmimodem/qmi.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

The CI doesn't like this version either:

Alpine (musl) gcc optimized
===========================
Configure: PASS
Build: FAIL
drivers/qmimodem/qmi.c: In function 'can_write_data':

   drivers/qmimodem/qmi.c:701:50: error: 'now' may be used uninitialized [-Werror=maybe-uninitialized]
     701 |                 transport->last_req_sent_time_us = now;
         |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
   drivers/qmimodem/qmi.c:669:18: note: 'now' was declared here
     669 |         uint64_t now;
         |                  ^~~
   cc1: all warnings being treated as errors
   make[1]: *** [Makefile:4090: drivers/qmimodem/qmi.o] Error 1
   make[1]: Target 'all-am' not remade because of errors.
   make: *** [Makefile:2405: all] Error 2
Quoted 2 lines

Regards,
-Denis

Denis,

Is there a place, in-tree, where the CI process and configuration are coded / documented? I’d like to be able to produce the same results which:

% ./bootstrap
% mkdir build-clang
% mkdir build-gcc
% cd build-clang
% CC=clang ../configure -C
% make -jnproc
% cd ../build-gcc
% CC=gcc ./configure -C
% make -jnproc

does not seem to replicate.

Best,

Grant

Signature · 5 lines
Principal
Nuovations

[email protected]
https://www.nuovations.com/
#7 lore raw

PATCH v4 5/7 qmi: Handle request rate limit option in 'qmi_qmux_device_new'.

In reply to Grant Erickson

If options are specified and QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT is asserted, set the qmux transport minimum service request period from the specified options.

---
 drivers/qmimodem/qmi.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/qmimodem/qmi.c +10 -0
index 7b20dc3f85dd..b50076b08a0c 100644
@@ -1614,6 +1614,16 @@ struct qmi_qmux_device *qmi_qmux_device_new(const char *device,
 
 	qmux = l_new(struct qmi_qmux_device, 1);
 
+	if (options != NULL) {
+		if ((options->quirks & QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT)
+			== QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT) {
+			qmux->transport.min_req_period_us = options->min_req_period_us;
+
+			ofono_info("QMI minimum service request period %u us",
+				qmux->transport.min_req_period_us);
+		}
+	}
+
 	if (qmi_transport_open(&qmux->transport, fd, &qmux_ops) < 0) {
 		close(fd);
 		l_free(qmux);
Signature · 1 line
2.45.0
#8 lore raw

PATCH v4 6/7 udevng: Set the QMI minimum service request period for Quectel BG96 modems.

In reply to Grant Erickson

The Quectel Wireless Solutions Co., Ltd. (2c7c) BG96 CAT-M1/NB-IoT modem (0296) has a firmware issue where it can lock up and hang (not responding to subsequent commands) due to high QMI service request arrival rates. If the vendor and model match those, then rate limit QMI service requests to no more than one every 2,000 us.

---
 plugins/udevng.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
plugins/udevng.c +14 -0
index 64875a47752b..b8df66de5c71 100644
@@ -1118,6 +1118,20 @@ static gboolean setup_quectelqmi(struct modem_info *modem)
 
 	DBG("%s", modem->syspath);
 
+	/*
+	 * The Quectel Wireless Solutions Co., Ltd. (2c7c) BG96
+	 * CAT-M1/NB-IoT modem (0296) has a firmware issue where it can
+	 * lock up and hang (not responding to subsequent commands) due to
+	 * high QMI service request arrival rates. If the vendor and model
+	 * match those, then rate limit QMI service requests to no more
+	 * than one every 2,000 us.
+	 */
+	if (l_streq0(modem->vendor, "2c7c")) {
+		if (l_streq0(modem->model, "0296"))
+			ofono_modem_set_integer(modem->modem,
+				"RequestThrottleTimeUs", 2000);
+	}
+
 	for (list = modem->devices; list; list = g_slist_next(list)) {
 		const struct device_info *info = list->data;
 		const char *subsystem =
Signature · 1 line
2.45.0
#9 lore raw

PATCH v4 7/7 gobi: Pass QMI qmux device options to 'qmi_qmux_device_new'.

In reply to Grant Erickson

If the QMI minimum service request period property is set on the modem object, then set the appropriate QMI QMUX device option quirk and value.

---
 plugins/gobi.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
plugins/gobi.c +21 -1
index 027aa91d6ade..dfc7402e36aa 100644
@@ -842,9 +842,19 @@ error:
 	__shutdown_device(modem);
 }
 
+/*
+ * Enable the modem. The following modem properties are optionally set:
+ *
+ * RequestThrottleTimeUs
+ *   The minimum period, in microseconds, in which back-to-back *
+ *   modem service requests may be sent.
+ *
+ */
 static int gobi_enable(struct ofono_modem *modem)
 {
 	struct gobi_data *data = ofono_modem_get_data(modem);
+	struct qmi_qmux_device_options options = { QMI_QMUX_DEVICE_QUIRK_NONE, 0 };
+	int min_req_period_us;
 	const char *device;
 
 	DBG("%p", modem);
@@ -853,7 +863,17 @@ static int gobi_enable(struct ofono_modem *modem)
 	if (!device)
 		return -EINVAL;
 
-	data->device = qmi_qmux_device_new(device);
+	/*
+	 * If the QMI minimum service request period property is set, then
+	 * set the appropriate QMI QMUX device quirk and value.
+	 */
+	min_req_period_us = ofono_modem_get_integer(modem, "RequestThrottleTimeUs");
+	if (min_req_period_us > 0) {
+		options.quirks |= QMI_QMUX_DEVICE_QUIRK_REQ_RATE_LIMIT;
+		options.min_req_period_us = min_req_period_us;
+	}
+
+	data->device = qmi_qmux_device_new(device, &options);
 	if (!data->device)
 		return -EIO;
 
Signature · 1 line
2.45.0