Skip to contentSkip to conversation

Add support for Microsoft Surface System Aggregator Module

PATCH Superseded Maximilian Luz 6 participants 25 messages Nov 15, 2020 – Nov 24, 2020

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Maximilian Luz

author maintainer
#0 lore raw

cover Add support for Microsoft Surface System Aggregator Module

Hello,

N.B.: the following text is mostly a repeat of cover letter from the previous RFC for the uninitiated, which can be found at

https://lore.kernel.org/linux-serial/[email protected]/

See "Changes" below for an overview of differences between the RFC and this patchset. I hope I have addressed all comments from that in this version, thank you again for those.

The Surface System Aggregator Module (we'll refer to it as Surface Aggregator or SAM below) is an embedded controller (EC) found on various Microsoft Surface devices. Specifically, all 4th and later generation Surface devices, i.e. Surface Pro 4, Surface Book 1 and later, with the exception of the Surface Go series and the Surface Duo. Notably, it seems like this EC can also be found on the ARM-based Surface Pro X [1].

Functionality provided by this EC depends on the Surface model and can (roughly) be broken down by their generations: Starting with 5th generation devices (Surface Pro 2017/5, Surface Book 2, Surface Laptop 1, and later), the EC provides battery and thermal readings, as well as access to the real-time clock. On 5th and 6th generations, these features, specifically, are provided via the ACPI interface of the EC, referred to as Surface ACPI Notify (SAN), i.e. they act as standard ACPI devices of that type, but require a driver translating requests written to an ACPI operation region to requests to the EC. On 7th generation devices, the ACPI interface is (largely) gone, and has been replaced with custom battery and thermal drivers, directly querying the EC.

Additionally, HID keyboard and touchpad input for Surface models with these devices built in can be handled via the EC: On the Surface Laptops 1 and 2, this includes only the keyboard, while on the Surface Laptop 3 and Book 3, this includes both touchpad and keyboard. In this case, actual input is provided as HID data and the EC connection acts as HID transport, thus requiring a special transport driver for those devices to work.

Further, all known devices (5th and later generations) also support changing of performance/cooling modes, which can influence cooling capabilities of the device (e.g. prefer silent operation over performance), and may influence power limits (e.g. of the discrete GPU found on Surface Books).

While this constitutes all major functionality, some more device specific functionality is also handled by the EC. For example, on the Surface Books, the EC handles detaching of the clipboard (i.e. the upper part with screen and CPU) from the base (the lower part with keyboard and optional discrete GPU) and can influence its behavior (i.e. it provides an interface via which detachment can be requested, aborted, or confirmed). It can also be used to detect if there has been a base attached to the clipboard, and if so what type.

This patch-series adds the basis for supporting this EC and the features provided by it, by, first, implementing a communication driver providing a fundamental API for client drivers, handling specific aspects of the EC. Additionally, it builds on top of that to provide a dedicated bus and device type to better manage EC clients (and break it down pseudo- device-wise), especially in the case when these client devices are not described in ACPI, i.e. cannot be discovered by conventional means. Furthermore, it provides support for debugging and prototyping via an optional user-space interface, and, lastly, also support for the aforementioned ACPI interface, allowing ACPI to communicate with the EC directly.

This series only addresses 5th and later generation Surface models as the communication interface has changed substantially from 4th to 5th generation, and the 4th generation interface has not been reverse- engineered yet. Specifically, the underlying transport has been changed from HID feature and input-/output-reports to serial communication via an UART and a custom protocol. Support for 4th generation devices may be added in the future, but as currently not much is known about 4th generation SAM, it yet remains to be seen if this can happen as addition to this subsystem, or if it will be easier to implement this as separate platform driver. Especially as the 4th generation EC does not seem to provide much of the functionality found on 5th and later generations (e.g. no battery status reporting, no thermal reporting, ..., we assume it's just clipboard detachment on the Surface Book 1 and performance mode setting).

In more detail, this series adds a driver for the Surface Serial Hub (SSH), the 5th- and later-generation communication channel to the EC, a pseudo-device and driver exposing a misc-device that can be used to communicate with the EC from user-space, intended for debugging, testing, and prototyping, as well as a driver for the Surface ACPI Notify (SAN) device, i.e. the interface between ACPI and EC. Some more details on those can be found on the individual commit messages.

This series, apart from the SAN and user-space drivers, does not add any client drivers. This will be handled via future patches once the core has been accepted (and the other client drivers have been cleaned up a bit).

On the top level, EC communication via the SSH driver can be broken down into requests (sent from host to EC), corresponding responses (sent from EC to host, associated with and triggered by a request), and events (sent from EC to host without association to a request). The SSH driver manages all communication (i.e. matches responses to requests, enables and disables events, and manages event handlers/notifiers installed by client drivers). On the lower levels, SSH communication is packet-based, and described in more detail in the documentation added in this series (specifically ssh.rst).

This set of modules and drivers has been developed out of tree at [2] and used/tested in the kernel we provide at [3] pretty much since its beginnings. It has been developed by reverse-engineering the SSH protocol, mostly through the ACPI interface, communication dumps obtained from listening in on Windows, and deduction. So things may be wrong. There have been some attempts at reverse-engineering existing drivers, which also gave a bit of insight for development, however, I haven't gotten very far on that front beyond some more higher-level concepts and detecting a couple of new EC commands/confirming the functionality of already known commands.

Driver and module names have been chosen to align with Windows driver names, some field, variable, and concept names have been chosen to align with ACPI code (or at least with what I think some of the more cryptic names could mean and make sense in the respective context, e.g. IID -> Instance ID, TC -> Target Category).

This patch-set can also be found at the following repository and reference, if you prefer to look at a kernel tree instead of these emails:

https://github.com/linux-surface/kernel tags/s/surface-aggregator/v1

Thanks,
Max

[1]: The Surface Pro X is, however, currently considered unsupported due
to a lack of test candidates and, as it seems, general lack of Linux support on other parts. AFAIK there is an issue preventing serial devices from being registered, on which the core driver in this series is build on, thus there is no way to even test that at this point. I'd be happy to work out any issues regarding SAM on the Pro X at some point in the future, provided someone can/wants to actually test it.

[2]: https://github.com/linux-surface/surface-aggregator-module
[3]: https://github.com/linux-surface/linux-surface

Note: This patch depends on

[PATCH v4] platform/surface: Create a platform subdirectory for
Microsoft Surface devices

which can be found at

https://lore.kernel.org/platform-driver-x86/[email protected]/

and is currently in platform-drivers-x86/for-next.

Changes from the previous RFC (overview):
- move to platform/surface
- add copyright lines
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- change user-space interface from debugfs to misc-device
- address issues in user-space interface
- fix typos in commit messages and documentation
- fix some bugs, address other issues

Changes regarding specific patches (and more details) can be found on the individual patch.

Maximilian Luz (9):
platform/surface: Add Surface Aggregator subsystem platform/surface: aggregator: Add control packet allocation caching platform/surface: aggregator: Add event item allocation caching platform/surface: aggregator: Add trace points platform/surface: aggregator: Add error injection capabilities platform/surface: aggregator: Add dedicated bus and device type
docs: driver-api: Add Surface Aggregator subsystem documentation platform/surface: Add Surface Aggregator user-space interface platform/surface: Add Surface ACPI Notify driver

 Documentation/driver-api/index.rst            |    1 +
 .../surface_aggregator/client-api.rst         |   38 +
 .../driver-api/surface_aggregator/client.rst  |  394 +++
 .../surface_aggregator/clients/cdev.rst       |   85 +
 .../surface_aggregator/clients/index.rst      |   21 +
 .../surface_aggregator/clients/san.rst        |   44 +
 .../driver-api/surface_aggregator/index.rst   |   21 +
 .../surface_aggregator/internal-api.rst       |   67 +
 .../surface_aggregator/internal.rst           |   50 +
 .../surface_aggregator/overview.rst           |   76 +
 .../driver-api/surface_aggregator/ssh.rst     |  343 +++
 MAINTAINERS                                   |   13 +
 drivers/platform/surface/Kconfig              |   39 +
 drivers/platform/surface/Makefile             |    3 +
 drivers/platform/surface/aggregator/Kconfig   |   65 +
 drivers/platform/surface/aggregator/Makefile  |   17 +
 drivers/platform/surface/aggregator/bus.c     |  424 +++
 drivers/platform/surface/aggregator/bus.h     |   27 +
 .../platform/surface/aggregator/controller.c  | 2557 +++++++++++++++++
 .../platform/surface/aggregator/controller.h  |  288 ++
 drivers/platform/surface/aggregator/core.c    |  831 ++++++
 .../platform/surface/aggregator/ssh_msgb.h    |  201 ++
 .../surface/aggregator/ssh_packet_layer.c     | 2009 +++++++++++++
 .../surface/aggregator/ssh_packet_layer.h     |  175 ++
 .../platform/surface/aggregator/ssh_parser.c  |  229 ++
 .../platform/surface/aggregator/ssh_parser.h  |  157 +
 .../surface/aggregator/ssh_request_layer.c    | 1254 ++++++++
 .../surface/aggregator/ssh_request_layer.h    |  142 +
 drivers/platform/surface/aggregator/trace.h   |  625 ++++
 .../platform/surface/surface_acpi_notify.c    |  884 ++++++
 .../surface/surface_aggregator_cdev.c         |  299 ++
 include/linux/mod_devicetable.h               |   18 +
 include/linux/surface_acpi_notify.h           |   39 +
 include/linux/surface_aggregator/controller.h |  832 ++++++
 include/linux/surface_aggregator/device.h     |  430 +++
 include/linux/surface_aggregator/serial_hub.h |  659 +++++
 include/uapi/linux/surface_aggregator/cdev.h  |   58 +
 scripts/mod/devicetable-offsets.c             |    8 +
 scripts/mod/file2alias.c                      |   23 +
 39 files changed, 13446 insertions(+)
 create mode 100644 Documentation/driver-api/surface_aggregator/client-api.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/client.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/cdev.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/index.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/san.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/index.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/internal-api.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/internal.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/overview.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/ssh.rst
 create mode 100644 drivers/platform/surface/aggregator/Kconfig
 create mode 100644 drivers/platform/surface/aggregator/Makefile
 create mode 100644 drivers/platform/surface/aggregator/bus.c
 create mode 100644 drivers/platform/surface/aggregator/bus.h
 create mode 100644 drivers/platform/surface/aggregator/controller.c
 create mode 100644 drivers/platform/surface/aggregator/controller.h
 create mode 100644 drivers/platform/surface/aggregator/core.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_msgb.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_parser.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_parser.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.h
 create mode 100644 drivers/platform/surface/aggregator/trace.h
 create mode 100644 drivers/platform/surface/surface_acpi_notify.c
 create mode 100644 drivers/platform/surface/surface_aggregator_cdev.c
 create mode 100644 include/linux/surface_acpi_notify.h
 create mode 100644 include/linux/surface_aggregator/controller.h
 create mode 100644 include/linux/surface_aggregator/device.h
 create mode 100644 include/linux/surface_aggregator/serial_hub.h
 create mode 100644 include/uapi/linux/surface_aggregator/cdev.h
Signature · 1 line
2.29.2
author maintainer
#9 lore raw

PATCH 2/9 platform/surface: aggregator: Add control packet allocation caching

Surface Serial Hub communication is, in its core, packet based. Each sequenced packet requires to be acknowledged, via an ACK-type control packet. In case invalid data has been received by the driver, a NAK-type (not-acknowledge/negative acknowledge) control packet is sent, triggering retransmission.

Control packets are therefore a core communication primitive and used frequently enough (with every sequenced packet transmission sent by the embedded controller, including events and request responses) that it may warrant caching their allocations to reduce possible memory fragmentation.

---
 drivers/platform/surface/aggregator/core.c    | 27 ++++++++++-
 .../surface/aggregator/ssh_packet_layer.c     | 47 +++++++++++++++----
 .../surface/aggregator/ssh_packet_layer.h     |  3 ++
 3 files changed, 67 insertions(+), 10 deletions(-)
drivers/platform/surface/aggregator/core.c +26 -1
index 58ba000e95d6..5c01d598a479 100644
@@ -772,7 +772,32 @@ static struct serdev_device_driver ssam_serial_hub = {
 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
 	},
 };
-module_serdev_device_driver(ssam_serial_hub);
+
+
+/* -- Module setup. --------------------------------------------------------- */
+
+static int __init ssam_core_init(void)
+{
+	int status;
+
+	status = ssh_ctrl_packet_cache_init();
+	if (status)
+		return status;
+
+	status = serdev_device_driver_register(&ssam_serial_hub);
+	if (status)
+		ssh_ctrl_packet_cache_destroy();
+
+	return status;
+}
+module_init(ssam_core_init);
+
+static void __exit ssam_core_exit(void)
+{
+	serdev_device_driver_unregister(&ssam_serial_hub);
+	ssh_ctrl_packet_cache_destroy();
+}
+module_exit(ssam_core_exit);
 
 MODULE_AUTHOR("Maximilian Luz <[email protected]>");
 MODULE_DESCRIPTION("Subsystem and Surface Serial Hub driver for Surface System Aggregator Module");
drivers/platform/surface/aggregator/ssh_packet_layer.c +38 -9

74 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_packet_layer.h +3 -0
index 01ac27d60c3a..f3d8a85389d5 100644
@@ -169,4 +169,7 @@ void ssh_ptl_tx_wakeup(struct ssh_ptl *ptl);
 void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
 		     u8 priority, const struct ssh_packet_ops *ops);
 
+int ssh_ctrl_packet_cache_init(void);
+void ssh_ctrl_packet_cache_destroy(void);
+
 #endif /* _SURFACE_AGGREGATOR_SSH_PACKET_LAYER_H */
Signature · 1 line
2.29.2
author maintainer
#10 lore raw

PATCH 3/9 platform/surface: aggregator: Add event item allocation caching

In reply to Maximilian Luz

Event items are used for completing Surface Aggregator EC events, i.e. placing event command data and payload on a workqueue for later processing to avoid doing said processing directly on the receiver thread. This means that event items are allocated for each incoming event, regardless of that event being transmitted via sequenced or unsequenced packets.

On the Surface Book 3 and Surface Laptop 3, touchpad HID input events (unsequenced), can constitute a larger amount of traffic, and therefore allocation of event items. This warrants caching event items to reduce memory fragmentation. The size of the cached objects is specifically tuned to accommodate keyboard and touchpad input events and their payloads on those devices. As a result, this effectively also covers most other event types. In case of a larger event payload, event item allocation will fall back to kzalloc().

---
 .../platform/surface/aggregator/controller.c  | 84 +++++++++++++++++--
 .../platform/surface/aggregator/controller.h  |  9 ++
 drivers/platform/surface/aggregator/core.c    | 16 +++-
 3 files changed, 100 insertions(+), 9 deletions(-)
drivers/platform/surface/aggregator/controller.c +77 -7
index fed5b069489c..1c7d2d7b29f7 100644
@@ -535,14 +535,74 @@ static void ssam_nf_destroy(struct ssam_nf *nf)
 
 #define SSAM_CPLT_WQ_NAME	"ssam_cpltq"
 
+/*
+ * SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN - Maximum payload length for a cached
+ * &struct ssam_event_item.
+ *
+ * This length has been chosen to be accommodate standard touchpad and
+ * keyboard input events. Events with larger payloads will be allocated
+ * separately.
+ */
+#define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN	32
+
+static struct kmem_cache *ssam_event_item_cache;
+
+/**
+ * ssam_event_item_cache_init() - Initialize the event item cache.
+ */
+int ssam_event_item_cache_init(void)
+{
+	const unsigned int size = sizeof(struct ssam_event_item)
+				  + SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN;
+	const unsigned int align = __alignof__(struct ssam_event_item);
+	struct kmem_cache *cache;
+
+	cache = kmem_cache_create("ssam_event_item", size, align, 0, NULL);
+	if (!cache)
+		return -ENOMEM;
+
+	ssam_event_item_cache = cache;
+	return 0;
+}
+
+/**
+ * ssam_event_item_cache_destroy() - Deinitialize the event item cache.
+ */
+void ssam_event_item_cache_destroy(void)
+{
+	kmem_cache_destroy(ssam_event_item_cache);
+	ssam_event_item_cache = NULL;
+}
+
+static void __ssam_event_item_free_cached(struct ssam_event_item *item)
+{
+	kmem_cache_free(ssam_event_item_cache, item);
+}
+
+static void __ssam_event_item_free_generic(struct ssam_event_item *item)
+{
+	kfree(item);
+}
+
+/**
+ * ssam_event_item_free() - Free the provided event item.
+ * @item: The event item to free.
+ */
+static void ssam_event_item_free(struct ssam_event_item *item)
+{
+	item->ops.free(item);
+}
+
 /**
  * ssam_event_item_alloc() - Allocate an event item with the given payload size.
  * @len:   The event payload length.
  * @flags: The flags used for allocation.
  *
- * Allocate an event item with the given payload size. Sets the item
- * operations and payload length values. The item free callback (``ops.free``)
- * should not be overwritten after this call.
+ * Allocate an event item with the given payload size, preferring allocation
+ * from the event item cache if the payload is small enough (i.e. smaller than
+ * %SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN). Sets the item operations and payload
+ * length values. The item free callback (``ops.free``) should not be
+ * overwritten after this call.
  *
  * Return: Returns the newly allocated event item.
  */
@@ -550,9 +610,19 @@ static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
 {
 	struct ssam_event_item *item;
 
-	item = kzalloc(sizeof(*item) + len, GFP_KERNEL);
-	if (!item)
-		return NULL;
+	if (len <= SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN) {
+		item = kmem_cache_alloc(ssam_event_item_cache, GFP_KERNEL);
+		if (!item)
+			return NULL;
+
+		item->ops.free = __ssam_event_item_free_cached;
+	} else {
+		item = kzalloc(sizeof(*item) + len, GFP_KERNEL);
+		if (!item)
+			return NULL;
+
+		item->ops.free = __ssam_event_item_free_generic;
+	}
 
 	item->event.length = len;
 	return item;
@@ -714,7 +784,7 @@ static void ssam_event_queue_work_fn(struct work_struct *work)
 			return;
 
 		ssam_nf_call(nf, dev, item->rqid, &item->event);
-		kfree(item);
+		ssam_event_item_free(item);
 	}
 
 	if (!ssam_event_queue_is_empty(queue))
drivers/platform/surface/aggregator/controller.h +9 -0
index 7688cbc7d0ae..96e2b87a25d9 100644
@@ -81,12 +81,18 @@ struct ssam_cplt;
  * struct ssam_event_item - Struct for event queuing and completion.
  * @node:     The node in the queue.
  * @rqid:     The request ID of the event.
+ * @ops:      Instance specific functions.
+ * @ops.free: Callback for freeing this event item.
  * @event:    Actual event data.
  */
 struct ssam_event_item {
 	struct list_head node;
 	u16 rqid;
 
+	struct {
+		void (*free)(struct ssam_event_item *event);
+	} ops;
+
 	struct ssam_event event;	// must be last
 };
 
@@ -276,4 +282,7 @@ int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl);
 int ssam_controller_suspend(struct ssam_controller *ctrl);
 int ssam_controller_resume(struct ssam_controller *ctrl);
 
+int ssam_event_item_cache_init(void);
+void ssam_event_item_cache_destroy(void);
+
 #endif /* _SURFACE_AGGREGATOR_CONTROLLER_H */
drivers/platform/surface/aggregator/core.c +14 -2
index 5c01d598a479..733a7d46ee07 100644
@@ -782,12 +782,23 @@ static int __init ssam_core_init(void)
 
 	status = ssh_ctrl_packet_cache_init();
 	if (status)
-		return status;
+		goto err_cpkg;
+
+	status = ssam_event_item_cache_init();
+	if (status)
+		goto err_evitem;
 
 	status = serdev_device_driver_register(&ssam_serial_hub);
 	if (status)
-		ssh_ctrl_packet_cache_destroy();
+		goto err_register;
 
+	return 0;
+
+err_register:
+	ssam_event_item_cache_destroy();
+err_evitem:
+	ssh_ctrl_packet_cache_destroy();
+err_cpkg:
 	return status;
 }
 module_init(ssam_core_init);
@@ -795,6 +806,7 @@ module_init(ssam_core_init);
 static void __exit ssam_core_exit(void)
 {
 	serdev_device_driver_unregister(&ssam_serial_hub);
+	ssam_event_item_cache_destroy();
 	ssh_ctrl_packet_cache_destroy();
 }
 module_exit(ssam_core_exit);
Signature · 1 line
2.29.2
author maintainer
#1 lore raw

PATCH 1/9 platform/surface: Add Surface Aggregator subsystem

In reply to Maximilian Luz

Add Surface System Aggregator Module core and Surface Serial Hub driver, required for the embedded controller found on Microsoft Surface devices.

The Surface System Aggregator Module (SSAM, SAM or Surface Aggregator) is an embedded controller (EC) found on 4th and later generation Microsoft Surface devices, with the exception of the Surface Go series. This EC provides various functionality, depending on the device in question. This can include battery status and thermal reporting (5th and later generations), but also HID keyboard (6th+) and touchpad input (7th+) on Surface Laptop and Surface Book 3 series devices.

This patch provides the basic necessities for communication with the SAM EC on 5th and later generation devices. On these devices, the EC provides an interface that acts as serial device, called the Surface Serial Hub (SSH). 4th generation devices, on which the EC interface is provided via an HID-over-I2C device, are not supported by this patch.

Specifically, this patch adds a driver for the SSH device (device HID MSHW0084 in ACPI), as well as a controller structure and associated API. This represents the functional core of the Surface Aggregator kernel subsystem, introduced with this patch, and will be expanded upon in subsequent commits.

The SSH driver acts as the main attachment point for this subsystem and sets-up and manages the controller structure. The controller in turn provides a basic communication interface, allowing to send requests from host to EC and receiving the corresponding responses, as well as managing and receiving events, sent from EC to host. It is structured into multiple layers, with the top layer presenting the API used by other kernel drivers and the lower layers modeled after the serial protocol used for communication.

Said other drivers are then responsible for providing the (Surface model specific) functionality accessible through the EC (e.g. battery status reporting, thermal information, ...) via said controller structure and API, and will be added in future commits.

---

Changes from RFC:
- add copyright lines
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- fix bug in __ssh_ptl_queue_push, potentially leading to a deadlock in high-traffic situations
- remove unnecessary READ_ONCE on multiple occasions
- add NULL checks for ssh_packet_get, ssh_packet_put
- add NULL checks for ssh_request_get, ssh_request_put
- guard dev_pm_ops with CONFIG_PM_SLEEP check
- mark struct ssam_request pointer as const when possible
- introduce generic retry macro with defaults
- retry base controller requests on failure

---
 MAINTAINERS                                   |    8 +
 drivers/platform/surface/Kconfig              |    2 +
 drivers/platform/surface/Makefile             |    1 +
 drivers/platform/surface/aggregator/Kconfig   |   39 +
 drivers/platform/surface/aggregator/Makefile  |   10 +
 .../platform/surface/aggregator/controller.c  | 2482 +++++++++++++++++
 .../platform/surface/aggregator/controller.h  |  279 ++
 drivers/platform/surface/aggregator/core.c    |  779 ++++++
 .../platform/surface/aggregator/ssh_msgb.h    |  201 ++
 .../surface/aggregator/ssh_packet_layer.c     | 1661 +++++++++++
 .../surface/aggregator/ssh_packet_layer.h     |  172 ++
 .../platform/surface/aggregator/ssh_parser.c  |  229 ++
 .../platform/surface/aggregator/ssh_parser.h  |  157 ++
 .../surface/aggregator/ssh_request_layer.c    | 1200 ++++++++
 .../surface/aggregator/ssh_request_layer.h    |  142 +
 include/linux/surface_aggregator/controller.h |  832 ++++++
 include/linux/surface_aggregator/serial_hub.h |  659 +++++
 17 files changed, 8853 insertions(+)
 create mode 100644 drivers/platform/surface/aggregator/Kconfig
 create mode 100644 drivers/platform/surface/aggregator/Makefile
 create mode 100644 drivers/platform/surface/aggregator/controller.c
 create mode 100644 drivers/platform/surface/aggregator/controller.h
 create mode 100644 drivers/platform/surface/aggregator/core.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_msgb.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_parser.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_parser.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.h
 create mode 100644 include/linux/surface_aggregator/controller.h
 create mode 100644 include/linux/surface_aggregator/serial_hub.h
MAINTAINERS +8 -0
index 0e54a5841b98..f0ebf547d984 100644
@@ -11692,6 +11692,14 @@ L:	[email protected]
 S:	Supported
 F:	drivers/platform/surface/surfacepro3_button.c
 
+MICROSOFT SURFACE SYSTEM AGGREGATOR SUBSYSTEM
+M:	Maximilian Luz <[email protected]>
+S:	Maintained
+W:	https://github.com/linux-surface/surface-aggregator-module
+C:	irc://chat.freenode.net/##linux-surface
+F:	drivers/platform/surface/aggregator/
+F:	include/linux/surface_aggregator/
+
 MICROTEK X6 SCANNER
 M:	Oliver Neukum <[email protected]>
 S:	Maintained
drivers/platform/surface/Kconfig +2 -0
index 33040b0b3b79..5af22f5141ff 100644
@@ -56,4 +56,6 @@ config SURFACE_PRO3_BUTTON
 	help
 	  This driver handles the power/home/volume buttons on the Microsoft Surface Pro 3/4 tablet.
 
+source "drivers/platform/surface/aggregator/Kconfig"
+
 endif # SURFACE_PLATFORMS
drivers/platform/surface/Makefile +1 -0
index cedfb027ded1..034134fe0264 100644
@@ -7,5 +7,6 @@
 obj-$(CONFIG_SURFACE3_WMI)		+= surface3-wmi.o
 obj-$(CONFIG_SURFACE_3_BUTTON)		+= surface3_button.o
 obj-$(CONFIG_SURFACE_3_POWER_OPREGION)	+= surface3_power.o
+obj-$(CONFIG_SURFACE_AGGREGATOR)	+= aggregator/
 obj-$(CONFIG_SURFACE_GPE)		+= surface_gpe.o
 obj-$(CONFIG_SURFACE_PRO3_BUTTON)	+= surfacepro3_button.o
drivers/platform/surface/aggregator/Kconfig new file +39 -0
new file mode 100644
index 000000000000..e0be15109e2f
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (C) 2019-2020 Maximilian Luz <[email protected]>
+
+menuconfig SURFACE_AGGREGATOR
+	tristate "Microsoft Surface System Aggregator Module Subsystem and Drivers"
+	depends on SERIAL_DEV_BUS
+	depends on ACPI
+	select CRC_CCITT
+	help
+	  The Surface System Aggregator Module (Surface SAM or SSAM) is an
+	  embedded controller (EC) found on 5th- and later-generation Microsoft
+	  Surface devices (i.e. Surface Pro 5, Surface Book 2, Surface Laptop,
+	  and newer, with exception of Surface Go series devices).
+
+	  Depending on the device in question, this EC provides varying
+	  functionality, including:
+	  - EC access from ACPI via Surface ACPI Notify (5th- and 6th-generation)
+	  - battery status information (all devices)
+	  - thermal sensor access (all devices)
+	  - performance mode / cooling mode control (all devices)
+	  - clipboard detachment system control (Surface Book 2 and 3)
+	  - HID / keyboard input (Surface Laptops, Surface Book 3)
+
+	  This option controls whether the Surface SAM subsystem core will be
+	  built. This includes a driver for the Surface Serial Hub (SSH), which
+	  is the device responsible for the communication with the EC, and a
+	  basic kernel interface exposing the EC functionality to other client
+	  drivers, i.e. allowing them to make requests to the EC and receive
+	  events from it. Selecting this option alone will not provide any
+	  client drivers and therefore no functionality beyond the in-kernel
+	  interface. Said functionality is the responsibility of the respective
+	  client drivers.
+
+	  Note: While 4th-generation Surface devices also make use of a SAM EC,
+	  due to a difference in the communication interface of the controller,
+	  only 5th and later generations are currently supported. Specifically,
+	  devices using SAM-over-SSH are supported, whereas devices using
+	  SAM-over-HID, which is used on the 4th generation, are currently not
+	  supported.
drivers/platform/surface/aggregator/Makefile new file +10 -0
new file mode 100644
index 000000000000..faad18d4a7f2
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (C) 2019-2020 Maximilian Luz <[email protected]>
+
+obj-$(CONFIG_SURFACE_AGGREGATOR) += surface_aggregator.o
+
+surface_aggregator-objs := core.o
+surface_aggregator-objs += ssh_parser.o
+surface_aggregator-objs += ssh_packet_layer.o
+surface_aggregator-objs += ssh_request_layer.o
+surface_aggregator-objs += controller.o
drivers/platform/surface/aggregator/controller.c new file +2482 -0

2,482 lines not on this page · raw message

drivers/platform/surface/aggregator/controller.h new file +279 -0

279 lines not on this page · raw message

drivers/platform/surface/aggregator/core.c new file +779 -0

164 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_msgb.h new file +201 -0

201 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_packet_layer.c new file +1661 -0

1,661 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_packet_layer.h new file +172 -0

172 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_parser.c new file +229 -0

229 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_parser.h new file +157 -0

157 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_request_layer.c new file +1200 -0

1,200 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_request_layer.h new file +142 -0

142 lines not on this page · raw message

include/linux/surface_aggregator/controller.h new file +832 -0

832 lines not on this page · raw message

include/linux/surface_aggregator/serial_hub.h new file +659 -0

659 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.29.2

Part of the diff is not shown here. View the raw message.

author maintainer
#11 lore raw

PATCH 4/9 platform/surface: aggregator: Add trace points

In reply to Maximilian Luz

Add trace points to the Surface Aggregator subsystem core. These trace points can be used to track packets, requests, and allocations. They are further intended for debugging and testing/validation, specifically in combination with the error injection capabilities introduced in the subsequent commit.

---

Changes from RFC:
- add copyright line
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- pack tracing structs

---
 drivers/platform/surface/aggregator/Makefile  |   3 +
 .../platform/surface/aggregator/controller.c  |   5 +
 drivers/platform/surface/aggregator/core.c    |   3 +
 .../surface/aggregator/ssh_packet_layer.c     |  21 +
 .../surface/aggregator/ssh_request_layer.c    |  18 +
 drivers/platform/surface/aggregator/trace.h   | 616 ++++++++++++++++++
 6 files changed, 666 insertions(+)
 create mode 100644 drivers/platform/surface/aggregator/trace.h
drivers/platform/surface/aggregator/Makefile +3 -0
index faad18d4a7f2..b8b24c8ec310 100644
@@ -1,6 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0+
 # Copyright (C) 2019-2020 Maximilian Luz <[email protected]>
 
+# For include/trace/define_trace.h to include trace.h
+CFLAGS_core.o = -I$(src)
+
 obj-$(CONFIG_SURFACE_AGGREGATOR) += surface_aggregator.o
 
 surface_aggregator-objs := core.o
drivers/platform/surface/aggregator/controller.c +5 -0
index 1c7d2d7b29f7..feee7c7c5945 100644
@@ -29,6 +29,8 @@
 #include "ssh_msgb.h"
 #include "ssh_request_layer.h"
 
+#include "trace.h"
+
 
 /* -- Safe counters. -------------------------------------------------------- */
 
@@ -590,6 +592,7 @@ static void __ssam_event_item_free_generic(struct ssam_event_item *item)
  */
 static void ssam_event_item_free(struct ssam_event_item *item)
 {
+	trace_ssam_event_item_free(item);
 	item->ops.free(item);
 }
 
@@ -625,6 +628,8 @@ static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
 	}
 
 	item->event.length = len;
+
+	trace_ssam_event_item_alloc(item, len);
 	return item;
 }
 
drivers/platform/surface/aggregator/core.c +3 -0
index 733a7d46ee07..9f5a35a741cd 100644
@@ -24,6 +24,9 @@
 #include <linux/surface_aggregator/controller.h>
 #include "controller.h"
 
+#define CREATE_TRACE_POINTS
+#include "trace.h"
+
 
 /* -- Static controller reference. ------------------------------------------ */
 
drivers/platform/surface/aggregator/ssh_packet_layer.c +21 -0
index 920e0d75c641..fe3c267b4811 100644
@@ -24,6 +24,8 @@
 #include "ssh_packet_layer.h"
 #include "ssh_parser.h"
 
+#include "trace.h"
+
 
 /*
  * To simplify reasoning about the code below, we define a few concepts. The
@@ -214,6 +216,8 @@ static void __ssh_ptl_packet_release(struct kref *kref)
 {
 	struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
 
+	trace_ssam_packet_release(p);
+
 	ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
 	p->ops->release(p);
 }
@@ -344,6 +348,7 @@ static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
 	buffer->ptr = (u8 *)(*packet + 1);
 	buffer->len = SSH_MSG_LEN_CTRL;
 
+	trace_ssam_ctrl_packet_alloc(*packet, buffer->len);
 	return 0;
 }
 
@@ -353,6 +358,7 @@ static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
  */
 static void ssh_ctrl_packet_free(struct ssh_packet *p)
 {
+	trace_ssam_ctrl_packet_free(p);
 	kmem_cache_free(ssh_ctrl_packet_cache, p);
 }
 
@@ -557,6 +563,7 @@ static void __ssh_ptl_complete(struct ssh_packet *p, int status)
 {
 	struct ssh_ptl *ptl = READ_ONCE(p->ptl);
 
+	trace_ssam_packet_complete(p, status);
 	ptl_dbg_cond(ptl, "ptl: completing packet %p (status: %d)\n", p, status);
 
 	if (p->ops->complete)
@@ -975,6 +982,8 @@ int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p)
 	struct ssh_ptl *ptl_old;
 	int status;
 
+	trace_ssam_packet_submit(p);
+
 	// validate packet fields
 	if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &p->state)) {
 		if (p->data.ptr || test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state))
@@ -1009,6 +1018,8 @@ static int __ssh_ptl_resubmit(struct ssh_packet *packet)
 {
 	int status;
 
+	trace_ssam_packet_resubmit(packet);
+
 	spin_lock(&packet->ptl->queue.lock);
 
 	status = __ssh_ptl_queue_push(packet);
@@ -1101,6 +1112,8 @@ void ssh_ptl_cancel(struct ssh_packet *p)
 	if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
 		return;
 
+	trace_ssam_packet_cancel(p);
+
 	/*
 	 * Lock packet and commit with memory barrier. If this packet has
 	 * already been locked, it's going to be removed and completed by
@@ -1154,6 +1167,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
 	ktime_t next = KTIME_MAX;
 	bool resub = false;
 
+	trace_ssam_ptl_timeout_reap("pending", atomic_read(&ptl->pending.count));
+
 	/*
 	 * Mark reaper as "not pending". This is done before checking any
 	 * packets to avoid lost-update type problems.
@@ -1185,6 +1200,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
 		// check if we still have some tries left
 		try = ssh_packet_priority_get_try(READ_ONCE(p->priority));
 		if (likely(try < SSH_PTL_MAX_PACKET_TRIES)) {
+			trace_ssam_packet_timeout(p);
+
 			/*
 			 * Submission fails if the packet has been locked, is
 			 * already queued, or the layer is being shut down.
@@ -1202,6 +1219,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
 		if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
 			continue;
 
+		trace_ssam_packet_timeout(p);
+
 		/*
 		 * We have now marked the packet as locked. Thus it cannot be
 		 * added to the pending list again after we've removed it here.
@@ -1366,6 +1385,8 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
 	if (!frame)	// not enough data
 		return aligned.ptr - source->ptr;
 
+	trace_ssam_rx_frame_received(frame);
+
 	switch (frame->type) {
 	case SSH_FRAME_TYPE_ACK:
 		ssh_ptl_acknowledge(ptl, frame->seq);
drivers/platform/surface/aggregator/ssh_request_layer.c +18 -0
index 2b0368bb2d79..0d3384fd2d4c 100644
@@ -21,6 +21,8 @@
 #include "ssh_packet_layer.h"
 #include "ssh_request_layer.h"
 
+#include "trace.h"
+
 
 /*
  * SSH_RTL_REQUEST_TIMEOUT - Request timeout.
@@ -141,6 +143,8 @@ static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
 {
 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
 
+	trace_ssam_request_complete(rqst, status);
+
 	// rtl/ptl may not be set if we're cancelling before submitting
 	rtl_dbg_cond(rtl, "rtl: completing request (rqid: 0x%04x, status: %d)\n",
 		     ssh_request_get_rqid_safe(rqst), status);
@@ -154,6 +158,8 @@ static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
 {
 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
 
+	trace_ssam_request_complete(rqst, 0);
+
 	rtl_dbg(rtl, "rtl: completing request with response (rqid: 0x%04x)\n",
 		ssh_request_get_rqid(rqst));
 
@@ -326,6 +332,8 @@ static void ssh_rtl_tx_work_fn(struct work_struct *work)
  */
 int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
 {
+	trace_ssam_request_submit(rqst);
+
 	/*
 	 * Ensure that requests expecting a response are sequenced. If this
 	 * invariant ever changes, see the comment in ssh_rtl_complete() on what
@@ -438,6 +446,8 @@ static void ssh_rtl_complete(struct ssh_rtl *rtl,
 	struct ssh_request *p, *n;
 	u16 rqid = get_unaligned_le16(&command->rqid);
 
+	trace_ssam_rx_response_received(command, command_data->len);
+
 	/*
 	 * Get request from pending based on request ID and mark it as response
 	 * received and locked.
@@ -688,6 +698,8 @@ bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
 	if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
 		return true;
 
+	trace_ssam_request_cancel(rqst);
+
 	if (pending)
 		canceled = ssh_rtl_cancel_pending(rqst);
 	else
@@ -777,6 +789,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
 	ktime_t timeout = rtl->rtx_timeout.timeout;
 	ktime_t next = KTIME_MAX;
 
+	trace_ssam_rtl_timeout_reap("pending", atomic_read(&rtl->pending.count));
+
 	/*
 	 * Mark reaper as "not pending". This is done before checking any
 	 * requests to avoid lost-update type problems.
@@ -825,6 +839,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
 
 	// cancel and complete the request
 	list_for_each_entry_safe(r, n, &claimed, node) {
+		trace_ssam_request_timeout(r);
+
 		/*
 		 * At this point we've removed the packet from pending. This
 		 * means that we've obtained the last (only) reference of the
@@ -850,6 +866,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
 static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
 			     const struct ssam_span *data)
 {
+	trace_ssam_rx_event_received(cmd, data->len);
+
 	rtl_dbg(rtl, "rtl: handling event (rqid: 0x%04x)\n",
 		get_unaligned_le16(&cmd->rqid));
 
drivers/platform/surface/aggregator/trace.h new file +616 -0

616 lines not on this page · raw message

Signature · 1 line
2.29.2
author maintainer
#15 lore raw

PATCH 6/9 platform/surface: aggregator: Add dedicated bus and device type

In reply to Maximilian Luz

The Surface Aggregator EC provides varying functionality, depending on the Surface device. To manage this functionality, we use dedicated client devices for each subsystem or virtual device of the EC. While some of these clients are described as standard devices in ACPI and the corresponding client drivers can be implemented as platform drivers in the kernel (making use of the controller API already present), many devices, especially on newer Surface models, cannot be found there.

To simplify management of these devices, we introduce a new bus and client device type for the Surface Aggregator subsystem. The new device type takes care of managing the controller reference, essentially guaranteeing its validity for as long as the client device exists, thus alleviating the need to manually establish device links for that purpose in the client driver (as has to be done with the platform devices).

---

Changes from RFC:
- add copyright lines
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- remove unnecessary READ_ONCE on multiple occasions
- improve documentation of special values for SSAM_DEVICE()
- add NULL checks for ssam_device_get, ssam_device_put

---
 drivers/platform/surface/aggregator/Kconfig  |  12 +
 drivers/platform/surface/aggregator/Makefile |   4 +
 drivers/platform/surface/aggregator/bus.c    | 424 ++++++++++++++++++
 drivers/platform/surface/aggregator/bus.h    |  27 ++
 drivers/platform/surface/aggregator/core.c   |  12 +
 include/linux/mod_devicetable.h              |  18 +
 include/linux/surface_aggregator/device.h    | 430 +++++++++++++++++++
 scripts/mod/devicetable-offsets.c            |   8 +
 scripts/mod/file2alias.c                     |  23 +
 9 files changed, 958 insertions(+)
 create mode 100644 drivers/platform/surface/aggregator/bus.c
 create mode 100644 drivers/platform/surface/aggregator/bus.h
 create mode 100644 include/linux/surface_aggregator/device.h
drivers/platform/surface/aggregator/Kconfig +12 -0
index 29cb28f2ab7e..111d703ed1c0 100644
@@ -38,6 +38,18 @@ menuconfig SURFACE_AGGREGATOR
 	  SAM-over-HID, which is used on the 4th generation, are currently not
 	  supported.
 
+config SURFACE_AGGREGATOR_BUS
+	bool "Surface System Aggregator Module Bus"
+	depends on SURFACE_AGGREGATOR
+	default y
+	help
+	  Expands the Surface System Aggregator Module (SSAM) core driver by
+	  providing a dedicated bus and client-device type.
+
+	  This bus and device type are intended to provide and simplify support
+	  for non-platform and non-ACPI SSAM devices, i.e. SSAM devices that are
+	  not auto-detectable via the conventional means (e.g. ACPI).
+
 config SURFACE_AGGREGATOR_ERROR_INJECTION
 	bool "Surface System Aggregator Module Error Injection Capabilities"
 	depends on SURFACE_AGGREGATOR
drivers/platform/surface/aggregator/Makefile +4 -0
index b8b24c8ec310..c112e2c7112b 100644
@@ -11,3 +11,7 @@ surface_aggregator-objs += ssh_parser.o
 surface_aggregator-objs += ssh_packet_layer.o
 surface_aggregator-objs += ssh_request_layer.o
 surface_aggregator-objs += controller.o
+
+ifeq ($(CONFIG_SURFACE_AGGREGATOR_BUS),y)
+surface_aggregator-objs += bus.o
+endif
drivers/platform/surface/aggregator/bus.c new file +424 -0

424 lines not on this page · raw message

drivers/platform/surface/aggregator/bus.h new file +27 -0
new file mode 100644
index 000000000000..7712baaed6a5
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Surface System Aggregator Module bus and device integration.
+ *
+ * Copyright (C) 2019-2020 Maximilian Luz <[email protected]>
+ */
+
+#ifndef _SURFACE_AGGREGATOR_BUS_H
+#define _SURFACE_AGGREGATOR_BUS_H
+
+#include <linux/surface_aggregator/controller.h>
+
+#ifdef CONFIG_SURFACE_AGGREGATOR_BUS
+
+void ssam_controller_remove_clients(struct ssam_controller *ctrl);
+
+int ssam_bus_register(void);
+void ssam_bus_unregister(void);
+
+#else /* CONFIG_SURFACE_AGGREGATOR_BUS */
+
+static inline void ssam_controller_remove_clients(struct ssam_controller *ctrl) {}
+static inline int ssam_bus_register(void) { return 0; }
+static inline void ssam_bus_unregister(void) {}
+
+#endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
+#endif /* _SURFACE_AGGREGATOR_BUS_H */
drivers/platform/surface/aggregator/core.c +12 -0
index 9f5a35a741cd..6aaac82b9091 100644
@@ -22,6 +22,8 @@
 #include <linux/sysfs.h>
 
 #include <linux/surface_aggregator/controller.h>
+
+#include "bus.h"
 #include "controller.h"
 
 #define CREATE_TRACE_POINTS
@@ -730,6 +732,9 @@ static void ssam_serial_hub_remove(struct serdev_device *serdev)
 	sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
 	ssam_controller_lock(ctrl);
 
+	// remove all client devices
+	ssam_controller_remove_clients(ctrl);
+
 	// act as if suspending to disable events
 	status = ssam_ctrl_notif_display_off(ctrl);
 	if (status) {
@@ -783,6 +788,10 @@ static int __init ssam_core_init(void)
 {
 	int status;
 
+	status = ssam_bus_register();
+	if (status)
+		goto err_bus;
+
 	status = ssh_ctrl_packet_cache_init();
 	if (status)
 		goto err_cpkg;
@@ -802,6 +811,8 @@ static int __init ssam_core_init(void)
 err_evitem:
 	ssh_ctrl_packet_cache_destroy();
 err_cpkg:
+	ssam_bus_unregister();
+err_bus:
 	return status;
 }
 module_init(ssam_core_init);
@@ -811,6 +822,7 @@ static void __exit ssam_core_exit(void)
 	serdev_device_driver_unregister(&ssam_serial_hub);
 	ssam_event_item_cache_destroy();
 	ssh_ctrl_packet_cache_destroy();
+	ssam_bus_unregister();
 }
 module_exit(ssam_core_exit);
 
include/linux/mod_devicetable.h +18 -0
index 5b08a473cdba..0b8f1feefe0e 100644
@@ -838,4 +838,22 @@ struct mhi_device_id {
 	kernel_ulong_t driver_data;
 };
 
+/* Surface System Aggregator Module */
+
+#define SSAM_MATCH_TARGET	0x1
+#define SSAM_MATCH_INSTANCE	0x2
+#define SSAM_MATCH_FUNCTION	0x4
+
+struct ssam_device_id {
+	__u8 match_flags;
+
+	__u8 domain;
+	__u8 category;
+	__u8 target;
+	__u8 instance;
+	__u8 function;
+
+	kernel_ulong_t driver_data;
+};
+
 #endif /* LINUX_MOD_DEVICETABLE_H */
include/linux/surface_aggregator/device.h new file +430 -0

430 lines not on this page · raw message

scripts/mod/devicetable-offsets.c +8 -0
index 27007c18e754..4339377ad929 100644
@@ -243,5 +243,13 @@ int main(void)
 	DEVID(mhi_device_id);
 	DEVID_FIELD(mhi_device_id, chan);
 
+	DEVID(ssam_device_id);
+	DEVID_FIELD(ssam_device_id, match_flags);
+	DEVID_FIELD(ssam_device_id, domain);
+	DEVID_FIELD(ssam_device_id, category);
+	DEVID_FIELD(ssam_device_id, target);
+	DEVID_FIELD(ssam_device_id, instance);
+	DEVID_FIELD(ssam_device_id, function);
+
 	return 0;
 }
scripts/mod/file2alias.c +23 -0
index 2417dd1dee33..5b79fdc42641 100644
@@ -1368,6 +1368,28 @@ static int do_mhi_entry(const char *filename, void *symval, char *alias)
 	return 1;
 }
 
+/*
+ * Looks like: ssam:dNcNtNiNfN
+ *
+ * N is exactly 2 digits, where each is an upper-case hex digit.
+ */
+static int do_ssam_entry(const char *filename, void *symval, char *alias)
+{
+	DEF_FIELD(symval, ssam_device_id, match_flags);
+	DEF_FIELD(symval, ssam_device_id, domain);
+	DEF_FIELD(symval, ssam_device_id, category);
+	DEF_FIELD(symval, ssam_device_id, target);
+	DEF_FIELD(symval, ssam_device_id, instance);
+	DEF_FIELD(symval, ssam_device_id, function);
+
+	sprintf(alias, "ssam:d%02Xc%02X", domain, category);
+	ADD(alias, "t", match_flags & SSAM_MATCH_TARGET, target);
+	ADD(alias, "i", match_flags & SSAM_MATCH_INSTANCE, instance);
+	ADD(alias, "f", match_flags & SSAM_MATCH_FUNCTION, function);
+
+	return 1;
+}
+
 /* Does namelen bytes of name exactly match the symbol? */
 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
 {
@@ -1442,6 +1464,7 @@ static const struct devtable devtable[] = {
 	{"tee", SIZE_tee_client_device_id, do_tee_entry},
 	{"wmi", SIZE_wmi_device_id, do_wmi_entry},
 	{"mhi", SIZE_mhi_device_id, do_mhi_entry},
+	{"ssam", SIZE_ssam_device_id, do_ssam_entry},
 };
 
 /* Create MODULE_ALIAS() statements.
Signature · 1 line
2.29.2
author maintainer
#14 lore raw

PATCH 5/9 platform/surface: aggregator: Add error injection capabilities

In reply to Maximilian Luz

This commit adds error injection hooks to the Surface Serial Hub communication protocol implementation, to:

- simulate simple serial transmission errors,

- drop packets, requests, and responses, simulating communication failures and potentially trigger retransmission timeouts, as well as

- inject invalid data into submitted and received packets.

Together with the trace points introduced in the previous commit, these facilities are intended to aid in testing, validation, and debugging of the Surface Aggregator communication layer.

---

Changes from RFC:
- remove unnecessary default in Kconfig entry

---
 drivers/platform/surface/aggregator/Kconfig   |  14 +
 .../surface/aggregator/ssh_packet_layer.c     | 302 +++++++++++++++++-
 .../surface/aggregator/ssh_request_layer.c    |  36 +++
 drivers/platform/surface/aggregator/trace.h   |   9 +
 4 files changed, 359 insertions(+), 2 deletions(-)
drivers/platform/surface/aggregator/Kconfig +14 -0
index e0be15109e2f..29cb28f2ab7e 100644
@@ -37,3 +37,17 @@ menuconfig SURFACE_AGGREGATOR
 	  devices using SAM-over-SSH are supported, whereas devices using
 	  SAM-over-HID, which is used on the 4th generation, are currently not
 	  supported.
+
+config SURFACE_AGGREGATOR_ERROR_INJECTION
+	bool "Surface System Aggregator Module Error Injection Capabilities"
+	depends on SURFACE_AGGREGATOR
+	depends on FUNCTION_ERROR_INJECTION
+	help
+	  Provides error-injection capabilities for the Surface System
+	  Aggregator Module subsystem and Surface Serial Hub driver.
+
+	  Specifically, exports error injection hooks to be used with the
+	  kernel's function error injection capabilities to simulate underlying
+	  transport and communication problems, such as invalid data sent to or
+	  received from the EC, dropped data, and communication timeouts.
+	  Intended for development and debugging.
drivers/platform/surface/aggregator/ssh_packet_layer.c +300 -2

345 lines not on this page · raw message

drivers/platform/surface/aggregator/ssh_request_layer.c +36 -0
index 0d3384fd2d4c..f47bd949b6c3 100644
@@ -8,6 +8,7 @@
 #include <asm/unaligned.h>
 #include <linux/atomic.h>
 #include <linux/completion.h>
+#include <linux/error-injection.h>
 #include <linux/ktime.h>
 #include <linux/list.h>
 #include <linux/slab.h>
@@ -52,6 +53,31 @@
 #define SSH_RTL_MAX_PENDING		3
 
 
+#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
+
+/**
+ * ssh_rtl_should_drop_response() - Error injection hook to drop request
+ * responses.
+ *
+ * Useful to cause request transmission timeouts in the driver by dropping the
+ * response to a request.
+ */
+static noinline bool ssh_rtl_should_drop_response(void)
+{
+	return false;
+}
+ALLOW_ERROR_INJECTION(ssh_rtl_should_drop_response, TRUE);
+
+#else
+
+static inline bool ssh_rtl_should_drop_response(void)
+{
+	return false;
+}
+
+#endif
+
+
 static u16 ssh_request_get_rqid(struct ssh_request *rqst)
 {
 	return get_unaligned_le16(rqst->packet.data.ptr
@@ -458,6 +484,16 @@ static void ssh_rtl_complete(struct ssh_rtl *rtl,
 		if (unlikely(ssh_request_get_rqid(p) != rqid))
 			continue;
 
+		// simulate response timeout
+		if (ssh_rtl_should_drop_response()) {
+			spin_unlock(&rtl->pending.lock);
+
+			trace_ssam_ei_rx_drop_response(p);
+			rtl_info(rtl, "request error injection: dropping response for request %p\n",
+				 &p->packet);
+			return;
+		}
+
 		/*
 		 * Mark as "response received" and "locked" as we're going to
 		 * complete it.
drivers/platform/surface/aggregator/trace.h +9 -0
index ead052c1ffab..232bf1142aae 100644
@@ -598,6 +598,15 @@ DEFINE_SSAM_REQUEST_EVENT(request_cancel);
 DEFINE_SSAM_REQUEST_STATUS_EVENT(request_complete);
 DEFINE_SSAM_GENERIC_UINT_EVENT(rtl_timeout_reap);
 
+DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_ack_packet);
+DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_nak_packet);
+DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_dsq_packet);
+DEFINE_SSAM_PACKET_STATUS_EVENT(ei_tx_fail_write);
+DEFINE_SSAM_PACKET_EVENT(ei_tx_corrupt_data);
+DEFINE_SSAM_GENERIC_UINT_EVENT(ei_rx_corrupt_syn);
+DEFINE_SSAM_FRAME_EVENT(ei_rx_corrupt_data);
+DEFINE_SSAM_REQUEST_EVENT(ei_rx_drop_response);
+
 DEFINE_SSAM_ALLOC_EVENT(ctrl_packet_alloc);
 DEFINE_SSAM_FREE_EVENT(ctrl_packet_free);
 
Signature · 1 line
2.29.2
author maintainer
#20 lore raw

PATCH 9/9 platform/surface: Add Surface ACPI Notify driver

In reply to Maximilian Luz

The Surface ACPI Notify (SAN) device provides an ACPI interface to the Surface Aggregator EC, specifically the Surface Serial Hub interface. This interface allows EC requests to be made from ACPI code and can convert a subset of EC events back to ACPI notifications.

Specifically, this interface provides a GenericSerialBus operation region ACPI code can execute a request by writing the request command data and payload to this operation region and reading back the corresponding response via a write-then-read operation. Furthermore, this interface provides a _DSM method to be called when certain events from the EC have been received, essentially turning them into ACPI notifications.

The driver provided in this commit essentially takes care of translating the request data written to the operation region, executing the request, waiting for it to finish, and finally writing and translating back the response (if the request has one). Furthermore, this driver takes care of enabling the events handled via ACPI _DSM calls. Lastly, this driver also exposes an interface providing discrete GPU (dGPU) power-on notifications on the Surface Book 2, which are also received via the operation region interface (but not handled by the SAN driver directly), making them accessible to other drivers (such as a dGPU hot-plug driver that may be added later on).

On 5th and 6th generation Surface devices (Surface Pro 5/2017, Pro 6, Book 2, Laptop 1 and 2), the SAN interface provides full battery and thermal subsystem access, as well as other EC based functionality. On those models, battery and thermal sensor devices are implemented as standard ACPI devices of that type, however, forward ACPI calls to the corresponding Surface Aggregator EC request via the SAN interface and receive corresponding notifications (e.g. battery information change) from it. This interface is therefore required to provide said functionality on those devices.

---

Changes from RFC:
- add copyright lines
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- fix invalid pointer calculation in san_evt_tmp
- add explicit dependency on CONFIG_ACPI
- remove default in Kconfig

---
 .../surface_aggregator/clients/index.rst      |   1 +
 .../surface_aggregator/clients/san.rst        |  44 +
 MAINTAINERS                                   |   2 +
 drivers/platform/surface/Kconfig              |  20 +
 drivers/platform/surface/Makefile             |   1 +
 .../platform/surface/surface_acpi_notify.c    | 884 ++++++++++++++++++
 include/linux/surface_acpi_notify.h           |  39 +
 7 files changed, 991 insertions(+)
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/san.rst
 create mode 100644 drivers/platform/surface/surface_acpi_notify.c
 create mode 100644 include/linux/surface_acpi_notify.h
Documentation/driver-api/surface_aggregator/clients/index.rst +1 -0
index ab260ec82cfb..3ccabce23271 100644
@@ -11,6 +11,7 @@ This is the documentation for client drivers themselves. Refer to
    :maxdepth: 1
 
    cdev
+   san
 
 .. only::  subproject and html
 
Documentation/driver-api/surface_aggregator/clients/san.rst new file +44 -0

44 lines not on this page · raw message

MAINTAINERS +2 -0
index c776343a49c6..f15431cbf8ff 100644
@@ -11699,7 +11699,9 @@ W:	https://github.com/linux-surface/surface-aggregator-module
 C:	irc://chat.freenode.net/##linux-surface
 F:	Documentation/driver-api/surface_aggregator/
 F:	drivers/platform/surface/aggregator/
+F:	drivers/platform/surface/surface_acpi_notify.c
 F:	drivers/platform/surface/surface_aggregator_cdev.c
+F:	include/linux/surface_acpi_notify.h
 F:	include/linux/surface_aggregator/
 F:	include/uapi/linux/surface_aggregator/
 
drivers/platform/surface/Kconfig +20 -0
index cecad7a0cb7b..3b0da86546fa 100644
@@ -40,6 +40,26 @@ config SURFACE_3_POWER_OPREGION
 	  This driver provides support for ACPI operation
 	  region of the Surface 3 battery platform driver.
 
+config SURFACE_ACPI_NOTIFY
+	tristate "Surface ACPI Notify Driver"
+	depends on ACPI
+	depends on SURFACE_AGGREGATOR
+	help
+	  Surface ACPI Notify (SAN) driver for Microsoft Surface devices.
+
+	  This driver provides support for the ACPI interface (called SAN) of
+	  the Surface System Aggregator Module (SSAM) EC. This interface is used
+	  on 5th- and 6th-generation Microsoft Surface devices (including
+	  Surface Pro 5 and 6, Surface Book 2, Surface Laptops 1 and 2, and in
+	  reduced functionality on the Surface Laptop 3) to execute SSAM
+	  requests directly from ACPI code, as well as receive SSAM events and
+	  turn them into ACPI notifications. It essentially acts as a
+	  translation layer between the SSAM controller and ACPI.
+
+	  Specifically, this driver may be needed for battery status reporting,
+	  thermal sensor access, and real-time clock information, depending on
+	  the Surface device in question.
+
 config SURFACE_AGGREGATOR_CDEV
 	tristate "Surface System Aggregator Module User-Space Interface"
 	depends on SURFACE_AGGREGATOR
drivers/platform/surface/Makefile +1 -0
index 161f0ad05795..3eb971006877 100644
@@ -7,6 +7,7 @@
 obj-$(CONFIG_SURFACE3_WMI)		+= surface3-wmi.o
 obj-$(CONFIG_SURFACE_3_BUTTON)		+= surface3_button.o
 obj-$(CONFIG_SURFACE_3_POWER_OPREGION)	+= surface3_power.o
+obj-$(CONFIG_SURFACE_ACPI_NOTIFY)	+= surface_acpi_notify.o
 obj-$(CONFIG_SURFACE_AGGREGATOR)	+= aggregator/
 obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV)	+= surface_aggregator_cdev.o
 obj-$(CONFIG_SURFACE_GPE)		+= surface_gpe.o
drivers/platform/surface/surface_acpi_notify.c new file +884 -0

884 lines not on this page · raw message

include/linux/surface_acpi_notify.h new file +39 -0
new file mode 100644
index 000000000000..8e3e86c7d78c
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Interface for Surface ACPI Notify (SAN) driver.
+ *
+ * Provides access to discrete GPU notifications sent from ACPI via the SAN
+ * driver, which are not handled by this driver directly.
+ *
+ * Copyright (C) 2019-2020 Maximilian Luz <[email protected]>
+ */
+
+#ifndef _LINUX_SURFACE_ACPI_NOTIFY_H
+#define _LINUX_SURFACE_ACPI_NOTIFY_H
+
+#include <linux/notifier.h>
+#include <linux/types.h>
+
+/**
+ * struct san_dgpu_event - Discrete GPU ACPI event.
+ * @category: Category of the event.
+ * @target:   Target ID of the event source.
+ * @command:  Command ID of the event.
+ * @instance: Instance ID of the event source.
+ * @length:   Length of the event's payload data (in bytes).
+ * @payload:  Pointer to the event's payload data.
+ */
+struct san_dgpu_event {
+	u8 category;
+	u8 target;
+	u8 command;
+	u8 instance;
+	u16 length;
+	u8 *payload;
+};
+
+int san_client_link(struct device *client);
+int san_dgpu_notifier_register(struct notifier_block *nb);
+int san_dgpu_notifier_unregister(struct notifier_block *nb);
+
+#endif /* _LINUX_SURFACE_ACPI_NOTIFY_H */
Signature · 1 line
2.29.2
author maintainer
#16 lore raw

PATCH 7/9 docs: driver-api: Add Surface Aggregator subsystem documentation

In reply to Maximilian Luz

Add documentation for the Surface Aggregator subsystem and its client drivers, giving an overview of the subsystem, its use-cases, its internal structure and internal API, as well as its external API for writing client drivers.

---

Changes from RFC:
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- fix typos

---
 Documentation/driver-api/index.rst            |   1 +
 .../surface_aggregator/client-api.rst         |  38 ++
 .../driver-api/surface_aggregator/client.rst  | 394 ++++++++++++++++++
 .../surface_aggregator/clients/index.rst      |  10 +
 .../driver-api/surface_aggregator/index.rst   |  21 +
 .../surface_aggregator/internal-api.rst       |  67 +++
 .../surface_aggregator/internal.rst           |  50 +++
 .../surface_aggregator/overview.rst           |  76 ++++
 .../driver-api/surface_aggregator/ssh.rst     | 343 +++++++++++++++
 MAINTAINERS                                   |   1 +
 10 files changed, 1001 insertions(+)
 create mode 100644 Documentation/driver-api/surface_aggregator/client-api.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/client.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/index.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/index.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/internal-api.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/internal.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/overview.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/ssh.rst
Documentation/driver-api/index.rst +1 -0
index f357f3eb400c..699dc7cac0fb 100644
@@ -97,6 +97,7 @@ available subsections can be seen below.
    rfkill
    serial/index
    sm501
+   surface_aggregator/index
    switchtec
    sync_file
    vfio-mediated-device
Documentation/driver-api/surface_aggregator/client-api.rst new file +38 -0
new file mode 100644
index 000000000000..8e0b000d0e64
@@ -0,0 +1,38 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+===============================
+Client Driver API Documentation
+===============================
+
+.. contents::
+    :depth: 2
+
+
+Serial Hub Communication
+========================
+
+.. kernel-doc:: include/linux/surface_aggregator/serial_hub.h
+
+.. kernel-doc:: drivers/platform/surface/aggregator/ssh_packet_layer.c
+    :export:
+
+
+Controller and Core Interface
+=============================
+
+.. kernel-doc:: include/linux/surface_aggregator/controller.h
+
+.. kernel-doc:: drivers/platform/surface/aggregator/controller.c
+    :export:
+
+.. kernel-doc:: drivers/platform/surface/aggregator/core.c
+    :export:
+
+
+Client Bus and Client Device API
+================================
+
+.. kernel-doc:: include/linux/surface_aggregator/device.h
+
+.. kernel-doc:: drivers/platform/surface/aggregator/bus.c
+    :export:
Documentation/driver-api/surface_aggregator/client.rst new file +394 -0

394 lines not on this page · raw message

Documentation/driver-api/surface_aggregator/clients/index.rst new file +10 -0
new file mode 100644
index 000000000000..31e026d96102
@@ -0,0 +1,10 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+===========================
+Client Driver Documentation
+===========================
+
+This is the documentation for client drivers themselves. Refer to
+:doc:`../client` for documentation on how to write client drivers.
+
+.. Place documentation for individual client drivers here.
Documentation/driver-api/surface_aggregator/index.rst new file +21 -0
new file mode 100644
index 000000000000..9fa70eedca59
@@ -0,0 +1,21 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+=======================================
+Surface System Aggregator Module (SSAM)
+=======================================
+
+.. toctree::
+   :maxdepth: 2
+
+   overview
+   ssh
+   client
+   internal
+   clients/index
+
+.. only::  subproject and html
+
+   Indices
+   =======
+
+   * :ref:`genindex`
Documentation/driver-api/surface_aggregator/internal-api.rst new file +67 -0

67 lines not on this page · raw message

Documentation/driver-api/surface_aggregator/internal.rst new file +50 -0

50 lines not on this page · raw message

Documentation/driver-api/surface_aggregator/overview.rst new file +76 -0

76 lines not on this page · raw message

Documentation/driver-api/surface_aggregator/ssh.rst new file +343 -0

343 lines not on this page · raw message

MAINTAINERS +1 -0
index f0ebf547d984..f0d23c113f3e 100644
@@ -11697,6 +11697,7 @@ M:	Maximilian Luz <[email protected]>
 S:	Maintained
 W:	https://github.com/linux-surface/surface-aggregator-module
 C:	irc://chat.freenode.net/##linux-surface
+F:	Documentation/driver-api/surface_aggregator/
 F:	drivers/platform/surface/aggregator/
 F:	include/linux/surface_aggregator/
 
Signature · 1 line
2.29.2
author maintainer
#17 lore raw

PATCH 8/9 platform/surface: Add Surface Aggregator user-space interface

In reply to Maximilian Luz

Add a misc-device providing user-space access to the Surface Aggregator EC, mainly intended for debugging, testing, and reverse-engineering. This interface gives user-space applications the ability to send requests to the EC and receive the corresponding responses.

The device-file is managed by a pseudo platform-device and corresponding driver to avoid dependence on the dedicated bus, allowing it to be loaded in a minimal configuration.

---

Changes from RFC:
- add copyright lines
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- move from debugfs to proper misc-device
- move interface definitions to uapi header
- remove versioning
- allocate platform device dynamically
- prioritize EFAULT error in IOCTL return
- use ENOTTY error for unknown IOCTLs
- fix IOCTL compatibility for 32-bit user-space on 64-bit kernels
- mark request struct as __attribute__((__packed__))
- address issues when driver is unbound while file still open

---
 .../surface_aggregator/clients/cdev.rst       |  85 +++++
 .../surface_aggregator/clients/index.rst      |  12 +-
 MAINTAINERS                                   |   2 +
 drivers/platform/surface/Kconfig              |  17 +
 drivers/platform/surface/Makefile             |   1 +
 .../surface/surface_aggregator_cdev.c         | 299 ++++++++++++++++++
 include/uapi/linux/surface_aggregator/cdev.h  |  58 ++++
 7 files changed, 473 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/cdev.rst
 create mode 100644 drivers/platform/surface/surface_aggregator_cdev.c
 create mode 100644 include/uapi/linux/surface_aggregator/cdev.h
Documentation/driver-api/surface_aggregator/clients/cdev.rst new file +85 -0
new file mode 100644
index 000000000000..720c9c03aa0e
@@ -0,0 +1,85 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+.. |u8| replace:: :c:type:`u8 <u8>`
+.. |u16| replace:: :c:type:`u16 <u16>`
+.. |ssam_cdev_request| replace:: :c:type:`struct ssam_cdev_request <ssam_cdev_request>`
+.. |ssam_request_flags| replace:: :c:type:`enum ssam_request_flags <ssam_request_flags>`
+
+==============================
+User-Space EC Interface (cdev)
+==============================
+
+The ``surface_aggregator_cdev`` module provides a misc-device for the SSAM
+controller to allow for a (more or less) direct connection from user-space to
+the SAM EC. It is intended to be used for development and debugging, and
+therefore should not be used or relied upon in any other way. Note that this
+module is not loaded automatically, but instead must be loaded manually.
+
+The provided interface is accessible through the ``/dev/surface/aggregator``
+device-file. All functionality of this interface is provided via IOCTLs.
+These IOCTLs and their respective input/output parameter structs are defined in
+``include/uapi/linux/surface_aggregator/cdev.h``.
+
+
+Controller IOCTLs
+=================
+
+The following IOCTLs are provided:
+
+.. flat-table:: Controller IOCTLs
+   :widths: 1 1 1 1 4
+   :header-rows: 1
+
+   * - Type
+     - Number
+     - Direction
+     - Name
+     - Description
+
+   * - ``0xA5``
+     - ``1``
+     - ``WR``
+     - ``REQUEST``
+     - Perform synchronous SAM request.
+
+
+``REQUEST``
+-----------
+
+Defined as ``_IOWR(0xA5, 1, struct ssam_cdev_request)``.
+
+Executes a synchronous SAM request. The request specification is passed in
+as argument of type |ssam_cdev_request|, which is then written to/modified
+by the IOCTL to return status and result of the request.
+
+Request payload data must be allocated separately and is passed in via the
+``payload.data`` and ``payload.length`` members. If a response is required,
+the response buffer must be allocated by the caller and passed in via the
+``response.data`` member. The ``response.length`` member must be set to the
+capacity of this buffer, or if no response is required, zero. Upon
+completion of the request, the call will write the response to the response
+buffer (if its capacity allows it) and overwrite the length field with the
+actual size of the response, in bytes.
+
+Additionally, if the request has a response, this should be indicated via
+the request flags, as is done with in-kernel requests. Request flags can be
+set via the ``flags`` member and the values correspond to the values found
+in |ssam_request_flags|.
+
+Finally, the status of the request itself is returned in the ``status``
+member (a negative value indicating failure). Note that failure indication
+of the IOCTL is separated from failure indication of the request: The IOCTL
+returns a negative status code if anything failed during setup of the
+request (``-EFAULT``) or if the provided argument or any of its fields are
+invalid (``-EINVAL``). In this case, the status value of the request
+argument may be set, providing more detail on what went wrong (e.g.
+``-ENOMEM`` for out-of-memory), but this value may also be zero. The IOCTL
+will return with a zero status code in case the request has been set up,
+submitted, and completed (i.e. handed back to user-space) successfully from
+inside the IOCTL, but the request ``status`` member may still be negative in
+case the actual execution of the request failed after it has been submitted.
+
+A full definition of the argument struct is provided below:
+
+.. kernel-doc:: include/uapi/linux/surface_aggregator/cdev.h
+   :functions: ssam_cdev_request
Documentation/driver-api/surface_aggregator/clients/index.rst +11 -1
index 31e026d96102..ab260ec82cfb 100644
@@ -7,4 +7,14 @@ Client Driver Documentation
 This is the documentation for client drivers themselves. Refer to
 :doc:`../client` for documentation on how to write client drivers.
 
-.. Place documentation for individual client drivers here.
+.. toctree::
+   :maxdepth: 1
+
+   cdev
+
+.. only::  subproject and html
+
+   Indices
+   =======
+
+   * :ref:`genindex`
MAINTAINERS +2 -0
index f0d23c113f3e..c776343a49c6 100644
@@ -11699,7 +11699,9 @@ W:	https://github.com/linux-surface/surface-aggregator-module
 C:	irc://chat.freenode.net/##linux-surface
 F:	Documentation/driver-api/surface_aggregator/
 F:	drivers/platform/surface/aggregator/
+F:	drivers/platform/surface/surface_aggregator_cdev.c
 F:	include/linux/surface_aggregator/
+F:	include/uapi/linux/surface_aggregator/
 
 MICROTEK X6 SCANNER
 M:	Oliver Neukum <[email protected]>
drivers/platform/surface/Kconfig +17 -0
index 5af22f5141ff..cecad7a0cb7b 100644
@@ -40,6 +40,23 @@ config SURFACE_3_POWER_OPREGION
 	  This driver provides support for ACPI operation
 	  region of the Surface 3 battery platform driver.
 
+config SURFACE_AGGREGATOR_CDEV
+	tristate "Surface System Aggregator Module User-Space Interface"
+	depends on SURFACE_AGGREGATOR
+	help
+	  Provides a misc-device interface to the Surface System Aggregator
+	  Module (SSAM) controller.
+
+	  This option provides a module (called surface_aggregator_cdev), that,
+	  when loaded, will add a client device (and its respective driver) to
+	  the SSAM controller. Said client device manages a misc-device
+	  interface (/dev/surface/aggregator), which can be used by user-space
+	  tools to directly communicate with the SSAM EC by sending requests and
+	  receiving the corresponding responses.
+
+	  The provided interface is intended for debugging and development only,
+	  and should not be used otherwise.
+
 config SURFACE_GPE
 	tristate "Surface GPE/Lid Support Driver"
 	depends on ACPI
drivers/platform/surface/Makefile +1 -0
index 034134fe0264..161f0ad05795 100644
@@ -8,5 +8,6 @@ obj-$(CONFIG_SURFACE3_WMI)		+= surface3-wmi.o
 obj-$(CONFIG_SURFACE_3_BUTTON)		+= surface3_button.o
 obj-$(CONFIG_SURFACE_3_POWER_OPREGION)	+= surface3_power.o
 obj-$(CONFIG_SURFACE_AGGREGATOR)	+= aggregator/
+obj-$(CONFIG_SURFACE_AGGREGATOR_CDEV)	+= surface_aggregator_cdev.o
 obj-$(CONFIG_SURFACE_GPE)		+= surface_gpe.o
 obj-$(CONFIG_SURFACE_PRO3_BUTTON)	+= surfacepro3_button.o
drivers/platform/surface/surface_aggregator_cdev.c new file +299 -0

299 lines not on this page · raw message

include/uapi/linux/surface_aggregator/cdev.h new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.29.2
#2 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

In reply to Maximilian Luz
Quoted 37 lines · Maximilian Luz

Add Surface System Aggregator Module core and Surface Serial Hub driver, required for the embedded controller found on Microsoft Surface devices.

The Surface System Aggregator Module (SSAM, SAM or Surface Aggregator) is an embedded controller (EC) found on 4th and later generation Microsoft Surface devices, with the exception of the Surface Go series. This EC provides various functionality, depending on the device in question. This can include battery status and thermal reporting (5th and later generations), but also HID keyboard (6th+) and touchpad input (7th+) on Surface Laptop and Surface Book 3 series devices.

This patch provides the basic necessities for communication with the SAM EC on 5th and later generation devices. On these devices, the EC provides an interface that acts as serial device, called the Surface Serial Hub (SSH). 4th generation devices, on which the EC interface is provided via an HID-over-I2C device, are not supported by this patch.

Specifically, this patch adds a driver for the SSH device (device HID MSHW0084 in ACPI), as well as a controller structure and associated API. This represents the functional core of the Surface Aggregator kernel subsystem, introduced with this patch, and will be expanded upon in subsequent commits.

The SSH driver acts as the main attachment point for this subsystem and sets-up and manages the controller structure. The controller in turn provides a basic communication interface, allowing to send requests from host to EC and receiving the corresponding responses, as well as managing and receiving events, sent from EC to host. It is structured into multiple layers, with the top layer presenting the API used by other kernel drivers and the lower layers modeled after the serial protocol used for communication.

Said other drivers are then responsible for providing the (Surface model specific) functionality accessible through the EC (e.g. battery status reporting, thermal information, ...) via said controller structure and API, and will be added in future commits.

...

Quoted 26 lines
drivers/platform/surface/aggregator/Kconfig new file +26 -0
+menuconfig SURFACE_AGGREGATOR
+       tristate "Microsoft Surface System Aggregator Module Subsystem and Drivers"
+       depends on SERIAL_DEV_BUS
+       depends on ACPI
+       select CRC_CCITT
+       help
+         The Surface System Aggregator Module (Surface SAM or SSAM) is an
+         embedded controller (EC) found on 5th- and later-generation Microsoft
+         Surface devices (i.e. Surface Pro 5, Surface Book 2, Surface Laptop,
+         and newer, with exception of Surface Go series devices).
+
+         Depending on the device in question, this EC provides varying
+         functionality, including:
+         - EC access from ACPI via Surface ACPI Notify (5th- and 6th-generation)
+         - battery status information (all devices)
+         - thermal sensor access (all devices)
+         - performance mode / cooling mode control (all devices)
+         - clipboard detachment system control (Surface Book 2 and 3)
+         - HID / keyboard input (Surface Laptops, Surface Book 3)
+
+         This option controls whether the Surface SAM subsystem core will be
+         built. This includes a driver for the Surface Serial Hub (SSH), which
+         is the device responsible for the communication with the EC, and a
+         basic kernel interface exposing the EC functionality to other client
+         drivers, i.e. allowing them to make requests to the EC and receive
+         events from it. Selecting this option alone will not provide any

drivers/platform/surface/aggregator/Kconfig line 39

Quoted 10 lines
drivers/platform/surface/aggregator/Kconfig new file +10 -0
+         client drivers and therefore no functionality beyond the in-kernel
+         interface. Said functionality is the responsibility of the respective
+         client drivers.
+
+         Note: While 4th-generation Surface devices also make use of a SAM EC,
+         due to a difference in the communication interface of the controller,
+         only 5th and later generations are currently supported. Specifically,
+         devices using SAM-over-SSH are supported, whereas devices using
+         SAM-over-HID, which is used on the 4th generation, are currently not
+         supported.

From this help text I didn't get if it will be a module or what if I chose the above?

...

Quoted 1 line

+/* -- Safe counters. -------------------------------------------------------- */

Why can't XArray be used here?

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+

One blank line is enough

...

drivers/platform/surface/aggregator/controller.c line 116

Quoted 3 lines
drivers/platform/surface/aggregator/controller.c new file +3 -0
+static bool ssam_event_matches_notifier(
+               const struct ssam_event_notifier *notif,
+               const struct ssam_event *event)

Perhaps

static bool
ssam_event_matches_notifier(const struct ssam_event_notifier *n,
const struct ssam_event *event)

(or even switch to 100 limit, also note notif ->n — no need to repeat same word)

...

drivers/platform/surface/aggregator/controller.c line 161

Quoted 9 lines
drivers/platform/surface/aggregator/controller.c new file +9 -0
+       nb = rcu_dereference_raw(nh->head);
+       while (nb) {
+               nf = container_of(nb, struct ssam_event_notifier, base);
+               next_nb = rcu_dereference_raw(nb->next);
+
+               if (ssam_event_matches_notifier(nf, event)) {
+                       ret = (ret & SSAM_NOTIF_STATE_MASK) | nb->fn(nf, event);
+                       if (ret & SSAM_NOTIF_STOP)
+                               break;

The returned value is a bitmask?!
What are you returning at the end?

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+               }
+
+               nb = next_nb;
+       }

...

Quoted 11 lines
drivers/platform/surface/aggregator/controller.c new file +11 -0
+static int __ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
+{
+       struct ssam_notifier_block **link = &nh->head;
+
+       while ((*link) != NULL) {
+               if (unlikely((*link) == nb)) {
+                       WARN(1, "double register detected");
+                       return -EINVAL;
+               }
+
+               if (nb->priority > (*link)->priority)

drivers/platform/surface/aggregator/controller.c line 203

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+                       break;
+
+               link = &((*link)->next);
+       }
+
+       nb->next = *link;
+       rcu_assign_pointer(*link, nb);
+
+       return 0;
+}

If you need RCU (which is also the Q per se), why not use RCU list?

...

Quoted 1 line

+ while ((*link) != NULL) {

Redundant parentheses, redundant ' != NULL' part.

Quoted 5 lines
drivers/platform/surface/aggregator/controller.c new file +5 -0
+               if ((*link) == nb)
+                       return link;
+
+               link = &((*link)->next);
+       }

...

Quoted 1 line

+ * struct ssam_nf_refcount_entry - RB-tree entry for referecnce counting event

In above you mistyped 'which' as 'whic' or so, and here reference. Perhaps go thru spell checker?

...

Quoted 1 line

+ * registered, or ``ERR_PTR(-ENOMEM)`` if the entry could not be allocated.

Better to spell out "error pointer" instead of cryptic ERR_PTR().

...

Quoted 2 lines

+ * Executa registered callbacks in order of their priority until either no
+ * callback is left or a callback returned a value with the %SSAM_NOTIF_STOP

returns

Quoted 2 lines

+ * bit set. Note that this bit is set automatically when converting non.zero
+ * error values via ssam_notifier_from_errno() to notifier values.

...

Quoted 1 line

+ for (i = i - 1; i >= 0; i--)

while (i--)

Quoted 1 line

+ ssam_nf_head_destroy(&nf->head[i]);

...

drivers/platform/surface/aggregator/controller.c line 711

Quoted 2 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+       // limit number of processed events to avoid livelocking
+       for (i = 0; i < 10; i++) {

Magic number! Also, this will be better to read in a form of

unsigned int iterations = 10;

do {
...
} while (--iterations);

Quoted 7 lines
drivers/platform/surface/aggregator/controller.c new file +7 -0
+               item = ssam_event_queue_pop(queue);
+               if (item == NULL)
+                       return;
+
+               ssam_nf_call(nf, dev, item->rqid, &item->event);
+               kfree(item);
+       }

...

drivers/platform/surface/aggregator/controller.c line 932

Quoted 2 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+static const guid_t SSAM_SSH_DSM_GUID = GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
+               0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);

Can you use usual pattern for these UIDs, like
static const guid_t SSAM_SSH_DSM_GUID =

        GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
                 0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);

?

Also put a comment how this UID will look like in a string representation.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (!acpi_has_method(handle, "_DSM"))
+               return 0;

Hmm... What's the expectation?

drivers/platform/surface/aggregator/controller.c line 957

Quoted 5 lines
drivers/platform/surface/aggregator/controller.c new file +5 -0
+       obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
+                                     SSAM_SSH_DSM_REVISION, 0, NULL,
+                                     ACPI_TYPE_BUFFER);
+       if (!obj)
+               return -EFAULT;

EFAULT?! Perhaps you can simply return 0 here, no?

drivers/platform/surface/aggregator/controller.c line 960

Quoted 2 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+       for (i = 0; i < obj->buffer.length && i < 8; i++)
+               mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));

Don't we have some helpers for this? At least I remember similar code went to one of PDx86 drivers like intel-vbtn or so.

Quoted 1 line

+ if (mask & 0x01)

BIT(0) ?

Quoted 1 line

+ *funcs = mask;

...

drivers/platform/surface/aggregator/controller.c line 1017

Quoted 5 lines
drivers/platform/surface/aggregator/controller.c new file +5 -0
+       caps->ssh_power_profile = (u32)-1;
+       caps->screen_on_sleep_idle_timeout = (u32)-1;
+       caps->screen_off_sleep_idle_timeout = (u32)-1;
+       caps->d3_closes_handle = false;
+       caps->ssh_buffer_size = (u32)-1;

Use proper types and their limits (limits.h missed?).

...

Quoted 1 line

+ // initialize request and packet transport layers

Inconsistent style of comments.

...

Quoted 3 lines

+ * In the course of this shutdown procedure, all currently registered
+ * notifiers will be unregistered. It is, however, strongly recommended to not
+ * rely on this behavior, and instead the party registring the notifier should

registering

Quoted 5 lines

+ * unregister it before the controller gets shut down, e.g. via the SSAM bus
+ * which guarantees client devices to be removed before a shutdown.

+ * Note that events may still be pending after this call, but due to the
+ * notifiers being unregistered, the will be dropped when the controller is

the?!

Quoted 1 line

+ * subsequently being destroyed via ssam_controller_destroy().

...

Quoted 4 lines

+ * Ensures that all resources associated with the controller get freed. This
+ * function should only be called after the controller has been stopped via
+ * ssam_controller_shutdown(). In general, this function should not be called
+ * directly. The only valid place to call this function direclty is during

directly

Quoted 3 lines

+ * initialization, before the controller has been fully initialized and passed
+ * to other processes. This function is called automatically when the
+ * reference count of the controller reaches zero.

...

Quoted 2 lines

+ * ssam_request_sync_free() - Free a synchronous request.
+ * @rqst: The request to free.

to be freed?

...

Quoted 3 lines

+ * Allocates a synchronous request struct on the stack, fully initializes it
+ * using the provided buffer as message data buffer, submits it, and then
+ * waits for its completion before returning its staus. The

status

Quoted 2 lines

+ * SSH_COMMAND_MESSAGE_LENGTH() macro can be used to compute the required
+ * message buffer size.

...

Quoted 3 lines

+ * This is a wrapper for the raw SAM request to enable an event, thus it does
+ * not handle referecnce counting for enable/disable of events. If an event
+ * has already been enabled, the EC will ignore this request.

Grammar and English language style somehow feels not okay.

...

Quoted 1 line

+ u8 buf[1] = { 0x00 };

Can't be simply buf ?

...

Quoted 3 lines

+ * This function will only send the display-off notification command if
+ * display noticications are supported by the EC. Currently all known devices
+ * support these notification.

Spell check!

...

Quoted 3 lines

+ * This function will only send the display-on notification command if display
+ * noticications are supported by the EC. Currently all known devices support
+ * these notification.

Ditto.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+               ssam_err(ctrl, "unexpected response from D0-exit notification:"
+                        " 0x%02x\n", response);

Don't split string literals. Had you run a checkpatch? Please do and use strict mode.

...

Overall impression of this submission:
- it's quite huge as for a single patch
- it feels like quite an overengineered solution: READ_ONCE, RCU,
list + spin_lock, RB tree of notifiers, my head!
- where is the architectural document of all these?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#3 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

Quoted 72 lines · Andy Shevchenko
Quoted 37 lines · Maximilian Luz

Add Surface System Aggregator Module core and Surface Serial Hub driver, required for the embedded controller found on Microsoft Surface devices.

The Surface System Aggregator Module (SSAM, SAM or Surface Aggregator) is an embedded controller (EC) found on 4th and later generation Microsoft Surface devices, with the exception of the Surface Go series. This EC provides various functionality, depending on the device in question. This can include battery status and thermal reporting (5th and later generations), but also HID keyboard (6th+) and touchpad input (7th+) on Surface Laptop and Surface Book 3 series devices.

This patch provides the basic necessities for communication with the SAM EC on 5th and later generation devices. On these devices, the EC provides an interface that acts as serial device, called the Surface Serial Hub (SSH). 4th generation devices, on which the EC interface is provided via an HID-over-I2C device, are not supported by this patch.

Specifically, this patch adds a driver for the SSH device (device HID MSHW0084 in ACPI), as well as a controller structure and associated API. This represents the functional core of the Surface Aggregator kernel subsystem, introduced with this patch, and will be expanded upon in subsequent commits.

The SSH driver acts as the main attachment point for this subsystem and sets-up and manages the controller structure. The controller in turn provides a basic communication interface, allowing to send requests from host to EC and receiving the corresponding responses, as well as managing and receiving events, sent from EC to host. It is structured into multiple layers, with the top layer presenting the API used by other kernel drivers and the lower layers modeled after the serial protocol used for communication.

Said other drivers are then responsible for providing the (Surface model specific) functionality accessible through the EC (e.g. battery status reporting, thermal information, ...) via said controller structure and API, and will be added in future commits.

...

Quoted 31 lines
drivers/platform/surface/aggregator/Kconfig new file +31 -0
+menuconfig SURFACE_AGGREGATOR
+       tristate "Microsoft Surface System Aggregator Module Subsystem and Drivers"
+       depends on SERIAL_DEV_BUS
+       depends on ACPI
+       select CRC_CCITT
+       help
+         The Surface System Aggregator Module (Surface SAM or SSAM) is an
+         embedded controller (EC) found on 5th- and later-generation Microsoft
+         Surface devices (i.e. Surface Pro 5, Surface Book 2, Surface Laptop,
+         and newer, with exception of Surface Go series devices).
+
+         Depending on the device in question, this EC provides varying
+         functionality, including:
+         - EC access from ACPI via Surface ACPI Notify (5th- and 6th-generation)
+         - battery status information (all devices)
+         - thermal sensor access (all devices)
+         - performance mode / cooling mode control (all devices)
+         - clipboard detachment system control (Surface Book 2 and 3)
+         - HID / keyboard input (Surface Laptops, Surface Book 3)
+
+         This option controls whether the Surface SAM subsystem core will be
+         built. This includes a driver for the Surface Serial Hub (SSH), which
+         is the device responsible for the communication with the EC, and a
+         basic kernel interface exposing the EC functionality to other client
+         drivers, i.e. allowing them to make requests to the EC and receive
+         events from it. Selecting this option alone will not provide any
+         client drivers and therefore no functionality beyond the in-kernel
+         interface. Said functionality is the responsibility of the respective
+         client drivers.
+
+         Note: While 4th-generation Surface devices also make use of a SAM EC,
Quoted 8 lines

drivers/platform/surface/aggregator/Kconfig line 39

Quoted 5 lines
drivers/platform/surface/aggregator/Kconfig new file +5 -0
+         due to a difference in the communication interface of the controller,
+         only 5th and later generations are currently supported. Specifically,
+         devices using SAM-over-SSH are supported, whereas devices using
+         SAM-over-HID, which is used on the 4th generation, are currently not
+         supported.

From this help text I didn't get if it will be a module or what if I
chose the above?

I thought this would be implied by the tristate options. So should I simply add something like

Choose m if you want to build the SAM subsystem core and SSH driver as module, y if you want to build it into the kernel and n if you don't want it at all.

Quoted 5 lines

...

Quoted 1 line

+/* -- Safe counters. -------------------------------------------------------- */

Why can't XArray be used here?

These are basically packet and request IDs (that are sent to the EC and received from it). XArray is a type of collection. I'm not sure how this would work.

What happens is: For each new packet/request we get a new ID by incrementing the counter (with wrap-around). This ID is used, for example, to match ACK-packets to the corresponding data packet. The EC will also use this to detect re-transmission, e.g. if we send two packets with the same ID in sequence, the EC will detect the second packet as re-transmission of the first one (which is the reason why simple IDA won't work).

Furthermore, both the Windows driver and the EC itself (for packets sent by it) use wrapping counters as well, so it seems to be the safest way to do this without possibly triggering re-transmission detection by accident.

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+

One blank line is enough

Can I keep two blank lines for the section separator comments?

Quoted 6 lines

...

drivers/platform/surface/aggregator/controller.c line 116

Quoted 3 lines
drivers/platform/surface/aggregator/controller.c new file +3 -0
+static bool ssam_event_matches_notifier(
+               const struct ssam_event_notifier *notif,
+               const struct ssam_event *event)
Quoted 7 lines

Perhaps

static bool
ssam_event_matches_notifier(const struct ssam_event_notifier *n,
const struct ssam_event *event)

(or even switch to 100 limit, also note notif ->n — no need to repeat same word)

Ack.

Quoted 12 lines

...

drivers/platform/surface/aggregator/controller.c line 161

Quoted 9 lines
drivers/platform/surface/aggregator/controller.c new file +9 -0
+       nb = rcu_dereference_raw(nh->head);
+       while (nb) {
+               nf = container_of(nb, struct ssam_event_notifier, base);
+               next_nb = rcu_dereference_raw(nb->next);
+
+               if (ssam_event_matches_notifier(nf, event)) {
+                       ret = (ret & SSAM_NOTIF_STATE_MASK) | nb->fn(nf, event);
+                       if (ret & SSAM_NOTIF_STOP)
+                               break;
Quoted 1 line

The returned value is a bitmask?!

In part, yes. I've referred to this as the "notifier status value" in the (function) documentation. This is basically

    | bit 0       | bit 1    | bit 2 to 31         |
    +-------------+----------+---------------------+
    | handled-bit | stop-bit | error-value or zero |

The ssam_notifier_to_errno() function can be used to convert an error value to this notifier status value. This is the same as the return value of the notifier function and is described a bit better in the documentation of struct ssam_notifier_block.

Quoted 1 line

What are you returning at the end?

The notifier status value as described above. This can be tested for the SSAM_NOTIF_HANDLED and SSAM_NOTIF_STOP bits. The error value can, as mentioned in the documentation, be extracted via ssam_notifier_to_errno().

All in all, this is based on notifier_call_chain() and SRCU notifiers (linux/notifier.h, kernel/notifier.c) in general, with the key difference that it includes a bit to indicate if the notification has been handled (or detect if it goes unhandled).

Quoted 23 lines
Quoted 5 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+               }
+
+               nb = next_nb;
+       }

...

Quoted 15 lines
drivers/platform/surface/aggregator/controller.c new file +15 -0
+static int __ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
+{
+       struct ssam_notifier_block **link = &nh->head;
+
+       while ((*link) != NULL) {
+               if (unlikely((*link) == nb)) {
+                       WARN(1, "double register detected");
+                       return -EINVAL;
+               }
+
+               if (nb->priority > (*link)->priority)
+                       break;
+
+               link = &((*link)->next);
+       }
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 203

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+
+       nb->next = *link;
+       rcu_assign_pointer(*link, nb);
+
+       return 0;
+}

If you need RCU (which is also the Q per se), why not use RCU list?

This part has been mostly copied and adapted from notifier_chain_register(), which uses the exact same mechanism. I can change it to use RCU list primitives if that's preferred.

Quoted 5 lines

...

Quoted 1 line

+ while ((*link) != NULL) {

Redundant parentheses, redundant ' != NULL' part.

Ack.

Quoted 7 lines

drivers/platform/surface/aggregator/controller.c line 227

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +5 -0
+               if ((*link) == nb)
+                       return link;
+
+               link = &((*link)->next);
+       }
Quoted 6 lines

...

Quoted 1 line

+ * struct ssam_nf_refcount_entry - RB-tree entry for referecnce counting event

In above you mistyped 'which' as 'whic' or so, and here reference. Perhaps go thru spell checker?

Ack. I'll do that.

Quoted 6 lines

...

Quoted 1 line

+ * registered, or ``ERR_PTR(-ENOMEM)`` if the entry could not be allocated.

Better to spell out "error pointer" instead of cryptic ERR_PTR().

Ack.

Quoted 7 lines

...

Quoted 2 lines

+ * Executa registered callbacks in order of their priority until either no
+ * callback is left or a callback returned a value with the %SSAM_NOTIF_STOP

returns

Ack.

Quoted 9 lines
Quoted 3 lines

+ * bit set. Note that this bit is set automatically when converting non.zero
+ * error values via ssam_notifier_from_errno() to notifier values.

...

Quoted 1 line

+ for (i = i - 1; i >= 0; i--)

while (i--)

Ack.

Quoted 8 lines
Quoted 2 lines

+ ssam_nf_head_destroy(&nf->head[i]);

...

drivers/platform/surface/aggregator/controller.c line 711

Quoted 2 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+       // limit number of processed events to avoid livelocking
+       for (i = 0; i < 10; i++) {
Quoted 7 lines

Magic number! Also, this will be better to read in a form of

unsigned int iterations = 10;

do {
...
} while (--iterations);

Okay.

Quoted 14 lines
Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +7 -0
+               item = ssam_event_queue_pop(queue);
+               if (item == NULL)
+                       return;
+
+               ssam_nf_call(nf, dev, item->rqid, &item->event);
+               kfree(item);
+       }

...

drivers/platform/surface/aggregator/controller.c line 932

Quoted 2 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+static const guid_t SSAM_SSH_DSM_GUID = GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
+               0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
Quoted 7 lines

Can you use usual pattern for these UIDs, like
static const guid_t SSAM_SSH_DSM_GUID =

         GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
                  0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);

?

Also put a comment how this UID will look like in a string representation.

Will change that.

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (!acpi_has_method(handle, "_DSM"))
+               return 0;

Hmm... What's the expectation?

Depends on the device. Old devices don't have that _DSM (thus fallback to defaults), newer devices do have it.

Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 957

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +5 -0
+       obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
+                                     SSAM_SSH_DSM_REVISION, 0, NULL,
+                                     ACPI_TYPE_BUFFER);
+       if (!obj)
+               return -EFAULT;

EFAULT?! Perhaps you can simply return 0 here, no?

I'd prefer returning an error (_DSM index 0 should always be present and return a buffer), but I agree that EFAULT is probably the wrong one.

Quoted 6 lines

drivers/platform/surface/aggregator/controller.c line 960

Quoted 3 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+       for (i = 0; i < obj->buffer.length && i < 8; i++)
+               mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));

Don't we have some helpers for this? At least I remember similar code went to one of PDx86 drivers like intel-vbtn or so.

There is acpi_check_dsm(), which checks if a single function is supported, but as far as I can tell no helper that returns a bitmask of all supported functions. I can change it to use acpi_check_dsm() in ssam_dsm_load_u32() though.

Quoted 4 lines
Quoted 2 lines

+ if (mask & 0x01)

BIT(0) ?

BIT(0) looks better, ack.

Quoted 5 lines
Quoted 2 lines

+ *funcs = mask;

...

Quoted 7 lines

drivers/platform/surface/aggregator/controller.c line 1017

Quoted 5 lines
drivers/platform/surface/aggregator/controller.c new file +5 -0
+       caps->ssh_power_profile = (u32)-1;
+       caps->screen_on_sleep_idle_timeout = (u32)-1;
+       caps->screen_off_sleep_idle_timeout = (u32)-1;
+       caps->d3_closes_handle = false;
+       caps->ssh_buffer_size = (u32)-1;

Use proper types and their limits (limits.h missed?).

Okay.

Quoted 6 lines

...

Quoted 1 line

+ // initialize request and packet transport layers

Inconsistent style of comments.

Will change that.

Quoted 8 lines

...

Quoted 3 lines

+ * In the course of this shutdown procedure, all currently registered
+ * notifiers will be unregistered. It is, however, strongly recommended to not
+ * rely on this behavior, and instead the party registring the notifier should

registering

Ack.

Quoted 8 lines
Quoted 6 lines

+ * unregister it before the controller gets shut down, e.g. via the SSAM bus
+ * which guarantees client devices to be removed before a shutdown.

+ * Note that events may still be pending after this call, but due to the
+ * notifiers being unregistered, the will be dropped when the controller is

the?!

Should be "the events".

Quoted 11 lines
Quoted 2 lines

+ * subsequently being destroyed via ssam_controller_destroy().

...

Quoted 4 lines

+ * Ensures that all resources associated with the controller get freed. This
+ * function should only be called after the controller has been stopped via
+ * ssam_controller_shutdown(). In general, this function should not be called
+ * directly. The only valid place to call this function direclty is during

directly

Ack.

Quoted 11 lines
Quoted 4 lines

+ * initialization, before the controller has been fully initialized and passed
+ * to other processes. This function is called automatically when the
+ * reference count of the controller reaches zero.

...

Quoted 2 lines

+ * ssam_request_sync_free() - Free a synchronous request.
+ * @rqst: The request to free.

to be freed?

I think both should be grammatically correct, but I'll change it.

Quoted 8 lines

...

Quoted 3 lines

+ * Allocates a synchronous request struct on the stack, fully initializes it
+ * using the provided buffer as message data buffer, submits it, and then
+ * waits for its completion before returning its staus. The

status

Ack.

Quoted 11 lines
Quoted 3 lines

+ * SSH_COMMAND_MESSAGE_LENGTH() macro can be used to compute the required
+ * message buffer size.

...

Quoted 3 lines

+ * This is a wrapper for the raw SAM request to enable an event, thus it does
+ * not handle referecnce counting for enable/disable of events. If an event
+ * has already been enabled, the EC will ignore this request.

Grammar and English language style somehow feels not okay.

I'll try to reword that. I agree that this feels a bit off.

Quoted 6 lines

...

Quoted 1 line

+ u8 buf[1] = { 0x00 };

Can't be simply buf ?

It can be, I'll change that

Quoted 8 lines

...

Quoted 3 lines

+ * This function will only send the display-off notification command if
+ * display noticications are supported by the EC. Currently all known devices
+ * support these notification.

Spell check!

Will do that.

Quoted 8 lines

...

Quoted 3 lines

+ * This function will only send the display-on notification command if display
+ * noticications are supported by the EC. Currently all known devices support
+ * these notification.

Ditto.

Ack.

Quoted 8 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+               ssam_err(ctrl, "unexpected response from D0-exit notification:"
+                        " 0x%02x\n", response);

Don't split string literals. Had you run a checkpatch? Please do and use strict mode.

I did run checkpatch, however, I decided to split strings as some of them are a bit long. When doing that, I only split at places where I personally wouldn't expect a grep to succeed for, e.g. before or after format specifiers. I though that this is okay as the main reason for "don't split string literals" is searchability.

Quoted 6 lines

...

Overall impression of this submission:
- it's quite huge as for a single patch

I tried to split it into parts, but this is the smallest I've been able to get it while also keeping it a functional unit.

Quoted 2 lines

- it feels like quite an overengineered solution: READ_ONCE, RCU,
list + spin_lock, RB tree of notifiers, my head!

Sorry about any headaches. My main goal was stability and reliability, while keeping things somewhat performant and generic.

The "generic" part mostly because this whole thing is based on reverse-engineering, and I believe that makes it easier to correct for things that we got wrong, e.g. in what we think the protocol looks like.

Packet state is tracked via atomic flags (which is also laid out in a comment), and is the simplest solution I found to track that. I found that a simple enum based state will complicate things as the driver has to support and properly handle re-submission of packets.

READ_ONCE and WRITE_ONCE are used to ensure proper access to state that can be changed outside of the queue/pending locks (or under any one of them). In general, I have tried to document all critical access of such state with an explanation of why it is safe to do so.

RCU is only used for notifier callbacks, which is based on the SRCU notifiers from linux/notifier.h and kernel/notifier.c, but adapted to detect unhandled events.

While it is somewhat interwoven, the (RCU-based) notifier chain stores the notifiers (one chain per target category) and the RB-tree stores the number of times an event has been enabled. Essentially, those should be seen as two separate parts (see e.g. documentation of struct ssam_nf, which brings those two together).

So the RB-tree is not for notifiers, but for tracking which events are enabled. Basically reference-counting for enabled events, as the EC itself only allows turning them on/off. It's necessary to do that as there can be multiple clients listening to the same event. It's also required to do this with some sort of map, as (in context of enabling/disabling) the events are uniquely identified by both their registry and ID. This combination is a bit too complex to do that with a simple array, so a RB-tree looked like the best solution.

Note that we store notifiers per target category only, so for that we can use a simple array.

Quoted 1 line

- where is the architectural document of all these?

What specifically do you want to see documented? I've documented the basic communication and principles in another patch of this series, but kept implementation details somewhat vague as I think those are better covered by struct and function documentation (which won't become outdated as easily as a separate documentation).

Thank you for your comments.

Regards,
Max

author maintainer
#4 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

Quoted 2 lines · Maximilian Luz
Quoted 1 line · Andy Shevchenko

On Sun, Nov 15, 2020 at 9:25 PM Maximilian Luz <[email protected]> wrote:

[...]

Quoted 4 lines

READ_ONCE and WRITE_ONCE are used to ensure proper access to state that can be changed outside of the queue/pending locks (or under any one of them). In general, I have tried to document all critical access of such state with an explanation of why it is safe to do so.

I've looked at this again and noticed that I can guard the packet timestamp by the pending lock and the packet priority by the queue lock (after first submission). This makes reasoning about access to them significantly easier and removes the need for WRITE_ONCE / READ_ONCE.

After that, READ_ONCE is used

- to access the controller state for smoke-testing to (hopefully) detect invalid request function usage (note that all other access to this state happens under the controller state lock)

- for the "safe counters", where access to the shared value is, after initialization, restricted to the increment function

- to update the timeout reaper, where access to the shared value (rtx_timeout.expires) is, after initialization, restricted to its modification function (ssh_ptl_timeout_reaper_mod() / ssh_rtl_timeout_reaper_mod()) and the timer function

- to access the request timestamp, which is, after initialization, only set once in the lifetime of a request (all other access is read-only)

- to access the 'ptl' reference of the packet, which, after initialization, is only set once, either at packet or request submission (all other access is read-only). Note due to this, READ_ONCE is only required for functions that can run concurrently with ssh_ptl_submit() and ssh_rtl_submit(), i.e. ssh_ptl_cancel() and ssh_rtl_cancel().

- to access request state outside of bit-ops when canceling

I'd argue that all of these cases can be checked and verified with a reasonable amount of effort. Cancellation (last two points) is probably the most complex one. Unfortunately, I don't see any way to simplify that part without disallowing cancellation to run concurrently to submission, which is something I'd like to support as this makes implementing asynchronous requests in the future easier.

Regards,
Max

#12 lore raw

Re: [PATCH 4/9] platform/surface: aggregator: Add trace points

In reply to Maximilian Luz
Quoted 14 lines · On Sun, 15 Nov 2020 20:21:38 +0100 Maximilian Luz <[email protected]>…

Add trace points to the Surface Aggregator subsystem core. These trace points can be used to track packets, requests, and allocations. They are further intended for debugging and testing/validation, specifically in combination with the error injection capabilities introduced in the subsequent commit.

---

drivers/platform/surface/aggregator/ssh_packet_layer.c +0 -0
 	/*
 	 * Lock packet and commit with memory barrier. If this packet has
 	 * already been locked, it's going to be removed and completed by

drivers/platform/surface/aggregator/ssh_packet_layer.c static void ssh_ptl_timeout_reap(struct work_struct *work) · line 1170

Quoted 4 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c +1 -0
@@ -1154,6 +1167,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
 	ktime_t next = KTIME_MAX;
 	bool resub = false;
 
+	trace_ssam_ptl_timeout_reap("pending", atomic_read(&ptl->pending.count));

I noticed that the only two places that use timeout_reap, both have "pending" as a string. Is this necessary to save? Would an enum work?

Quoted 227 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c +7 -0
+
 	/*
 	 * Mark reaper as "not pending". This is done before checking any
 	 * packets to avoid lost-update type problems.
@@ -1185,6 +1200,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
 		// check if we still have some tries left
 		try = ssh_packet_priority_get_try(READ_ONCE(p->priority));
 		if (likely(try < SSH_PTL_MAX_PACKET_TRIES)) {
+			trace_ssam_packet_timeout(p);
+
 			/*
 			 * Submission fails if the packet has been locked, is
 			 * already queued, or the layer is being shut down.
@@ -1202,6 +1219,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
 		if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
 			continue;
 
+		trace_ssam_packet_timeout(p);
+
 		/*
 		 * We have now marked the packet as locked. Thus it cannot be
 		 * added to the pending list again after we've removed it here.
@@ -1366,6 +1385,8 @@ static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
 	if (!frame)	// not enough data
 		return aligned.ptr - source->ptr;
 
+	trace_ssam_rx_frame_received(frame);
+
 	switch (frame->type) {
 	case SSH_FRAME_TYPE_ACK:
 		ssh_ptl_acknowledge(ptl, frame->seq);
drivers/platform/surface/aggregator/ssh_request_layer.c +18 -0
index 2b0368bb2d79..0d3384fd2d4c 100644
@@ -21,6 +21,8 @@
 #include "ssh_packet_layer.h"
 #include "ssh_request_layer.h"
 
+#include "trace.h"
+
 
 /*
  * SSH_RTL_REQUEST_TIMEOUT - Request timeout.
@@ -141,6 +143,8 @@ static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
 {
 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
 
+	trace_ssam_request_complete(rqst, status);
+
 	// rtl/ptl may not be set if we're cancelling before submitting
 	rtl_dbg_cond(rtl, "rtl: completing request (rqid: 0x%04x, status: %d)\n",
 		     ssh_request_get_rqid_safe(rqst), status);
@@ -154,6 +158,8 @@ static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
 {
 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
 
+	trace_ssam_request_complete(rqst, 0);
+
 	rtl_dbg(rtl, "rtl: completing request with response (rqid: 0x%04x)\n",
 		ssh_request_get_rqid(rqst));
 
@@ -326,6 +332,8 @@ static void ssh_rtl_tx_work_fn(struct work_struct *work)
  */
 int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
 {
+	trace_ssam_request_submit(rqst);
+
 	/*
 	 * Ensure that requests expecting a response are sequenced. If this
 	 * invariant ever changes, see the comment in ssh_rtl_complete() on what
@@ -438,6 +446,8 @@ static void ssh_rtl_complete(struct ssh_rtl *rtl,
 	struct ssh_request *p, *n;
 	u16 rqid = get_unaligned_le16(&command->rqid);
 
+	trace_ssam_rx_response_received(command, command_data->len);
+
 	/*
 	 * Get request from pending based on request ID and mark it as response
 	 * received and locked.
@@ -688,6 +698,8 @@ bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
 	if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
 		return true;
 
+	trace_ssam_request_cancel(rqst);
+
 	if (pending)
 		canceled = ssh_rtl_cancel_pending(rqst);
 	else
@@ -777,6 +789,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
 	ktime_t timeout = rtl->rtx_timeout.timeout;
 	ktime_t next = KTIME_MAX;
 
+	trace_ssam_rtl_timeout_reap("pending", atomic_read(&rtl->pending.count));
+
 	/*
 	 * Mark reaper as "not pending". This is done before checking any
 	 * requests to avoid lost-update type problems.
@@ -825,6 +839,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
 
 	// cancel and complete the request
 	list_for_each_entry_safe(r, n, &claimed, node) {
+		trace_ssam_request_timeout(r);
+
 		/*
 		 * At this point we've removed the packet from pending. This
 		 * means that we've obtained the last (only) reference of the
@@ -850,6 +866,8 @@ static void ssh_rtl_timeout_reap(struct work_struct *work)
 static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
 			     const struct ssam_span *data)
 {
+	trace_ssam_rx_event_received(cmd, data->len);
+
 	rtl_dbg(rtl, "rtl: handling event (rqid: 0x%04x)\n",
 		get_unaligned_le16(&cmd->rqid));
 
drivers/platform/surface/aggregator/trace.h new file +105 -0
new file mode 100644
index 000000000000..ead052c1ffab
@@ -0,0 +1,616 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Trace points for SSAM/SSH.
+ *
+ * Copyright (C) 2020 Maximilian Luz <[email protected]>
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM surface_aggregator
+
+#if !defined(_SURFACE_AGGREGATOR_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _SURFACE_AGGREGATOR_TRACE_H
+
+#include <linux/surface_aggregator/serial_hub.h>
+
+#include <asm/unaligned.h>
+#include <linux/tracepoint.h>
+
+
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_SEQ);
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_NSQ);
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_ACK);
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_NAK);
+
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_LOCKED_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_QUEUED_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_PENDING_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTING_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTED_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_ACKED_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_CANCELED_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_COMPLETED_BIT);
+
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_FLUSH_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_SEQUENCED_BIT);
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_BLOCKING_BIT);
+
+TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_SF_MASK);
+TRACE_DEFINE_ENUM(SSH_PACKET_FLAGS_TY_MASK);
+
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_LOCKED_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_QUEUED_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_PENDING_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTING_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTED_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_RSPRCVD_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_CANCELED_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_COMPLETED_BIT);
+
+TRACE_DEFINE_ENUM(SSH_REQUEST_TY_FLUSH_BIT);
+TRACE_DEFINE_ENUM(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
+
+TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_SF_MASK);
+TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_TY_MASK);
+
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SAM);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAT);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TMP);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_PMC);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_FAN);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_PoM);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_DBG);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_KBD);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_FWU);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_UNI);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_LPC);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCL);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SFL);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_KIP);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_EXT);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BLD);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAS);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SEN);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SRQ);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_MCU);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_HID);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCH);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BKL);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TAM);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_ACC);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_UFI);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_USC);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_PEN);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_VID);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_AUD);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SMC);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_KPD);
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_REG);
+
+
+#define SSAM_PTR_UID_LEN		9
+#define SSAM_U8_FIELD_NOT_APPLICABLE	((u16)-1)
+#define SSAM_SEQ_NOT_APPLICABLE		((u16)-1)
+#define SSAM_RQID_NOT_APPLICABLE	((u32)-1)
+#define SSAM_SSH_TC_NOT_APPLICABLE	0
+
+
+#ifndef _SURFACE_AGGREGATOR_TRACE_HELPERS
+#define _SURFACE_AGGREGATOR_TRACE_HELPERS
+
+/**
+ * ssam_trace_ptr_uid() - Convert the pointer to a non-pointer UID string.
+ * @ptr: The pointer to convert.
+ * @uid_str: A buffer of length SSAM_PTR_UID_LEN where the UID will be stored.
+ *

drivers/platform/surface/aggregator/trace.h line 115

Quoted 10 lines
drivers/platform/surface/aggregator/trace.h new file +10 -0
+ * Converts the given pointer into a UID string that is safe to be shared
+ * with userspace and logs, i.e. doesn't give away the real memory location.
+ */
+static inline void ssam_trace_ptr_uid(const void *ptr, char *uid_str)
+{
+	char buf[2 * sizeof(void *) + 1];
+
+	snprintf(buf, ARRAY_SIZE(buf), "%p", ptr);
+	memcpy(uid_str, &buf[ARRAY_SIZE(buf) - SSAM_PTR_UID_LEN],
+	       SSAM_PTR_UID_LEN);

Is the above snprintf() to have the ptr turn into a hash?

Otherwise, couldn't you just truncate the value of ptr?

In any case, you want to make sure that ARRAY_SIZE(buf) is always bigger than, or equal to, SSAM_PTR_UID_LEN, and you should probably stick a:

	BUILD_BUG_ON(ARRAY_SIZE(buf) < SSAM_PTR_UID_LEN);

in there, which would cause the build to fail if that was ever the case.

Quoted 44 lines
drivers/platform/surface/aggregator/trace.h new file +44 -0
+}
+
+/**
+ * ssam_trace_get_packet_seq() - Read the packet's sequence ID.
+ * @p: The packet.
+ *
+ * Return: Returns the packet's sequence ID (SEQ) field if present, or
+ * %SSAM_SEQ_NOT_APPLICABLE if not (e.g. flush packet).
+ */
+static inline u16 ssam_trace_get_packet_seq(const struct ssh_packet *p)
+{
+	if (!p->data.ptr || p->data.len < SSH_MESSAGE_LENGTH(0))
+		return SSAM_SEQ_NOT_APPLICABLE;
+
+	return p->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
+}
+
+/**
+ * ssam_trace_get_request_id() - Read the packet's request ID.
+ * @p: The packet.
+ *
+ * Return: Returns the packet's request ID (RQID) field if the packet
+ * represents a request with command data, or %SSAM_RQID_NOT_APPLICABLE if not
+ * (e.g. flush request, control packet).
+ */
+static inline u32 ssam_trace_get_request_id(const struct ssh_packet *p)
+{
+	if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))
+		return SSAM_RQID_NOT_APPLICABLE;
+
+	return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(rqid)]);
+}
+
+/**
+ * ssam_trace_get_request_tc() - Read the packet's request target category.
+ * @p: The packet.
+ *
+ * Return: Returns the packet's request target category (TC) field if the
+ * packet represents a request with command data, or %SSAM_TC_NOT_APPLICABLE
+ * if not (e.g. flush request, control packet).
+ */
+static inline u32 ssam_trace_get_request_tc(const struct ssh_packet *p)
+{
+	if (!p->data.ptr || p->data.len < SSH_COMMAND_MESSAGE_LENGTH(0))

drivers/platform/surface/aggregator/trace.h line 169

Quoted 10 lines
drivers/platform/surface/aggregator/trace.h new file +10 -0
+		return SSAM_SSH_TC_NOT_APPLICABLE;
+
+	return get_unaligned_le16(&p->data.ptr[SSH_MSGOFFSET_COMMAND(tc)]);
+}
+
+#endif /* _SURFACE_AGGREGATOR_TRACE_HELPERS */
+
+#define ssam_trace_get_command_field_u8(packet, field) \
+	((!packet || packet->data.len < SSH_COMMAND_MESSAGE_LENGTH(0)) \
+	 ? 0 : p->data.ptr[SSH_MSGOFFSET_COMMAND(field)])

I think you want the above to be:

	? 0 : packet->data.ptr[SSH_MSGOFFSET_COMMAND(field)])

The only reason it worked, is because the caller passed in "p".

Quoted 177 lines
drivers/platform/surface/aggregator/trace.h new file +177 -0
+
+#define ssam_show_generic_u8_field(value)				\
+	__print_symbolic(value,						\
+		{ SSAM_U8_FIELD_NOT_APPLICABLE,		"N/A" }		\
+	)
+
+
+#define ssam_show_frame_type(ty)					\
+	__print_symbolic(ty,						\
+		{ SSH_FRAME_TYPE_DATA_SEQ,		"DSEQ" },	\
+		{ SSH_FRAME_TYPE_DATA_NSQ,		"DNSQ" },	\
+		{ SSH_FRAME_TYPE_ACK,			"ACK"  },	\
+		{ SSH_FRAME_TYPE_NAK,			"NAK"  }	\
+	)
+
+#define ssam_show_packet_type(type)					\
+	__print_flags(flags & SSH_PACKET_FLAGS_TY_MASK, "",		\
+		{ BIT(SSH_PACKET_TY_FLUSH_BIT),		"F" },		\
+		{ BIT(SSH_PACKET_TY_SEQUENCED_BIT),	"S" },		\
+		{ BIT(SSH_PACKET_TY_BLOCKING_BIT),	"B" }		\
+	)
+
+#define ssam_show_packet_state(state)					\
+	__print_flags(flags & SSH_PACKET_FLAGS_SF_MASK, "",		\
+		{ BIT(SSH_PACKET_SF_LOCKED_BIT),	"L" },		\
+		{ BIT(SSH_PACKET_SF_QUEUED_BIT),	"Q" },		\
+		{ BIT(SSH_PACKET_SF_PENDING_BIT),	"P" },		\
+		{ BIT(SSH_PACKET_SF_TRANSMITTING_BIT),	"S" },		\
+		{ BIT(SSH_PACKET_SF_TRANSMITTED_BIT),	"T" },		\
+		{ BIT(SSH_PACKET_SF_ACKED_BIT),		"A" },		\
+		{ BIT(SSH_PACKET_SF_CANCELED_BIT),	"C" },		\
+		{ BIT(SSH_PACKET_SF_COMPLETED_BIT),	"F" }		\
+	)
+
+#define ssam_show_packet_seq(seq)					\
+	__print_symbolic(seq,						\
+		{ SSAM_SEQ_NOT_APPLICABLE,		"N/A" }		\
+	)
+
+
+#define ssam_show_request_type(flags)					\
+	__print_flags(flags & SSH_REQUEST_FLAGS_TY_MASK, "",		\
+		{ BIT(SSH_REQUEST_TY_FLUSH_BIT),	"F" },		\
+		{ BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT),	"R" }		\
+	)
+
+#define ssam_show_request_state(flags)					\
+	__print_flags(flags & SSH_REQUEST_FLAGS_SF_MASK, "",		\
+		{ BIT(SSH_REQUEST_SF_LOCKED_BIT),	"L" },		\
+		{ BIT(SSH_REQUEST_SF_QUEUED_BIT),	"Q" },		\
+		{ BIT(SSH_REQUEST_SF_PENDING_BIT),	"P" },		\
+		{ BIT(SSH_REQUEST_SF_TRANSMITTING_BIT),	"S" },		\
+		{ BIT(SSH_REQUEST_SF_TRANSMITTED_BIT),	"T" },		\
+		{ BIT(SSH_REQUEST_SF_RSPRCVD_BIT),	"A" },		\
+		{ BIT(SSH_REQUEST_SF_CANCELED_BIT),	"C" },		\
+		{ BIT(SSH_REQUEST_SF_COMPLETED_BIT),	"F" }		\
+	)
+
+#define ssam_show_request_id(rqid)					\
+	__print_symbolic(rqid,						\
+		{ SSAM_RQID_NOT_APPLICABLE,		"N/A" }		\
+	)
+
+#define ssam_show_ssh_tc(rqid)						\
+	__print_symbolic(rqid,						\
+		{ SSAM_SSH_TC_NOT_APPLICABLE,		"N/A" },	\
+		{ SSAM_SSH_TC_SAM,			"SAM" },	\
+		{ SSAM_SSH_TC_BAT,			"BAT" },	\
+		{ SSAM_SSH_TC_TMP,			"TMP" },	\
+		{ SSAM_SSH_TC_PMC,			"PMC" },	\
+		{ SSAM_SSH_TC_FAN,			"FAN" },	\
+		{ SSAM_SSH_TC_PoM,			"PoM" },	\
+		{ SSAM_SSH_TC_DBG,			"DBG" },	\
+		{ SSAM_SSH_TC_KBD,			"KBD" },	\
+		{ SSAM_SSH_TC_FWU,			"FWU" },	\
+		{ SSAM_SSH_TC_UNI,			"UNI" },	\
+		{ SSAM_SSH_TC_LPC,			"LPC" },	\
+		{ SSAM_SSH_TC_TCL,			"TCL" },	\
+		{ SSAM_SSH_TC_SFL,			"SFL" },	\
+		{ SSAM_SSH_TC_KIP,			"KIP" },	\
+		{ SSAM_SSH_TC_EXT,			"EXT" },	\
+		{ SSAM_SSH_TC_BLD,			"BLD" },	\
+		{ SSAM_SSH_TC_BAS,			"BAS" },	\
+		{ SSAM_SSH_TC_SEN,			"SEN" },	\
+		{ SSAM_SSH_TC_SRQ,			"SRQ" },	\
+		{ SSAM_SSH_TC_MCU,			"MCU" },	\
+		{ SSAM_SSH_TC_HID,			"HID" },	\
+		{ SSAM_SSH_TC_TCH,			"TCH" },	\
+		{ SSAM_SSH_TC_BKL,			"BKL" },	\
+		{ SSAM_SSH_TC_TAM,			"TAM" },	\
+		{ SSAM_SSH_TC_ACC,			"ACC" },	\
+		{ SSAM_SSH_TC_UFI,			"UFI" },	\
+		{ SSAM_SSH_TC_USC,			"USC" },	\
+		{ SSAM_SSH_TC_PEN,			"PEN" },	\
+		{ SSAM_SSH_TC_VID,			"VID" },	\
+		{ SSAM_SSH_TC_AUD,			"AUD" },	\
+		{ SSAM_SSH_TC_SMC,			"SMC" },	\
+		{ SSAM_SSH_TC_KPD,			"KPD" },	\
+		{ SSAM_SSH_TC_REG,			"REG" }		\
+	)
+
+
+DECLARE_EVENT_CLASS(ssam_frame_class,
+	TP_PROTO(const struct ssh_frame *frame),
+
+	TP_ARGS(frame),
+
+	TP_STRUCT__entry(
+		__field(u8, type)
+		__field(u8, seq)
+		__field(u16, len)
+	),
+
+	TP_fast_assign(
+		__entry->type = frame->type;
+		__entry->seq = frame->seq;
+		__entry->len = get_unaligned_le16(&frame->len);
+	),
+
+	TP_printk("ty=%s, seq=0x%02x, len=%u",
+		ssam_show_frame_type(__entry->type),
+		__entry->seq,
+		__entry->len
+	)
+);
+
+#define DEFINE_SSAM_FRAME_EVENT(name)				\
+	DEFINE_EVENT(ssam_frame_class, ssam_##name,		\
+		TP_PROTO(const struct ssh_frame *frame),	\
+		TP_ARGS(frame)					\
+	)
+
+
+DECLARE_EVENT_CLASS(ssam_command_class,
+	TP_PROTO(const struct ssh_command *cmd, u16 len),
+
+	TP_ARGS(cmd, len),
+
+	TP_STRUCT__entry(
+		__field(u16, rqid)
+		__field(u16, len)
+		__field(u8, tc)
+		__field(u8, cid)
+		__field(u8, iid)
+	),
+
+	TP_fast_assign(
+		__entry->rqid = get_unaligned_le16(&cmd->rqid);
+		__entry->tc = cmd->tc;
+		__entry->cid = cmd->cid;
+		__entry->iid = cmd->iid;
+		__entry->len = len;
+	),
+
+	TP_printk("rqid=0x%04x, tc=%s, cid=0x%02x, iid=0x%02x, len=%u",
+		__entry->rqid,
+		ssam_show_ssh_tc(__entry->tc),
+		__entry->cid,
+		__entry->iid,
+		__entry->len
+	)
+);
+
+#define DEFINE_SSAM_COMMAND_EVENT(name)					\
+	DEFINE_EVENT(ssam_command_class, ssam_##name,			\
+		TP_PROTO(const struct ssh_command *cmd, u16 len),	\
+		TP_ARGS(cmd, len)					\
+	)
+
+
+DECLARE_EVENT_CLASS(ssam_packet_class,
+	TP_PROTO(const struct ssh_packet *packet),
+
+	TP_ARGS(packet),
+
+	TP_STRUCT__entry(
+		__field(unsigned long, state)

drivers/platform/surface/aggregator/trace.h line 356

Quoted 10 lines
drivers/platform/surface/aggregator/trace.h new file +10 -0
+		__array(char, uid, SSAM_PTR_UID_LEN)
+		__field(u8, priority)
+		__field(u16, length)
+		__field(u16, seq)
+	),
+
+	TP_fast_assign(
+		__entry->state = READ_ONCE(packet->state);
+		ssam_trace_ptr_uid(packet, __entry->uid);
+		__entry->priority = READ_ONCE(packet->priority);

I'm curious about the need for the read once here.

The rest seems fine.

-- Steve

Quoted 62 lines
drivers/platform/surface/aggregator/trace.h new file +62 -0
+		__entry->length = packet->data.len;
+		__entry->seq = ssam_trace_get_packet_seq(packet);
+	),
+
+	TP_printk("uid=%s, seq=%s, ty=%s, pri=0x%02x, len=%u, sta=%s",
+		__entry->uid,
+		ssam_show_packet_seq(__entry->seq),
+		ssam_show_packet_type(__entry->state),
+		__entry->priority,
+		__entry->length,
+		ssam_show_packet_state(__entry->state)
+	)
+);
+
+#define DEFINE_SSAM_PACKET_EVENT(name)				\
+	DEFINE_EVENT(ssam_packet_class, ssam_##name,		\
+		TP_PROTO(const struct ssh_packet *packet),	\
+		TP_ARGS(packet)					\
+	)
+
+
+DECLARE_EVENT_CLASS(ssam_packet_status_class,
+	TP_PROTO(const struct ssh_packet *packet, int status),
+
+	TP_ARGS(packet, status),
+
+	TP_STRUCT__entry(
+		__field(unsigned long, state)
+		__field(int, status)
+		__array(char, uid, SSAM_PTR_UID_LEN)
+		__field(u8, priority)
+		__field(u16, length)
+		__field(u16, seq)
+	),
+
+	TP_fast_assign(
+		__entry->state = READ_ONCE(packet->state);
+		__entry->status = status;
+		ssam_trace_ptr_uid(packet, __entry->uid);
+		__entry->priority = READ_ONCE(packet->priority);
+		__entry->length = packet->data.len;
+		__entry->seq = ssam_trace_get_packet_seq(packet);
+	),
+
+	TP_printk("uid=%s, seq=%s, ty=%s, pri=0x%02x, len=%u, sta=%s, status=%d",
+		__entry->uid,
+		ssam_show_packet_seq(__entry->seq),
+		ssam_show_packet_type(__entry->state),
+		__entry->priority,
+		__entry->length,
+		ssam_show_packet_state(__entry->state),
+		__entry->status
+	)
+);
+
+#define DEFINE_SSAM_PACKET_STATUS_EVENT(name)				\
+	DEFINE_EVENT(ssam_packet_status_class, ssam_##name,		\
+		TP_PROTO(const struct ssh_packet *packet, int status),	\
+		TP_ARGS(packet, status)					\
+	)
+
+
author maintainer
#13 lore raw

Re: [PATCH 4/9] platform/surface: aggregator: Add trace points

Quoted 2 lines · Steven Rostedt

On Sun, 15 Nov 2020 20:21:38 +0100
Maximilian Luz <[email protected]> wrote:

[...]

Quoted 11 lines

drivers/platform/surface/aggregator/ssh_packet_layer.c static void ssh_ptl_timeout_reap(struct work_struct *work) · line 1170

Quoted 8 lines

/*
* Lock packet and commit with memory barrier. If this packet has
* already been locked, it's going to be removed and completed by

drivers/platform/surface/aggregator/ssh_packet_layer.c +1 -0
@@ -1154,6 +1167,8 @@ static void ssh_ptl_timeout_reap(struct work_struct *work)
  	ktime_t next = KTIME_MAX;
  	bool resub = false;
 
+	trace_ssam_ptl_timeout_reap("pending", atomic_read(&ptl->pending.count));

I noticed that the only two places that use timeout_reap, both have "pending" as a string. Is this necessary to save? Would an enum work?

I think its probably cleaner to declare an event class for those. Currently they are using the GENERIC_UINT_EVENT class, which I intended as mapping with string to uint, but I'm only using that in three places, two of which are for the timeout. So dropping the GENERIC_UINT_EVENT class is probably the best solution(?).

[...]

Quoted 18 lines
Quoted 5 lines
drivers/platform/surface/aggregator/trace.h new file +5 -0
+/**
+ * ssam_trace_ptr_uid() - Convert the pointer to a non-pointer UID string.
+ * @ptr: The pointer to convert.
+ * @uid_str: A buffer of length SSAM_PTR_UID_LEN where the UID will be stored.
+ *

drivers/platform/surface/aggregator/trace.h line 115

Quoted 10 lines
drivers/platform/surface/aggregator/trace.h new file +10 -0
+ * Converts the given pointer into a UID string that is safe to be shared
+ * with userspace and logs, i.e. doesn't give away the real memory location.
+ */
+static inline void ssam_trace_ptr_uid(const void *ptr, char *uid_str)
+{
+	char buf[2 * sizeof(void *) + 1];
+
+	snprintf(buf, ARRAY_SIZE(buf), "%p", ptr);
+	memcpy(uid_str, &buf[ARRAY_SIZE(buf) - SSAM_PTR_UID_LEN],
+	       SSAM_PTR_UID_LEN);

Is the above snprintf() to have the ptr turn into a hash?

Quoted 1 line

Otherwise, couldn't you just truncate the value of ptr?

Yes, the idea is to generate the same output for a pointer as a normal printk message would generate, i.e. generate a (truncated) hash of the pointer. While the trace points should be enough on their own, I prefer to have the output lining up with the kernel log, so that we can match packets across both if ever necessary.

Quoted 6 lines

In any case, you want to make sure that ARRAY_SIZE(buf) is always bigger than, or equal to, SSAM_PTR_UID_LEN, and you should probably stick a:

	BUILD_BUG_ON(ARRAY_SIZE(buf) < SSAM_PTR_UID_LEN);

in there, which would cause the build to fail if that was ever the case.

Thanks! That is a good idea.

[...]

Quoted 9 lines

drivers/platform/surface/aggregator/trace.h line 169

Quoted 3 lines
drivers/platform/surface/aggregator/trace.h new file +3 -0
+#define ssam_trace_get_command_field_u8(packet, field) \
+	((!packet || packet->data.len < SSH_COMMAND_MESSAGE_LENGTH(0)) \
+	 ? 0 : p->data.ptr[SSH_MSGOFFSET_COMMAND(field)])

I think you want the above to be:

	? 0 : packet->data.ptr[SSH_MSGOFFSET_COMMAND(field)])

The only reason it worked, is because the caller passed in "p".

Oh, right!

Quoted 11 lines
Quoted 11 lines
drivers/platform/surface/aggregator/trace.h new file +11 -0
+DECLARE_EVENT_CLASS(ssam_packet_class,
+	TP_PROTO(const struct ssh_packet *packet),
+
+	TP_ARGS(packet),
+
+	TP_STRUCT__entry(
+		__field(unsigned long, state)
+		__array(char, uid, SSAM_PTR_UID_LEN)
+		__field(u8, priority)
+		__field(u16, length)
+		__field(u16, seq)
Quoted 8 lines

drivers/platform/surface/aggregator/trace.h line 356

Quoted 6 lines
drivers/platform/surface/aggregator/trace.h new file +6 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = READ_ONCE(packet->state);
+		ssam_trace_ptr_uid(packet, __entry->uid);
+		__entry->priority = READ_ONCE(packet->priority);

I'm curious about the need for the read once here.

I generally prefer to be explicit when accessing values that can be changed concurrently by other parties. In the very least, this should guarantee that the value will be read as a whole in one instruction and the compiler does not do anything unexpected (like reading it in multiple instructions, which could lead to invalid state or priority values).

Arguably, the latter will very likely never happen even without the READ_ONCE, but I prefer to be on the safe side.

Quoted 1 line

The rest seems fine.

Thanks,
Max

reviewer
#21 lore raw

Re: [PATCH 9/9] platform/surface: Add Surface ACPI Notify driver

In reply to Maximilian Luz
Quoted 5 lines · Maximilian Luz
drivers/platform/surface/Kconfig +0 -0
index cecad7a0cb7b..3b0da86546fa 100644

drivers/platform/surface/Kconfig config SURFACE_3_POWER_OPREGION · line 48

Quoted 9 lines
drivers/platform/surface/Kconfig +6 -0
@@ -40,6 +40,26 @@ config SURFACE_3_POWER_OPREGION
 	  This driver provides support for ACPI operation
 	  region of the Surface 3 battery platform driver.
 
+config SURFACE_ACPI_NOTIFY
+	tristate "Surface ACPI Notify Driver"
+	depends on ACPI
+	depends on SURFACE_AGGREGATOR
+	help
+	  Surface ACPI Notify (SAN) driver for Microsoft Surface devices.

Drop "depends on ACPI" since SURFACE_AGGREGATOR already depends on ACPI.

Signature · 1 line
~Randy
author maintainer
#22 lore raw

Re: [PATCH 9/9] platform/surface: Add Surface ACPI Notify driver

Quoted 9 lines · Randy Dunlap
Quoted 8 lines · Maximilian Luz
drivers/platform/surface/Kconfig +0 -0
index cecad7a0cb7b..3b0da86546fa 100644
@@ -40,6 +40,26 @@ config SURFACE_3_POWER_OPREGION
  	  This driver provides support for ACPI operation
  	  region of the Surface 3 battery platform driver.
 
Quoted 8 lines

drivers/platform/surface/Kconfig config SURFACE_3_POWER_OPREGION · line 48

Quoted 6 lines
drivers/platform/surface/Kconfig +6 -0
+config SURFACE_ACPI_NOTIFY
+	tristate "Surface ACPI Notify Driver"
+	depends on ACPI
+	depends on SURFACE_AGGREGATOR
+	help
+	  Surface ACPI Notify (SAN) driver for Microsoft Surface devices.

Drop "depends on ACPI" since SURFACE_AGGREGATOR already depends on ACPI.

Okay, will do that.

Thanks,
Max

reviewer
#18 lore raw

Re: [PATCH 8/9] platform/surface: Add Surface Aggregator user-space interface

In reply to Maximilian Luz
Quoted 1 line · Maximilian Luz

+#define SSAM_CDEV_REQUEST _IOWR(0xA5, 1, struct ssam_cdev_request)

All ioctl major numbers (0xA5) should be documented in Documentation/userspace-api/ioctl/ioctl-number.rst

Apologies if I missed it somewhere else.

thanks.

Signature · 1 line
~Randy
author maintainer
#19 lore raw

Re: [PATCH 8/9] platform/surface: Add Surface Aggregator user-space interface

Quoted 7 lines · Randy Dunlap
Quoted 1 line · Maximilian Luz

+#define SSAM_CDEV_REQUEST _IOWR(0xA5, 1, struct ssam_cdev_request)

All ioctl major numbers (0xA5) should be documented in Documentation/userspace-api/ioctl/ioctl-number.rst

Apologies if I missed it somewhere else.

No, I forgot to do that. Will add it for the next revision.

Thanks,
Max

#5 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

In reply to Maximilian Luz

Hi

I have attached some thoughts and comments inline.

2020. november 15., vasárnap 20:21 keltezéssel, Maximilian Luz írta:

drivers/platform/surface/aggregator/controller.c line 101

Quoted 8 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +7 -0
+/* -- Event notifier/callbacks. --------------------------------------------- */
+/*
+ * The notifier system is based on linux/notifier.h, specifically the SRCU
+ * implementation. The difference to that is, that some bits of the notifier
+ * call return value can be tracked across multiple calls. This is done so that
+ * handling of events can be tracked and a warning can be issued in case an
+ * event goes unhandled. The idea of that waring is that it should help discover
                                                ^

"warning"

drivers/platform/surface/aggregator/controller.c line 107

Quoted 6 lines

+ * and identify new/currently unimplemented features.
+ */

drivers/platform/surface/aggregator/controller.c new file +4 -0
+
+
+/**
+ * ssam_event_matches_notifier() - Test if an event matches a notifier;
                                                                         ^

Shouldn't it be a period?

Quoted 9 lines

+ * @notif: The event notifier to test against.
+ * @event: The event to test.
+ *
+ * Return: Returns %true iff the given event matches the given notifier
+ * according to the rules set in the notifier's event mask, %false otherwise.
+ */

[...]

drivers/platform/surface/aggregator/controller.c line 270

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+static int __ssam_nfblk_remove(struct ssam_nf_head *nh,
+			       struct ssam_notifier_block *nb)
+{
+	struct ssam_notifier_block **link;
+
+	link = __ssam_nfblk_find_link(nh, nb);
+	if (!link)
+		return -ENOENT;

I find it odd that here you return ENOENT, but in __ssam_nfblk_insert() EINVAL is returned instead of EEXIST. I believe either both should be EINVAL, or EEXIST+ENOENT.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	__ssam_nfblk_erase(link);

I'm wondering if it's necessary to create a new function which contains just a single line.

drivers/platform/surface/aggregator/controller.c line 299

Quoted 13 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+	return 0;
+}
 
[...]
 
+/**
+ * ssam_nf_head_destroy() - Deinitialize the given notifier head.
+ * @nh: The notifier head to deinitialize.
+ */
+static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
+{
+	cleanup_srcu_struct(&nh->srcu);
+}

I'm also wondering if there's any reason why these static one-liner functions are not explicitly marked inline.

Quoted 4 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +2 -0
+/**
+ * struct ssam_nf_refcount_entry - RB-tree entry for referecnce counting event

drivers/platform/surface/aggregator/controller.c line 326

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+ * activations.
+ * @node:     The node of this entry in the rb-tree.
+ * @key:      The key of the event.
+ * @refcount: The reference-count of the event.
+ * @flags:    The flags used when enabling the event.
+ */
+struct ssam_nf_refcount_entry {
+	struct rb_node node;
+	struct ssam_nf_refcount_key key;
+	int refcount;

Is there any reason why a signed type is used for reference counting?

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+	u8 flags;
+};
+
+

drivers/platform/surface/aggregator/controller.c line 340

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+/**
+ * ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
+ * event.
+ * @nf:  The notifier system reference.
+ * @reg: The registry used to enable/disable the event.
+ * @id:  The event ID.
+ *
+ * Increments the reference-/activation-count associated with the specified
+ * event type/ID, allocating a new entry for this event ID if necessary. A
+ * newly allocated entry will have a refcount of one.

Shouldn't it be noted that nf->lock(?) must(?) be held when calling?

Quoted 10 lines

[...]

+/**
+ * ssam_nf_call() - Call notification callbacks for the provided event.

+ * @nf:    The notifier system
+ * @dev:   The associated device, only used for logging.
+ * @rqid:  The request ID of the event.

+ * @event: The event provided to the callbacks.
+ *
+ * Executa registered callbacks in order of their priority until either no

            ^

"execute"

Quoted 2 lines

+ * callback is left or a callback returned a value with the %SSAM_NOTIF_STOP
+ * bit set. Note that this bit is set automatically when converting non.zero

                                                                          ^

maybe "non-zero"?

Quoted 9 lines

+ * error values via ssam_notifier_from_errno() to notifier values.
+ *
+ * Also note that any callback that could handle an event should return a value
+ * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
+ * unhandled/ignored. In case no registered callback could handle an event,
+ * this function will emit a warning.
+ *
+ * In case a callback failed, this function will emit an error message.
+ */

drivers/platform/surface/aggregator/controller.c line 471

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
+			 struct ssam_event *event)
+{
+	struct ssam_nf_head *nf_head;
+	int status, nf_ret;
+
+	if (!ssh_rqid_is_event(rqid)) {
+		dev_warn(dev, "event: unsupported rqid: 0x%04x\n", rqid);

A small note, "%#04x" would insert the "0x" prefix.

Quoted 7 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+		return;
+	}
 
[...]
 
+/**
+ * ssam_event_item_alloc() - Allocate an event item with the given payload size.

drivers/platform/surface/aggregator/controller.c line 549

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+ * @len:   The event payload length.
+ * @flags: The flags used for allocation.
+ *
+ * Allocate an event item with the given payload size. Sets the item
+ * operations and payload length values. The item free callback (``ops.free``)
+ * should not be overwritten after this call.
+ *
+ * Return: Returns the newly allocated event item.
+ */
+static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)

The flags argument is seemingly ignored.

drivers/platform/surface/aggregator/controller.c line 553

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+{
+	struct ssam_event_item *item;
+
+	item = kzalloc(sizeof(*item) + len, GFP_KERNEL);

I believe struct_size(item, event.data, len) could be utilized here.

Quoted 15 lines
drivers/platform/surface/aggregator/controller.c new file +12 -0
+	if (!item)
+		return NULL;
+
+	item->event.length = len;
+	return item;
+}
 
[...]
 
+static void ssam_event_queue_work_fn(struct work_struct *work)
+{
+	struct ssam_event_queue *queue;
+	struct ssam_event_item *item;
+	struct ssam_nf *nf;
+	struct device *dev;

drivers/platform/surface/aggregator/controller.c line 713

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+	int i;
+
+	queue = container_of(work, struct ssam_event_queue, work);
+	nf = &queue->cplt->event.notif;
+	dev = queue->cplt->dev;
+
+	// limit number of processed events to avoid livelocking
+	for (i = 0; i < 10; i++) {
+		item = ssam_event_queue_pop(queue);
+		if (item == NULL)

I believe !item is preferred.

Quoted 21 lines
(quoted diff) +18 -0
+			return;
+
+		ssam_nf_call(nf, dev, item->rqid, &item->event);
+		kfree(item);
+	}
+
+	if (!ssam_event_queue_is_empty(queue))
+		ssam_cplt_submit(queue->cplt, &queue->work);
+}
 
[...]
 
+static void ssam_handle_event(struct ssh_rtl *rtl,
+			      const struct ssh_command *cmd,
+			      const struct ssam_span *data)
+{
+	struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
+	struct ssam_event_item *item;
+
+	item = ssam_event_item_alloc(data->len, GFP_KERNEL);
+	if (!item)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return;
+
+	item->rqid = get_unaligned_le16(&cmd->rqid);
+	item->event.target_category = cmd->tc;
+	item->event.target_id = cmd->tid_in;
+	item->event.command_id = cmd->cid;
+	item->event.instance_id = cmd->iid;
+	memcpy(&item->event.data[0], data->ptr, data->len);
+
+	WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item));

I believe that if submission fails, item is leaked.

drivers/platform/surface/aggregator/controller.c line 926

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+}
+
+static const struct ssh_rtl_ops ssam_rtl_ops = {
+	.handle_event = ssam_handle_event,
+};
+
+
+static bool ssam_notifier_empty(struct ssam_controller *ctrl);

I think it'd be better to name it ssam_notifier_is_empty() to be consistent with the rest of the patch.

drivers/platform/surface/aggregator/controller.c line 1008

Quoted 6 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +4 -0
+static int ssam_controller_caps_load_from_acpi(
+		acpi_handle handle, struct ssam_controller_caps *caps)
+{
+	u32 d3_closes_handle = false;

Assinging a boolean like this to a u32 looks very odd to me.

Quoted 9 lines
drivers/platform/surface/aggregator/controller.c new file +9 -0
+	u64 funcs;
+	int status;
+
+	// set defaults
+	caps->ssh_power_profile = (u32)-1;
+	caps->screen_on_sleep_idle_timeout = (u32)-1;
+	caps->screen_off_sleep_idle_timeout = (u32)-1;
+	caps->d3_closes_handle = false;
+	caps->ssh_buffer_size = (u32)-1;

drivers/platform/surface/aggregator/controller.c line 1123

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +7 -0
 
[...]
 
+
+/**
+ * ssam_controller_start() - Start the receiver and transmitter threads of the
+ * controller.
+ * @ctrl: The controller.
+ *
+ * Note: When this function is called, the controller shouldbe properly hooked
                                                               ^

space

Quoted 1 line

+ * up to the serdev core via &struct serdev_device_ops. Please refert to

                                                                       ^

"refer"

drivers/platform/surface/aggregator/controller.c line 1181

Quoted 12 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +10 -0
+void ssam_controller_shutdown(struct ssam_controller *ctrl)
+{
+	enum ssam_controller_state s = ctrl->state;
+	int status;
+
+	if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
+		return;
+
+	// try to flush pending events and requests while everything still works
+	status = ssh_rtl_flush(&ctrl->rtl, msecs_to_jiffies(5000));

Wouldn't it be better to name that 5000?

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+	if (status) {
+		ssam_err(ctrl, "failed to flush request transport layer: %d\n",
+			 status);
+	}
 
[...]

drivers/platform/surface/aggregator/controller.c line 1222

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +9 -0
 
+
+/**
+ * ssam_controller_destroy() - Destroy the controller and free its resources.
+ * @ctrl: The controller.
+ *
+ * Ensures that all resources associated with the controller get freed. This
+ * function should only be called after the controller has been stopped via
+ * ssam_controller_shutdown(). In general, this function should not be called
+ * directly. The only valid place to call this function direclty is during
                                                                ^

"directly"

Quoted 15 lines

+ * initialization, before the controller has been fully initialized and passed
+ * to other processes. This function is called automatically when the
+ * reference count of the controller reaches zero.
+ *
+ * Must be called from an exclusive context with regards to the controller
+ * state.
+ */

[...]

drivers/platform/surface/aggregator/controller.c new file +5 -0
+int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
+			    struct ssam_request_sync **rqst,
+			    struct ssam_span *buffer)
+{
+	size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);

drivers/platform/surface/aggregator/controller.c line 1453

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+
+	*rqst = kzalloc(sizeof(**rqst) + msglen, flags);
+	if (!*rqst)
+		return -ENOMEM;
+
+	buffer->ptr = (u8 *)(*rqst + 1);
+	buffer->len = msglen;
+
+	return 0;
+}

I think there is a bit of incosistency: sometimes you use ** pointer + return int, sometimes you return a pointer with potentially embedded errno. I think it would be better if you stuck with one or the other.

drivers/platform/surface/aggregator/controller.c line 1804

Quoted 8 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +5 -0
+static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
+				  struct ssam_event_registry reg,
+				  struct ssam_event_id id, u8 flags)
+{
[...]
+	rqst.command_id = reg.cid_disable;

If I see it correctly, this line is the only significant difference between this one and the previous function. Is there any reason they're not combined?

Quoted 12 lines

[...]

+/**
+ * ssam_notifier_disable_registered() - Disable events for all registered
+ * notifiers.
+ * @ctrl: The controller for which to disable the notifiers/events.
+ *
+ * Disables events for all currently registered notifiers. In case of an error
+ * (EC command failing), all previously disabled events will be restored and
+ * the error code returned.
+ *
+ * This function is intended to disable all events prior to hibenration entry.

                                                                   ^

"hibernation"

drivers/platform/surface/aggregator/controller.c line 2479

Quoted 12 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +10 -0
+void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
+{
+	int status;
+
+	if (ctrl->irq.wakeup_enabled) {
+		status = disable_irq_wake(ctrl->irq.num);
+		if (status)
+			ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
+
+		ctrl->irq.wakeup_enabled = false;

It's set to false even if disable_irq_wake() fails?

Quoted 5 lines
drivers/platform/surface/aggregator/core.c new file +3 -0
+	}
+	disable_irq(ctrl->irq.num);
+}
 
[...]

drivers/platform/surface/aggregator/core.c line 83

Quoted 10 lines
drivers/platform/surface/aggregator/core.c new file +9 -0
 
+static int ssam_try_set_controller(struct ssam_controller *ctrl)
+{
+	int status = 0;
+
+	spin_lock(&__ssam_controller_lock);
+	if (!__ssam_controller)
+		__ssam_controller = ctrl;
+	else
+		status = -EBUSY;

I feel like EBUSY might not be the best errno here.

Quoted 16 lines
drivers/platform/surface/aggregator/core.c new file +13 -0
+	spin_unlock(&__ssam_controller_lock);
+
+	return status;
+}
 
[...]
 
+int ssam_client_link(struct ssam_controller *c, struct device *client)
+{
+	const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
+	struct device_link *link;
+	struct device *ctrldev;
+
+	ssam_controller_statelock(c);
+
+	if (c->state != SSAM_CONTROLLER_STARTED) {

drivers/platform/surface/aggregator/core.c line 140

Quoted 10 lines
drivers/platform/surface/aggregator/core.c new file +10 -0
+		ssam_controller_stateunlock(c);
+		return -ENXIO;
+	}
+
+	ctrldev = ssam_controller_device(c);
+	if (!ctrldev) {
+		ssam_controller_stateunlock(c);
+		return -ENXIO;
+	}
+

I'm not sure if ENXIO is the best errno in the last two returns;

Quoted 21 lines
drivers/platform/surface/aggregator/core.c new file +20 -0
+	link = device_link_add(client, ctrldev, flags);
+	if (!link) {
+		ssam_controller_stateunlock(c);
+		return -ENOMEM;
+	}
+
+	/*
+	 * Return -ENXIO if supplier driver is on its way to be removed. In this
+	 * case, the controller won't be around for much longer and the device
+	 * link is not going to save us any more, as unbinding is already in
+	 * progress.
+	 */
+	if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
+		ssam_controller_stateunlock(c);
+		return -ENXIO;
+	}
+
+	ssam_controller_stateunlock(c);
+	return 0;
+}
 

drivers/platform/surface/aggregator/core.c line 206

Quoted 10 lines
drivers/platform/surface/aggregator/core.c new file +8 -0
[...]
 
+int ssam_client_bind(struct device *client, struct ssam_controller **ctrl)
+{
+	struct ssam_controller *c;
+	int status;
+
+	c = ssam_get_controller();
+	if (!c)
+		return -ENXIO;

To me, ENODEV seems like a better fit here.

Quoted 12 lines
drivers/platform/surface/aggregator/core.c new file +8 -0
+
+	status = ssam_client_link(c, client);
 
[...]
 
 
+static ssize_t firmware_version_show(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	struct ssam_controller *ctrl = dev_get_drvdata(dev);
+	u32 version, a, b, c;
+	int status;

drivers/platform/surface/aggregator/core.c line 281

Quoted 10 lines
drivers/platform/surface/aggregator/core.c new file +10 -0
+
+	status = ssam_get_firmware_version(ctrl, &version);
+	if (status < 0)
+		return status;
+
+	a = (version >> 24) & 0xff;
+	b = ((version >> 8) & 0xffff);
+	c = version & 0xff;
+
+	return snprintf(buf, PAGE_SIZE - 1, "%u.%u.%u\n", a, b, c);

snprintf() takes care of the null byte, so simply PAGE_SIZE would've been sufficient. But that doesn't matter much since you should use sysfs_emit().

drivers/platform/surface/aggregator/core.c line 287

Quoted 6 lines
drivers/platform/surface/aggregator/core.c new file +6 -0
+}
+static DEVICE_ATTR_RO(firmware_version);
+
+static struct attribute *ssam_sam_attrs[] = {
+	&dev_attr_firmware_version.attr,
+	NULL,
            ^

I believe it is preferred to omit the comma after the terminating entry.

Quoted 18 lines

[...]

drivers/platform/surface/aggregator/ssh_msgb.h new file +16 -0
+/**
+ * msgb_push_cmd() - Push a SSH command frame with payload to the buffer.
+ * @msgb: The message buffer.
+ * @seq:  The sequence ID (SEQ) of the frame/packet.
+ * @rqid: The request ID (RQID) of the request contained in the frame.
+ * @rqst: The request to wrap in the frame.
+ */
+static inline void msgb_push_cmd(struct msgbuf *msgb, u8 seq, u16 rqid,
+				 const struct ssam_request *rqst)
+{
+	struct ssh_command *cmd;
+	const u8 *cmd_begin;
+	const u8 type = SSH_FRAME_TYPE_DATA_SEQ;
+
+	// SYN
+	msgb_push_syn(msgb);

drivers/platform/surface/aggregator/ssh_msgb.h line 182

Quoted 10 lines
drivers/platform/surface/aggregator/ssh_msgb.h new file +10 -0
+
+	// command frame + crc
+	msgb_push_frame(msgb, type, sizeof(*cmd) + rqst->length, seq);
+
+	// frame payload: command struct + payload
+	if (WARN_ON(msgb->ptr + sizeof(*cmd) > msgb->end))
+		return;
+
+	cmd_begin = msgb->ptr;
+	cmd = (struct ssh_command *)msgb->ptr;

I believe this violates strict aliasing.

Quoted 12 lines

[...]

+ * Note that the packet completion callback is, in case of success and for a
+ * sequenced packet, guaranteed to run on the receiver thread, thus providing
+ * a way to reliably identify responses to the packet. The packet completion
+ * callback is only run once and it does not indicate that the packet has
+ * fully left the system (for this, one should rely on the release method,
+ * triggered when the reference count of the packet reaches zero). In case of
+ * re-submission (and with somewhat unlikely timing), it may be possible that
+ * the packet is being re-transmitted while the completion callback runs.
+ * Completion will occur both on success and internal error, as well as when
+ * the packet is canceled.

If I understand it correctly, it is possible that submission of a packet fails for the first time, but it's scheduled for resubmission, and this retransmission happens at the same time when the complete() callback is called. If that's the case, then the callback is called with an error condition, no? Thus it is possible that a packet is successfully submitted (for the second, third, etc. time), but the complete() callback receives notification about failure? Or am I missing something?

drivers/platform/surface/aggregator/ssh_packet_layer.c line 1469

Quoted 10 lines

[...]

drivers/platform/surface/aggregator/ssh_packet_layer.c new file +8 -0
+int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
+{
+	int used;
+
+	if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
+		return -ESHUTDOWN;
+
+	used = kfifo_in(&ptl->rx.fifo, buf, n);

Isn't it possible that n is greater than the free space in the fifo? What happens then?

Quoted 7 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c new file +5 -0
+	if (used)
+		ssh_ptl_rx_wakeup(ptl);
+
+	return used;
+}
 
[...]

drivers/platform/surface/aggregator/ssh_packet_layer.c line 1532

Quoted 10 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c new file +8 -0
 
+void ssh_ptl_shutdown(struct ssh_ptl *ptl)
+{
[...]
+	 * Note 2: We can re-use queue_node (or pending_node) if we mark the
+	 * packet as locked an then remove it from the queue (or pending set
+	 * respecitvely). Marking the packet as locked avoids re-queueing
+	 * (which should already be prevented by having stopped the treads...)
+	 * and not setting QUEUED_BIT (or PENDING_BIT) prevents removal from a
+	 * new list via other threads (e.g. canellation).
                                               ^

"cancellation"

drivers/platform/surface/aggregator/ssh_packet_layer.c

Quoted 11 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c new file +8 -0
+	 *
+	 * Note 3: There may be overlap between complete_p and complete_q.
+	 * This is handled via test_and_set_bit() on the "completed" flag
+	 * (also handles cancellation).
+	 */
 
[...]
 
+#define __ssam_prcond(func, p, fmt, ...)		\
+	do {						\
+		if ((p))				\

I believe if (p) is sufficient.

Quoted 4 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +2 -0
+			func((p), fmt, ##__VA_ARGS__);	\
+	} while (0)
 
[...]

drivers/platform/surface/aggregator/ssh_parser.c line 154

Quoted 10 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +8 -0
 
+int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
+		     struct ssh_frame **frame, struct ssam_span *payload,
+		     size_t maxlen)
+{
[...]
+	// ensure packet does not exceed maximum length
+	sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
+	if (unlikely(sp.len + SSH_MESSAGE_LENGTH(0) > maxlen)) {
+		dev_warn(dev, "rx: parser: frame too large: %u bytes\n",

I believe %hu would be more appropriate.

drivers/platform/surface/aggregator/ssh_parser.c line 155

Quoted 1 line
drivers/platform/surface/aggregator/ssh_parser.c new file +1 -0
+			 ((struct ssh_frame *)sf.ptr)->len);

Why isn't get_unaligned_le16() used here? (Or simply even sp.len.)

drivers/platform/surface/aggregator/ssh_parser.c line 174

Quoted 4 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +3 -0
+		return -EMSGSIZE;
+	}
[...]
+	*frame = (struct ssh_frame *)sf.ptr;

This also violates strict aliasing.

Quoted 15 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +12 -0
+	*payload = sp;
+
+	dev_dbg(dev, "rx: parser: valid frame found (type: 0x%02x, len: %u)\n",
+		(*frame)->type, (*frame)->len);
+
+	return 0;
+}
 
[...]
 
+int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
+		       struct ssh_command **command,
+		       struct ssam_span *command_data)
+{
+	// check for minimum length

drivers/platform/surface/aggregator/ssh_parser.c line 221

Quoted 10 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +10 -0
+	if (unlikely(source->len < sizeof(struct ssh_command))) {
+		*command = NULL;
+		command_data->ptr = NULL;
+		command_data->len = 0;
+
+		dev_err(dev, "rx: parser: command payload is too short\n");
+		return -ENOMSG;
+	}
+
+	*command = (struct ssh_command *)source->ptr;

I'm quite sure this also violates strict aliasing.

Quoted 15 lines
(quoted diff) +12 -0
+	command_data->ptr = source->ptr + sizeof(struct ssh_command);
+	command_data->len = source->len - sizeof(struct ssh_command);
+
+	dev_dbg(dev, "rx: parser: valid command found (tc: 0x%02x, cid: 0x%02x)\n",
+		(*command)->tc, (*command)->cid);
+
+	return 0;
+}
 
[...]
 
+#define SSH_MSGOFFSET_FRAME(field) \
+	(sizeof(u16) + offsetof(struct ssh_frame, field))
+
+/**

Review

Quoted 10 lines
(quoted diff) +10 -0
+ * SSH_MSGOFFSET_COMMAND() - Compute offset in SSH message to specified field
+ * in command.
+ * @field: The field for which the offset should be computed.
+ *
+ * Return: Returns the offset of the specified &struct ssh_command field in
+ * the raw SSH message data.
+ */
+#define SSH_MSGOFFSET_COMMAND(field) \
+	(2ull * sizeof(u16) + sizeof(struct ssh_frame) \
+		+ offsetof(struct ssh_command, field))

I believe it should be noted (here and for SSH_MSGOFFSET_FRAME()) why the sizeof(u16)s are necessary.

Quoted 35 lines
include/linux/surface_aggregator/serial_hub.h new file +32 -0
+
+/*
+ * SSH_MSG_SYN - SSH message synchronization (SYN) bytes as u16.
+ */
+#define SSH_MSG_SYN		((u16)0x55aa)
 
[...]
 
+enum ssam_ssh_tc {
+	/* Known SSH/EC target categories. */
+				// category 0x00 is invalid for EC use
+	SSAM_SSH_TC_SAM = 0x01,	// generic system functionality, real-time clock
+	SSAM_SSH_TC_BAT = 0x02,	// battery/power subsystem
+	SSAM_SSH_TC_TMP = 0x03,	// thermal subsystem
+	SSAM_SSH_TC_PMC = 0x04,
+	SSAM_SSH_TC_FAN = 0x05,
+	SSAM_SSH_TC_PoM = 0x06,
+	SSAM_SSH_TC_DBG = 0x07,
+	SSAM_SSH_TC_KBD = 0x08,	// legacy keyboard (Laptop 1/2)
+	SSAM_SSH_TC_FWU = 0x09,
+	SSAM_SSH_TC_UNI = 0x0a,
+	SSAM_SSH_TC_LPC = 0x0b,
+	SSAM_SSH_TC_TCL = 0x0c,
+	SSAM_SSH_TC_SFL = 0x0d,
+	SSAM_SSH_TC_KIP = 0x0e,
+	SSAM_SSH_TC_EXT = 0x0f,
+	SSAM_SSH_TC_BLD = 0x10,
+	SSAM_SSH_TC_BAS = 0x11,	// detachment system (Surface Book 2/3)
+	SSAM_SSH_TC_SEN = 0x12,
+	SSAM_SSH_TC_SRQ = 0x13,
+	SSAM_SSH_TC_MCU = 0x14,
+	SSAM_SSH_TC_HID = 0x15,	// generic HID input subsystem
+	SSAM_SSH_TC_TCH = 0x16,
+	SSAM_SSH_TC_BKL = 0x17,
+	SSAM_SSH_TC_TAM = 0x18,

include/linux/surface_aggregator/serial_hub.h line 317

Quoted 10 lines
include/linux/surface_aggregator/serial_hub.h new file +10 -0
+	SSAM_SSH_TC_ACC = 0x19,
+	SSAM_SSH_TC_UFI = 0x1a,
+	SSAM_SSH_TC_USC = 0x1b,
+	SSAM_SSH_TC_PEN = 0x1c,
+	SSAM_SSH_TC_VID = 0x1d,
+	SSAM_SSH_TC_AUD = 0x1e,
+	SSAM_SSH_TC_SMC = 0x1f,
+	SSAM_SSH_TC_KPD = 0x20,
+	SSAM_SSH_TC_REG = 0x21,
+};

Is it known what these abbreviations stand for? Maybe I missed them?

Quoted 13 lines

[...]

+/**
+ * struct ssh_request_ops - Callback operations for a SSH request.

+ * @release:  Function called when the request's reference count reaches zero.
+ *            This callback must be relied upon to ensure that the request has
+ *            left the transport systems (both, packet an request systems).
+ * @complete: Function called when the request is completed, either with
+ *            success or failure. The command data for the request response
+ *            is provided via the &struct ssh_command parameter (``cmd``),
+ *            the command payload of the request response via the &struct
+ *            ssh_span parameter (``data``).

+ *

Quoted 8 lines
+ *            If the request does not have any response or has not been
+ *            completed with success, both ``cmd`` and ``data`` parameters will
+ *            be NULL. If the request response does not have any command
+ *            payload, the ``data`` span will be an empty (zero-length) span.
+ *
+ *            In case of failure, the reason for the failure is indicated by
+ *            the value of the provided status code argument (``status``). This
+ *            value will be zero in case of success.

I believe it should be noted if the status argument is a regular errno, or something different.

include/linux/surface_aggregator/serial_hub.h line 570

Quoted 11 lines

+ *
+ * Note that a call to this callback does not guarantee that the
+ * request is not in use by the transport systems any more.
+ */

include/linux/surface_aggregator/serial_hub.h new file +6 -0
+struct ssh_request_ops {
+	void (*release)(struct ssh_request *rqst);
+	void (*complete)(struct ssh_request *rqst,
+			 const struct ssh_command *cmd,
+			 const struct ssam_span *data, int status);
+};
[...]

I have to agree, this is quite a sizable patch, although I think it's well-commented, which helps reading the code by a lot, however, in some places I feel like it's a bit over-engineered (or maybe I just cannot fully appreciate the subject at hand at the moment), nonetheless, I applaud your efforts, I can only imagine the hours that must have gone into it.

Regards,
Barnabás Pőcze

author maintainer
#6 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

Quoted 3 lines · Barnabás Pőcze

Hi

I have attached some thoughts and comments inline.

Thanks!

Quoted 11 lines

2020. november 15., vasárnap 20:21 keltezéssel, Maximilian Luz írta:

drivers/platform/surface/aggregator/controller.c line 101

Quoted 8 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +7 -0
+/* -- Event notifier/callbacks. --------------------------------------------- */
+/*
+ * The notifier system is based on linux/notifier.h, specifically the SRCU
+ * implementation. The difference to that is, that some bits of the notifier
+ * call return value can be tracked across multiple calls. This is done so that
+ * handling of events can be tracked and a warning can be issued in case an
+ * event goes unhandled. The idea of that waring is that it should help discover
                                                 ^
Quoted 1 line

"warning"

Thanks, I'll run a spell-checker over the comments and (hopefully) fix all typos.

Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 107

Quoted 6 lines

+ * and identify new/currently unimplemented features.
+ */

drivers/platform/surface/aggregator/controller.c new file +4 -0
+
+
+/**
+ * ssam_event_matches_notifier() - Test if an event matches a notifier;
                                                                          ^

Shouldn't it be a period?

Yes, it should.

Quoted 9 lines
Quoted 8 lines

+ * @notif: The event notifier to test against.
+ * @event: The event to test.
+ *
+ * Return: Returns %true iff the given event matches the given notifier
+ * according to the rules set in the notifier's event mask, %false otherwise.
+ */

[...]

Quoted 12 lines

drivers/platform/surface/aggregator/controller.c line 270

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+static int __ssam_nfblk_remove(struct ssam_nf_head *nh,
+			       struct ssam_notifier_block *nb)
+{
+	struct ssam_notifier_block **link;
+
+	link = __ssam_nfblk_find_link(nh, nb);
+	if (!link)
+		return -ENOENT;

I find it odd that here you return ENOENT, but in __ssam_nfblk_insert() EINVAL is returned instead of EEXIST. I believe either both should be EINVAL, or EEXIST+ENOENT.

Yes, EEXIST is better for insert.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	__ssam_nfblk_erase(link);

I'm wondering if it's necessary to create a new function which contains just a single line.

I prefer to create these sorts of abstractions as this, in my opinion, keeps things contained and makes things easier to reason about. In this case, it also allows for documentation.

Quoted 5 lines
Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+	return 0;
+}
 
[...]
Quoted 11 lines

drivers/platform/surface/aggregator/controller.c line 299

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+/**
+ * ssam_nf_head_destroy() - Deinitialize the given notifier head.
+ * @nh: The notifier head to deinitialize.
+ */
+static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
+{
+	cleanup_srcu_struct(&nh->srcu);
+}

I'm also wondering if there's any reason why these static one-liner functions are not explicitly marked inline.

Isn't inline more of a linkage keyword at this point? I fully expect any compiler to inline things like this automatically at the first hint of an optimization flag.

Quoted 16 lines
Quoted 4 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +2 -0
+/**
+ * struct ssam_nf_refcount_entry - RB-tree entry for referecnce counting event

drivers/platform/surface/aggregator/controller.c line 326

Quoted 10 lines
drivers/platform/surface/aggregator/controller.c new file +10 -0
+ * activations.
+ * @node:     The node of this entry in the rb-tree.
+ * @key:      The key of the event.
+ * @refcount: The reference-count of the event.
+ * @flags:    The flags used when enabling the event.
+ */
+struct ssam_nf_refcount_entry {
+	struct rb_node node;
+	struct ssam_nf_refcount_key key;
+	int refcount;

Is there any reason why a signed type is used for reference counting?

Can help to spot overflows when debugging, not that that should happen. We also don't need values larger than INT_MAX. We shouldn't even get remotely close to it.

Quoted 8 lines
Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+	u8 flags;
+};
+
+
+/**
+ * ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
+ * event.
+ * @nf:  The notifier system reference.
Quoted 8 lines
Quoted 6 lines

+ * @reg: The registry used to enable/disable the event.
+ * @id: The event ID.
+ *
+ * Increments the reference-/activation-count associated with the specified
+ * event type/ID, allocating a new entry for this event ID if necessary. A
+ * newly allocated entry will have a refcount of one.

Shouldn't it be noted that nf->lock(?) must(?) be held when calling?

All functions taking 'struct ssam_nf *' as first parameter must run synchronized with regards to each other, except for ssam_nf_call(). Same for all access to struct ssam_nf directly. Whether that is guaranteed by nf->lock or other means (de-/initialization) doesn't really matter. Only higher-level functions may run concurrently.

On the higher level, the lock must also be held to ensure that the EC requests are sent at the right time (which is its main purpose).

Arguably that's not well documented here, I'll try to fix that.

[...]

Quoted 4 lines
Quoted 2 lines

+ * callback is left or a callback returned a value with the %SSAM_NOTIF_STOP
+ * bit set. Note that this bit is set automatically when converting non.zero

                                                                           ^

maybe "non-zero"?

Right.

Quoted 11 lines
Quoted 11 lines

+ * error values via ssam_notifier_from_errno() to notifier values.
+ *
+ * Also note that any callback that could handle an event should return a value
+ * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
+ * unhandled/ignored. In case no registered callback could handle an event,
+ * this function will emit a warning.
+ *
+ * In case a callback failed, this function will emit an error message.
+ */

drivers/platform/surface/aggregator/controller.c new file +2 -0
+static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
+			 struct ssam_event *event)
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 471

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+{
+	struct ssam_nf_head *nf_head;
+	int status, nf_ret;
+
+	if (!ssh_rqid_is_event(rqid)) {
+		dev_warn(dev, "event: unsupported rqid: 0x%04x\n", rqid);

A small note, "%#04x" would insert the "0x" prefix.

Oh neat, didn't know that. Should be "%#06x" though, right?

Quoted 5 lines
Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +2 -0
+		return;
+	}
 
[...]
Quoted 14 lines

drivers/platform/surface/aggregator/controller.c line 549

Quoted 12 lines
drivers/platform/surface/aggregator/controller.c new file +12 -0
+/**
+ * ssam_event_item_alloc() - Allocate an event item with the given payload size.
+ * @len:   The event payload length.
+ * @flags: The flags used for allocation.
+ *
+ * Allocate an event item with the given payload size. Sets the item
+ * operations and payload length values. The item free callback (``ops.free``)
+ * should not be overwritten after this call.
+ *
+ * Return: Returns the newly allocated event item.
+ */
+static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)

The flags argument is seemingly ignored.

Oh, right.

Quoted 6 lines

drivers/platform/surface/aggregator/controller.c line 553

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+{
+	struct ssam_event_item *item;
+
+	item = kzalloc(sizeof(*item) + len, GFP_KERNEL);

I believe struct_size(item, event.data, len) could be utilized here.

Right, I'll use that.

Quoted 19 lines
Quoted 19 lines
drivers/platform/surface/aggregator/controller.c new file +16 -0
+	if (!item)
+		return NULL;
+
+	item->event.length = len;
+	return item;
+}
 
[...]
 
+static void ssam_event_queue_work_fn(struct work_struct *work)
+{
+	struct ssam_event_queue *queue;
+	struct ssam_event_item *item;
+	struct ssam_nf *nf;
+	struct device *dev;
+	int i;
+
+	queue = container_of(work, struct ssam_event_queue, work);
+	nf = &queue->cplt->event.notif;
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 713

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+	dev = queue->cplt->dev;
+
+	// limit number of processed events to avoid livelocking
+	for (i = 0; i < 10; i++) {
+		item = ssam_event_queue_pop(queue);
+		if (item == NULL)

I believe !item is preferred.

Right.

Quoted 25 lines
Quoted 25 lines
drivers/platform/surface/aggregator/controller.c new file +22 -0
+			return;
+
+		ssam_nf_call(nf, dev, item->rqid, &item->event);
+		kfree(item);
+	}
+
+	if (!ssam_event_queue_is_empty(queue))
+		ssam_cplt_submit(queue->cplt, &queue->work);
+}
 
[...]
 
+static void ssam_handle_event(struct ssh_rtl *rtl,
+			      const struct ssh_command *cmd,
+			      const struct ssam_span *data)
+{
+	struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
+	struct ssam_event_item *item;
+
+	item = ssam_event_item_alloc(data->len, GFP_KERNEL);
+	if (!item)
+		return;
+
+	item->rqid = get_unaligned_le16(&cmd->rqid);
+	item->event.target_category = cmd->tc;
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 918

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+	item->event.target_id = cmd->tid_in;
+	item->event.command_id = cmd->cid;
+	item->event.instance_id = cmd->iid;
+	memcpy(&item->event.data[0], data->ptr, data->len);
+
+	WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item));

I believe that if submission fails, item is leaked.

Oh, that is correct. Thanks for spotting that!

Quoted 11 lines

drivers/platform/surface/aggregator/controller.c line 926

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+}
+
+static const struct ssh_rtl_ops ssam_rtl_ops = {
+	.handle_event = ssam_handle_event,
+};
+
+
+static bool ssam_notifier_empty(struct ssam_controller *ctrl);

I think it'd be better to name it ssam_notifier_is_empty() to be consistent with the rest of the patch.

Okay, I'll change that.

Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 1008

Quoted 6 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +4 -0
+static int ssam_controller_caps_load_from_acpi(
+		acpi_handle handle, struct ssam_controller_caps *caps)
+{
+	u32 d3_closes_handle = false;

Assinging a boolean like this to a u32 looks very odd to me.

The value returned by the corresponding DSM call is an integer, but essentially only contains a bool value (zero vs. one). I thought using false here explicitly for initialization helps clarify that. That also makes it consistent with the "caps->d3_closes_handle = false" line below.

Quoted 9 lines
Quoted 9 lines
drivers/platform/surface/aggregator/controller.c new file +9 -0
+	u64 funcs;
+	int status;
+
+	// set defaults
+	caps->ssh_power_profile = (u32)-1;
+	caps->screen_on_sleep_idle_timeout = (u32)-1;
+	caps->screen_off_sleep_idle_timeout = (u32)-1;
+	caps->d3_closes_handle = false;
+	caps->ssh_buffer_size = (u32)-1;

[...]

Quoted 4 lines
Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+void ssam_controller_shutdown(struct ssam_controller *ctrl)
+{
+	enum ssam_controller_state s = ctrl->state;
+	int status;
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 1181

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+
+	if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
+		return;
+
+	// try to flush pending events and requests while everything still works
+	status = ssh_rtl_flush(&ctrl->rtl, msecs_to_jiffies(5000));

Wouldn't it be better to name that 5000?

I'll do that.

Quoted 4 lines
Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+	if (status) {
+		ssam_err(ctrl, "failed to flush request transport layer: %d\n",
+			 status);
+	}

[...]

Quoted 11 lines
Quoted 11 lines
drivers/platform/surface/aggregator/controller.c new file +11 -0
+int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
+			    struct ssam_request_sync **rqst,
+			    struct ssam_span *buffer)
+{
+	size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
+
+	*rqst = kzalloc(sizeof(**rqst) + msglen, flags);
+	if (!*rqst)
+		return -ENOMEM;
+
+	buffer->ptr = (u8 *)(*rqst + 1);
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 1453

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+	buffer->len = msglen;
+
+	return 0;
+}

I think there is a bit of incosistency: sometimes you use ** pointer + return int, sometimes you return a pointer with potentially embedded errno. I think it would be better if you stuck with one or the other.

My rule of thumb is: If it's one output parameter, use a pointer as return value. If it's two or more output parameters (as in this case, "buffer" is written to), use two pointer arguments and an int as return.

I noticed that ssam_client_bind doesn't follow that scheme so I'll change that.

Quoted 11 lines

drivers/platform/surface/aggregator/controller.c line 1804

Quoted 8 lines

[...]

drivers/platform/surface/aggregator/controller.c new file +5 -0
+static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
+				  struct ssam_event_registry reg,
+				  struct ssam_event_id id, u8 flags)
+{
[...]
+	rqst.command_id = reg.cid_disable;

If I see it correctly, this line is the only significant difference between this one and the previous function. Is there any reason they're not combined?

Debug and error messages are also different. I'll try to put the common code into one function though.

[...]

Quoted 4 lines
Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
+{
+	int status;
+
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 2479

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+	if (ctrl->irq.wakeup_enabled) {
+		status = disable_irq_wake(ctrl->irq.num);
+		if (status)
+			ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
+
+		ctrl->irq.wakeup_enabled = false;

It's set to false even if disable_irq_wake() fails?

Yes, on purpose. The irq.wakeup_enabled flag is there to ensure that disable_irq_wake() gets called only if there has been a successful enable_irq_wake() call.

This seems to be a common pattern, see e.g. /drivers/mmc/host/sdhci.c

Quoted 6 lines
Quoted 5 lines
drivers/platform/surface/aggregator/controller.c new file +3 -0
+	}
+	disable_irq(ctrl->irq.num);
+}
 
[...]
Quoted 11 lines

drivers/platform/surface/aggregator/core.c line 83

Quoted 9 lines
drivers/platform/surface/aggregator/core.c new file +9 -0
+static int ssam_try_set_controller(struct ssam_controller *ctrl)
+{
+	int status = 0;
+
+	spin_lock(&__ssam_controller_lock);
+	if (!__ssam_controller)
+		__ssam_controller = ctrl;
+	else
+		status = -EBUSY;

I feel like EBUSY might not be the best errno here.

Okay, I'll replace it with EEXIST.

Quoted 20 lines
Quoted 20 lines
drivers/platform/surface/aggregator/core.c new file +17 -0
+	spin_unlock(&__ssam_controller_lock);
+
+	return status;
+}
 
[...]
 
+int ssam_client_link(struct ssam_controller *c, struct device *client)
+{
+	const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
+	struct device_link *link;
+	struct device *ctrldev;
+
+	ssam_controller_statelock(c);
+
+	if (c->state != SSAM_CONTROLLER_STARTED) {
+		ssam_controller_stateunlock(c);
+		return -ENXIO;
+	}
+
Quoted 8 lines

drivers/platform/surface/aggregator/core.c line 140

Quoted 6 lines
drivers/platform/surface/aggregator/core.c new file +6 -0
+	ctrldev = ssam_controller_device(c);
+	if (!ctrldev) {
+		ssam_controller_stateunlock(c);
+		return -ENXIO;
+	}
+

I'm not sure if ENXIO is the best errno in the last two returns;

I guess I can return ENODEV.

Quoted 23 lines
Quoted 22 lines
drivers/platform/surface/aggregator/core.c new file +20 -0
+	link = device_link_add(client, ctrldev, flags);
+	if (!link) {
+		ssam_controller_stateunlock(c);
+		return -ENOMEM;
+	}
+
+	/*
+	 * Return -ENXIO if supplier driver is on its way to be removed. In this
+	 * case, the controller won't be around for much longer and the device
+	 * link is not going to save us any more, as unbinding is already in
+	 * progress.
+	 */
+	if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
+		ssam_controller_stateunlock(c);
+		return -ENXIO;
+	}
+
+	ssam_controller_stateunlock(c);
+	return 0;
+}
 
[...]
Quoted 10 lines

drivers/platform/surface/aggregator/core.c line 206

Quoted 8 lines
drivers/platform/surface/aggregator/core.c new file +8 -0
+int ssam_client_bind(struct device *client, struct ssam_controller **ctrl)
+{
+	struct ssam_controller *c;
+	int status;
+
+	c = ssam_get_controller();
+	if (!c)
+		return -ENXIO;

To me, ENODEV seems like a better fit here.

Okay, I'll change it as well as above.

Quoted 17 lines
Quoted 17 lines
drivers/platform/surface/aggregator/core.c new file +13 -0
+
+	status = ssam_client_link(c, client);
 
[...]
 
 
+static ssize_t firmware_version_show(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	struct ssam_controller *ctrl = dev_get_drvdata(dev);
+	u32 version, a, b, c;
+	int status;
+
+	status = ssam_get_firmware_version(ctrl, &version);
+	if (status < 0)
+		return status;
+
Quoted 8 lines

drivers/platform/surface/aggregator/core.c line 281

Quoted 5 lines
drivers/platform/surface/aggregator/core.c new file +5 -0
+	a = (version >> 24) & 0xff;
+	b = ((version >> 8) & 0xffff);
+	c = version & 0xff;
+
+	return snprintf(buf, PAGE_SIZE - 1, "%u.%u.%u\n", a, b, c);

snprintf() takes care of the null byte, so simply PAGE_SIZE would've been sufficient. But that doesn't matter much since you should use sysfs_emit().

I didn't know about sysfs_emit(), I'll switch to that then. Also you're right about that "- 1", not sure where I got that from.

Quoted 8 lines

drivers/platform/surface/aggregator/core.c line 287

Quoted 6 lines
drivers/platform/surface/aggregator/core.c new file +6 -0
+}
+static DEVICE_ATTR_RO(firmware_version);
+
+static struct attribute *ssam_sam_attrs[] = {
+	&dev_attr_firmware_version.attr,
+	NULL,
             ^

I believe it is preferred to omit the comma after the terminating entry.

Right.

Quoted 22 lines
Quoted 22 lines

[...]

drivers/platform/surface/aggregator/ssh_msgb.h new file +20 -0
+/**
+ * msgb_push_cmd() - Push a SSH command frame with payload to the buffer.
+ * @msgb: The message buffer.
+ * @seq:  The sequence ID (SEQ) of the frame/packet.
+ * @rqid: The request ID (RQID) of the request contained in the frame.
+ * @rqst: The request to wrap in the frame.
+ */
+static inline void msgb_push_cmd(struct msgbuf *msgb, u8 seq, u16 rqid,
+				 const struct ssam_request *rqst)
+{
+	struct ssh_command *cmd;
+	const u8 *cmd_begin;
+	const u8 type = SSH_FRAME_TYPE_DATA_SEQ;
+
+	// SYN
+	msgb_push_syn(msgb);
+
+	// command frame + crc
+	msgb_push_frame(msgb, type, sizeof(*cmd) + rqst->length, seq);
+
Quoted 8 lines

drivers/platform/surface/aggregator/ssh_msgb.h line 182

Quoted 6 lines
drivers/platform/surface/aggregator/ssh_msgb.h new file +6 -0
+	// frame payload: command struct + payload
+	if (WARN_ON(msgb->ptr + sizeof(*cmd) > msgb->end))
+		return;
+
+	cmd_begin = msgb->ptr;
+	cmd = (struct ssh_command *)msgb->ptr;

I believe this violates strict aliasing.

It does. The same happens in msgb_push_frame(), too. The kernel is built with -fno-strict-aliasing though if I remember correctly.

I guess I can try to avoid that in those two functions with put_unaligned and offsetof to compute the pointers.

Quoted 13 lines
Quoted 12 lines

[...]

+ * Note that the packet completion callback is, in case of success and for a
+ * sequenced packet, guaranteed to run on the receiver thread, thus providing
+ * a way to reliably identify responses to the packet. The packet completion
+ * callback is only run once and it does not indicate that the packet has
+ * fully left the system (for this, one should rely on the release method,
+ * triggered when the reference count of the packet reaches zero). In case of
+ * re-submission (and with somewhat unlikely timing), it may be possible that
+ * the packet is being re-transmitted while the completion callback runs.
+ * Completion will occur both on success and internal error, as well as when
+ * the packet is canceled.

Quoted 6 lines

If I understand it correctly, it is possible that submission of a packet fails for the first time, but it's scheduled for resubmission, and this retransmission happens at the same time when the complete() callback is called. If that's the case, then the callback is called with an error condition, no? Thus it is possible that a packet is successfully submitted (for the second, third, etc. time), but the complete() callback receives notification about failure? Or am I missing something?

Not sure I can follow you here. What do you mean by "fails for the first time"?

A couple of notes before I try to explain the error cases:

- Resubmission only happens on timeout or NAK.

- Only sequenced packets can be resubmitted (unsequenced are not added to the pending set, thus do not have a packet timeout and do not react to NAKs).

- Completion means the packet gets locked, which prevents it from being resubmitted and retransmitted, and is removed from all collections.

- Completion cannot be triggered by NAK. In case a NAK is received and all tries have been exceeded, the packet still waits for the timeout (or ACK, whatever happens first) one last time.

The error cases:

- serdev: Failure to send via serdev results in immediate completion with error on the transmitter thread. Due to a NAK (or a _very_ unlikely timeout), the packet can be queued for a second time at that point, but, as completion runs on the transmitter thread in this case, the packet is removed from the queue before the thread goes on to get the next packet.

- Timeout: On resubmission, the timeout of a packet is disabled. It is (re-)armed directly before starting transmission on the transmitter thread.

What _could_ now happen is that, for some reason, the transmitter thread waits the full timeout period between arming the timeout and actually sending it (which I'd consider highly unlikely). Note that "actually sending it" includes whatever asynchronous stuff the serdev core does after serdev_device_write_buf() returns.

In that case, the packet can be transmitted _after_ it has received a timeout. So assuming this is the last try, then the packet can be sent after completion with -ETIMEDOUT.

I believe this is what you meant?

Again, I consider this highly unlikely as this would require either the scheduler to pause our thread for a full timeout period or require the serdev core to basically do nothing for a full timeout period. I'd argue this is a lot less likely than a dropped ACK packet from the EC (which results in the same thing: EC receives data but we think it didn't, causing a timeout error).

Note that putting the timeout (re-)arming step after the last call to serdev_device_write_buf() doesn't really help either, as that only writes to the transport buffer. If we would want to guarantee that this doesn't happen, we'd have to explicitly wait for the hardware to finish transmitting, which essentially breaks the whole point of buffering.

Also I prefer to have the timeout cover the transmission part as well. In the very least, this simplifies reasoning about it once I've restricted it to be (re-)set under the pending lock only (as mentioned in the message to Andy).

- Finally, there is completion by cancellation or shutdown, for which the packet can have been (or currently be) transmitted successfully but completion returns with -ECANCELLED or -ESHUTDOWN, respectively. This is the expected behavior in those cases. Again, the completion procedure takes care of locking the packet and removing all references from the collections.

Also there's one success case, when we receive an ACK after a timeout and during the re-transmission. Then the packet is completed with success, but might still be transmitted once more. The EC should detect that packet as re-submission via its packet ID and ignore it.

Quoted 13 lines

drivers/platform/surface/aggregator/ssh_packet_layer.c line 1469

Quoted 10 lines

[...]

drivers/platform/surface/aggregator/ssh_packet_layer.c new file +8 -0
+int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
+{
+	int used;
+
+	if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
+		return -ESHUTDOWN;
+
+	used = kfifo_in(&ptl->rx.fifo, buf, n);

Isn't it possible that n is greater than the free space in the fifo? What happens then?

If I've read the documentation of kfifo_in() right, it takes at most n elements. If n is greater than the free space, it will only take as much elements as it has space. The return value of kfifo_in() is the number of elements actually consumed. We simply return that here (as elements = bytes) and ultimately let the serdev/tty core take care of handling that.

Quoted 5 lines
Quoted 5 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c new file +5 -0
+	if (used)
+		ssh_ptl_rx_wakeup(ptl);
+
+	return used;
+}

[...]

Quoted 5 lines

drivers/platform/surface/aggregator/ssh_packet_layer.h line 126

Quoted 3 lines
drivers/platform/surface/aggregator/ssh_packet_layer.h new file +3 -0
+#define __ssam_prcond(func, p, fmt, ...)		\
+	do {						\
+		if ((p))				\

I believe if (p) is sufficient.

Right.

Quoted 5 lines
Quoted 4 lines
drivers/platform/surface/aggregator/ssh_packet_layer.h new file +2 -0
+			func((p), fmt, ##__VA_ARGS__);	\
+	} while (0)
 
[...]
Quoted 11 lines

drivers/platform/surface/aggregator/ssh_parser.c line 154

Quoted 9 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +8 -0
+int sshp_parse_frame(const struct device *dev, const struct ssam_span *source,
+		     struct ssh_frame **frame, struct ssam_span *payload,
+		     size_t maxlen)
+{
[...]
+	// ensure packet does not exceed maximum length
+	sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
+	if (unlikely(sp.len + SSH_MESSAGE_LENGTH(0) > maxlen)) {
+		dev_warn(dev, "rx: parser: frame too large: %u bytes\n",

I believe %hu would be more appropriate.

Right.

Quoted 3 lines

drivers/platform/surface/aggregator/ssh_parser.c line 155

Quoted 1 line
drivers/platform/surface/aggregator/ssh_parser.c new file +1 -0
+			 ((struct ssh_frame *)sf.ptr)->len);

Why isn't get_unaligned_le16() used here? (Or simply even sp.len.)

This should actually be "sp.len + SSH_MESSAGE_LENGTH(0)", and also format spezifier %zu with that as SSH_MESSAGE_LENGTH returns size_t.

Quoted 6 lines

drivers/platform/surface/aggregator/ssh_parser.c line 174

Quoted 4 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +3 -0
+		return -EMSGSIZE;
+	}
[...]
+	*frame = (struct ssh_frame *)sf.ptr;

This also violates strict aliasing.

Sure, the whole point of this function (and ssh_parse_command below) is to get pointers to the data structures in the given buffer, i.e. create aliases of the right types to the right places in the buffer. Not sure how I'd do that differently, apart from copying.

I think that this would even work properly (in practice) without setting -fno-strict-aliasing, as all data at this point is effectively read-only. Furthermore, after the call access is restricted to the non-aliasing parts respectively (frame and payload), whereas the underlying buffer they do alias isn't accessed for the rest of that scope (i.e. until the other references don't exist any more).

Anyway, as far as I know the kernel is built with -fno-strict-aliasing. Please correct me if I'm wrong or relying on aliasing is frowned upon.

Quoted 19 lines
Quoted 19 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +16 -0
+	*payload = sp;
+
+	dev_dbg(dev, "rx: parser: valid frame found (type: 0x%02x, len: %u)\n",
+		(*frame)->type, (*frame)->len);
+
+	return 0;
+}
 
[...]
 
+int sshp_parse_command(const struct device *dev, const struct ssam_span *source,
+		       struct ssh_command **command,
+		       struct ssam_span *command_data)
+{
+	// check for minimum length
+	if (unlikely(source->len < sizeof(struct ssh_command))) {
+		*command = NULL;
+		command_data->ptr = NULL;
+		command_data->len = 0;
Quoted 8 lines

drivers/platform/surface/aggregator/ssh_parser.c line 221

Quoted 6 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +6 -0
+
+		dev_err(dev, "rx: parser: command payload is too short\n");
+		return -ENOMSG;
+	}
+
+	*command = (struct ssh_command *)source->ptr;

I'm quite sure this also violates strict aliasing.

It does, see above.

Quoted 20 lines
Quoted 20 lines
(quoted diff) +17 -0
+	command_data->ptr = source->ptr + sizeof(struct ssh_command);
+	command_data->len = source->len - sizeof(struct ssh_command);
+
+	dev_dbg(dev, "rx: parser: valid command found (tc: 0x%02x, cid: 0x%02x)\n",
+		(*command)->tc, (*command)->cid);
+
+	return 0;
+}
 
[...]
 
+#define SSH_MSGOFFSET_FRAME(field) \
+	(sizeof(u16) + offsetof(struct ssh_frame, field))
+
+/**
+ * SSH_MSGOFFSET_COMMAND() - Compute offset in SSH message to specified field
+ * in command.
+ * @field: The field for which the offset should be computed.
+ *
+ * Return: Returns the offset of the specified &struct ssh_command field in
Quoted 8 lines

include/linux/surface_aggregator/serial_hub.h line 174

Quoted 5 lines

+ * the raw SSH message data.
+ */

include/linux/surface_aggregator/serial_hub.h new file +3 -0
+#define SSH_MSGOFFSET_COMMAND(field) \
+	(2ull * sizeof(u16) + sizeof(struct ssh_frame) \
+		+ offsetof(struct ssh_command, field))

I believe it should be noted (here and for SSH_MSGOFFSET_FRAME()) why the sizeof(u16)s are necessary.

Right, I can add an explanation for this. One is for the SYN bytes, the other one for the frame CRC (between frame and command).

Quoted 39 lines
Quoted 39 lines
include/linux/surface_aggregator/serial_hub.h new file +36 -0
+
+/*
+ * SSH_MSG_SYN - SSH message synchronization (SYN) bytes as u16.
+ */
+#define SSH_MSG_SYN		((u16)0x55aa)
 
[...]
 
+enum ssam_ssh_tc {
+	/* Known SSH/EC target categories. */
+				// category 0x00 is invalid for EC use
+	SSAM_SSH_TC_SAM = 0x01,	// generic system functionality, real-time clock
+	SSAM_SSH_TC_BAT = 0x02,	// battery/power subsystem
+	SSAM_SSH_TC_TMP = 0x03,	// thermal subsystem
+	SSAM_SSH_TC_PMC = 0x04,
+	SSAM_SSH_TC_FAN = 0x05,
+	SSAM_SSH_TC_PoM = 0x06,
+	SSAM_SSH_TC_DBG = 0x07,
+	SSAM_SSH_TC_KBD = 0x08,	// legacy keyboard (Laptop 1/2)
+	SSAM_SSH_TC_FWU = 0x09,
+	SSAM_SSH_TC_UNI = 0x0a,
+	SSAM_SSH_TC_LPC = 0x0b,
+	SSAM_SSH_TC_TCL = 0x0c,
+	SSAM_SSH_TC_SFL = 0x0d,
+	SSAM_SSH_TC_KIP = 0x0e,
+	SSAM_SSH_TC_EXT = 0x0f,
+	SSAM_SSH_TC_BLD = 0x10,
+	SSAM_SSH_TC_BAS = 0x11,	// detachment system (Surface Book 2/3)
+	SSAM_SSH_TC_SEN = 0x12,
+	SSAM_SSH_TC_SRQ = 0x13,
+	SSAM_SSH_TC_MCU = 0x14,
+	SSAM_SSH_TC_HID = 0x15,	// generic HID input subsystem
+	SSAM_SSH_TC_TCH = 0x16,
+	SSAM_SSH_TC_BKL = 0x17,
+	SSAM_SSH_TC_TAM = 0x18,
+	SSAM_SSH_TC_ACC = 0x19,
+	SSAM_SSH_TC_UFI = 0x1a,
+	SSAM_SSH_TC_USC = 0x1b,
+	SSAM_SSH_TC_PEN = 0x1c,
Quoted 8 lines

include/linux/surface_aggregator/serial_hub.h line 317

Quoted 6 lines
include/linux/surface_aggregator/serial_hub.h new file +6 -0
+	SSAM_SSH_TC_VID = 0x1d,
+	SSAM_SSH_TC_AUD = 0x1e,
+	SSAM_SSH_TC_SMC = 0x1f,
+	SSAM_SSH_TC_KPD = 0x20,
+	SSAM_SSH_TC_REG = 0x21,
+};

Is it known what these abbreviations stand for? Maybe I missed them?

The comments state all we really know about these (through observation and experimentation). The table itself has been extracted from the Windows driver, but the abbreviations and values are all we're getting from it.

I could go ahead and guess for a couple of them but for most of them we haven't really seen any request at all, let alone know what the request does.

For those that we now, the abbreviations should be fairly easy to guess based on the comments, e.g. BAT -> battery, TMP -> temperature. I'm not sure about BAS, which could mean base (as that's the detachment system allowing to separate the top part of the Surface Books from the base).

REG is currently missing a comment because I'm not entirely sure if it's just for registring (enabling/disabling) events or if it also has any other purpose. I'll add a comment for it though, then we have comments on all that are somewhat known.

Quoted 22 lines
Quoted 13 lines

[...]

+/**
+ * struct ssh_request_ops - Callback operations for a SSH request.

+ * @release:  Function called when the request's reference count reaches zero.
+ *            This callback must be relied upon to ensure that the request has
+ *            left the transport systems (both, packet an request systems).
+ * @complete: Function called when the request is completed, either with
+ *            success or failure. The command data for the request response
+ *            is provided via the &struct ssh_command parameter (``cmd``),
+ *            the command payload of the request response via the &struct
+ *            ssh_span parameter (``data``).

+ *

Quoted 8 lines
+ *            If the request does not have any response or has not been
+ *            completed with success, both ``cmd`` and ``data`` parameters will
+ *            be NULL. If the request response does not have any command
+ *            payload, the ``data`` span will be an empty (zero-length) span.
+ *
+ *            In case of failure, the reason for the failure is indicated by
+ *            the value of the provided status code argument (``status``). This
+ *            value will be zero in case of success.
Quoted 2 lines

I believe it should be noted if the status argument is a regular errno, or something different.

Right, I'll add that.

Quoted 9 lines
Quoted 9 lines

+ *
+ * Note that a call to this callback does not guarantee that the
+ * request is not in use by the transport systems any more.
+ */

include/linux/surface_aggregator/serial_hub.h new file +5 -0
+struct ssh_request_ops {
+	void (*release)(struct ssh_request *rqst);
+	void (*complete)(struct ssh_request *rqst,
+			 const struct ssh_command *cmd,
+			 const struct ssam_span *data, int status);
Quoted 8 lines
Quoted 2 lines

+};
[...]

I have to agree, this is quite a sizable patch, although I think it's well-commented, which helps reading the code by a lot, however, in some places I feel like it's a bit over-engineered (or maybe I just cannot fully appreciate the subject at hand at the moment), nonetheless, I applaud your efforts, I can only imagine the hours that must have gone into it.

I think much of what's probably considered over-engineered is the result of reworking the core after that had some reliability issues (dropped packets and timeouts). Specifically, the introduction of dedicated transmitter and receiver threads combined with the need to properly handle NAKs has lead to the current asynchronous system.

I have probably gone a bit too far towards generifying that, but I belive that this should make it easier to adapt for protocol additions or errors in the future.

Other parts, like the RB-tree for event reference counting seem, unfortunately, to be necessary. Events are enabled by registry (specifying which command to use to enable/disable the event) and ID (which event to enable/disable on the registry). So without knowing which registry and ID pairs are valid, we'll have to use some sort of map. Could have been a simple list, but I think an RB-tree makes a bit more sense here.

Thank you for your review!

Regards,
Max

#7 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

Hi

2020. november 19., csütörtök 0:06 keltezéssel, Maximilian Luz <[email protected]> írta:

Quoted 14 lines

[...]

Quoted 11 lines

drivers/platform/surface/aggregator/controller.c line 299

Quoted 8 lines
drivers/platform/surface/aggregator/controller.c new file +8 -0
+/**
+ * ssam_nf_head_destroy() - Deinitialize the given notifier head.
+ * @nh: The notifier head to deinitialize.
+ */
+static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
+{
+	cleanup_srcu_struct(&nh->srcu);
+}

I'm also wondering if there's any reason why these static one-liner functions are not explicitly marked inline.

Quoted 4 lines

Isn't inline more of a linkage keyword at this point? I fully expect any compiler to inline things like this automatically at the first hint of an optimization flag.

I also expect the compiler to inline such functions even without the inline specifier. In retrospect I'm not sure why I actually made this comment, possibly because of my preference, but I believe it's fine either way, so my comment is moot, sorry.

Quoted 22 lines

[...]

Quoted 11 lines
Quoted 11 lines

+ * error values via ssam_notifier_from_errno() to notifier values.
+ *
+ * Also note that any callback that could handle an event should return a value
+ * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
+ * unhandled/ignored. In case no registered callback could handle an event,
+ * this function will emit a warning.
+ *
+ * In case a callback failed, this function will emit an error message.
+ */

drivers/platform/surface/aggregator/controller.c new file +2 -0
+static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
+			 struct ssam_event *event)
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 471

Quoted 6 lines
drivers/platform/surface/aggregator/controller.c new file +6 -0
+{
+	struct ssam_nf_head *nf_head;
+	int status, nf_ret;
+
+	if (!ssh_rqid_is_event(rqid)) {
+		dev_warn(dev, "event: unsupported rqid: 0x%04x\n", rqid);

A small note, "%#04x" would insert the "0x" prefix.

Quoted 2 lines

Oh neat, didn't know that. Should be "%#06x" though, right?

Yes, indeed, sorry, it should be "%#06x".

Quoted 9 lines

[...]

Quoted 6 lines

drivers/platform/surface/aggregator/controller.c line 1008

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+static int ssam_controller_caps_load_from_acpi(
+		acpi_handle handle, struct ssam_controller_caps *caps)
+{
+	u32 d3_closes_handle = false;

Assinging a boolean like this to a u32 looks very odd to me.

Quoted 6 lines

The value returned by the corresponding DSM call is an integer, but essentially only contains a bool value (zero vs. one). I thought using false here explicitly for initialization helps clarify that. That also makes it consistent with the "caps->d3_closes_handle = false" line below.

Although I still find it pretty odd, your explanation makes sense to me.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/platform/surface/aggregator/controller.c new file +11 -0
+int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
+			    struct ssam_request_sync **rqst,
+			    struct ssam_span *buffer)
+{
+	size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
+
+	*rqst = kzalloc(sizeof(**rqst) + msglen, flags);
+	if (!*rqst)
+		return -ENOMEM;
+
+	buffer->ptr = (u8 *)(*rqst + 1);
Quoted 8 lines

drivers/platform/surface/aggregator/controller.c line 1453

Quoted 4 lines
drivers/platform/surface/aggregator/controller.c new file +4 -0
+	buffer->len = msglen;
+
+	return 0;
+}

I think there is a bit of incosistency: sometimes you use ** pointer + return int, sometimes you return a pointer with potentially embedded errno. I think it would be better if you stuck with one or the other.

Quoted 7 lines

My rule of thumb is: If it's one output parameter, use a pointer as return value. If it's two or more output parameters (as in this case, "buffer" is written to), use two pointer arguments and an int as return.

I noticed that ssam_client_bind doesn't follow that scheme so I'll change that.

I see, thanks for the explanation, what prompted me to make this comment was that, as you noted, there were functions with one output parameter that were not consistent.

Quoted 56 lines

[...]

Quoted 11 lines
Quoted 10 lines

+ * Note that the packet completion callback is, in case of success and for a
+ * sequenced packet, guaranteed to run on the receiver thread, thus providing
+ * a way to reliably identify responses to the packet. The packet completion
+ * callback is only run once and it does not indicate that the packet has
+ * fully left the system (for this, one should rely on the release method,
+ * triggered when the reference count of the packet reaches zero). In case of
+ * re-submission (and with somewhat unlikely timing), it may be possible that
+ * the packet is being re-transmitted while the completion callback runs.
+ * Completion will occur both on success and internal error, as well as when
+ * the packet is canceled.

Quoted 6 lines

If I understand it correctly, it is possible that submission of a packet fails for the first time, but it's scheduled for resubmission, and this retransmission happens at the same time when the complete() callback is called. If that's the case, then the callback is called with an error condition, no? Thus it is possible that a packet is successfully submitted (for the second, third, etc. time), but the complete() callback receives notification about failure? Or am I missing something?

Not sure I can follow you here. What do you mean by "fails for the first time"?

A couple of notes before I try to explain the error cases:

- Resubmission only happens on timeout or NAK.

- Only sequenced packets can be resubmitted (unsequenced are not added to the pending set, thus do not have a packet timeout and do not react to NAKs).

- Completion means the packet gets locked, which prevents it from being resubmitted and retransmitted, and is removed from all collections.

- Completion cannot be triggered by NAK. In case a NAK is received and all tries have been exceeded, the packet still waits for the timeout (or ACK, whatever happens first) one last time.

The error cases:

- serdev: Failure to send via serdev results in immediate completion with error on the transmitter thread. Due to a NAK (or a _very_ unlikely timeout), the packet can be queued for a second time at that point, but, as completion runs on the transmitter thread in this case, the packet is removed from the queue before the thread goes on to get the next packet.

- Timeout: On resubmission, the timeout of a packet is disabled. It is (re-)armed directly before starting transmission on the transmitter thread.

What _could_ now happen is that, for some reason, the transmitter thread waits the full timeout period between arming the timeout and actually sending it (which I'd consider highly unlikely). Note that "actually sending it" includes whatever asynchronous stuff the serdev core does after serdev_device_write_buf() returns.

Quoted 6 lines

In that case, the packet can be transmitted _after_ it has received a timeout. So assuming this is the last try, then the packet can be sent after completion with -ETIMEDOUT.

I believe this is what you meant?

Yes, that's what I meant, thanks for the clarification and broader explanation about the things at play here.

Quoted 45 lines

Again, I consider this highly unlikely as this would require either the scheduler to pause our thread for a full timeout period or require the serdev core to basically do nothing for a full timeout period. I'd argue this is a lot less likely than a dropped ACK packet from the EC (which results in the same thing: EC receives data but we think it didn't, causing a timeout error).

Note that putting the timeout (re-)arming step after the last call to serdev_device_write_buf() doesn't really help either, as that only writes to the transport buffer. If we would want to guarantee that this doesn't happen, we'd have to explicitly wait for the hardware to finish transmitting, which essentially breaks the whole point of buffering.

Also I prefer to have the timeout cover the transmission part as well. In the very least, this simplifies reasoning about it once I've restricted it to be (re-)set under the pending lock only (as mentioned in the message to Andy).

- Finally, there is completion by cancellation or shutdown, for which the packet can have been (or currently be) transmitted successfully but completion returns with -ECANCELLED or -ESHUTDOWN, respectively. This is the expected behavior in those cases. Again, the completion procedure takes care of locking the packet and removing all references from the collections.

Also there's one success case, when we receive an ACK after a timeout and during the re-transmission. Then the packet is completed with success, but might still be transmitted once more. The EC should detect that packet as re-submission via its packet ID and ignore it.

Quoted 13 lines

drivers/platform/surface/aggregator/ssh_packet_layer.c line 1469

Quoted 10 lines

[...]

drivers/platform/surface/aggregator/ssh_packet_layer.c new file +8 -0
+int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
+{
+	int used;
+
+	if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
+		return -ESHUTDOWN;
+
+	used = kfifo_in(&ptl->rx.fifo, buf, n);

Isn't it possible that n is greater than the free space in the fifo? What happens then?

Quoted 7 lines

If I've read the documentation of kfifo_in() right, it takes at most n elements. If n is greater than the free space, it will only take as much elements as it has space. The return value of kfifo_in() is the number of elements actually consumed. We simply return that here (as elements = bytes) and ultimately let the serdev/tty core take care of handling that.

Ahh, sorry, you're right.

Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
drivers/platform/surface/aggregator/ssh_packet_layer.c new file +5 -0
+	if (used)
+		ssh_ptl_rx_wakeup(ptl);
+
+	return used;
+}

[...]

Quoted 7 lines
Quoted 3 lines

drivers/platform/surface/aggregator/ssh_parser.c line 155

Quoted 1 line
drivers/platform/surface/aggregator/ssh_parser.c new file +1 -0
+			 ((struct ssh_frame *)sf.ptr)->len);

Why isn't get_unaligned_le16() used here? (Or simply even sp.len.)

This should actually be "sp.len + SSH_MESSAGE_LENGTH(0)", and also format spezifier %zu with that as SSH_MESSAGE_LENGTH returns size_t.

Yes, indeed, sorry, I missed the + SSH_MESSAGE_LENGTH(0) part.

Quoted 19 lines
Quoted 6 lines

drivers/platform/surface/aggregator/ssh_parser.c line 174

Quoted 4 lines
drivers/platform/surface/aggregator/ssh_parser.c new file +3 -0
+		return -EMSGSIZE;
+	}
[...]
+	*frame = (struct ssh_frame *)sf.ptr;

This also violates strict aliasing.

Sure, the whole point of this function (and ssh_parse_command below) is to get pointers to the data structures in the given buffer, i.e. create aliases of the right types to the right places in the buffer. Not sure how I'd do that differently, apart from copying.

I think that this would even work properly (in practice) without setting -fno-strict-aliasing, as all data at this point is effectively read-only. Furthermore, after the call access is restricted to the non-aliasing parts respectively (frame and payload), whereas the underlying buffer they do alias isn't accessed for the rest of that scope (i.e. until the other references don't exist any more).

Quoted 3 lines

Anyway, as far as I know the kernel is built with -fno-strict-aliasing. Please correct me if I'm wrong or relying on aliasing is frowned upon.

I don't believe it could cause issue here, I just wanted to note it.

Quoted 42 lines

[...]

Quoted 31 lines
Quoted 31 lines
include/linux/surface_aggregator/serial_hub.h new file +31 -0
+enum ssam_ssh_tc {
+	/* Known SSH/EC target categories. */
+				// category 0x00 is invalid for EC use
+	SSAM_SSH_TC_SAM = 0x01,	// generic system functionality, real-time clock
+	SSAM_SSH_TC_BAT = 0x02,	// battery/power subsystem
+	SSAM_SSH_TC_TMP = 0x03,	// thermal subsystem
+	SSAM_SSH_TC_PMC = 0x04,
+	SSAM_SSH_TC_FAN = 0x05,
+	SSAM_SSH_TC_PoM = 0x06,
+	SSAM_SSH_TC_DBG = 0x07,
+	SSAM_SSH_TC_KBD = 0x08,	// legacy keyboard (Laptop 1/2)
+	SSAM_SSH_TC_FWU = 0x09,
+	SSAM_SSH_TC_UNI = 0x0a,
+	SSAM_SSH_TC_LPC = 0x0b,
+	SSAM_SSH_TC_TCL = 0x0c,
+	SSAM_SSH_TC_SFL = 0x0d,
+	SSAM_SSH_TC_KIP = 0x0e,
+	SSAM_SSH_TC_EXT = 0x0f,
+	SSAM_SSH_TC_BLD = 0x10,
+	SSAM_SSH_TC_BAS = 0x11,	// detachment system (Surface Book 2/3)
+	SSAM_SSH_TC_SEN = 0x12,
+	SSAM_SSH_TC_SRQ = 0x13,
+	SSAM_SSH_TC_MCU = 0x14,
+	SSAM_SSH_TC_HID = 0x15,	// generic HID input subsystem
+	SSAM_SSH_TC_TCH = 0x16,
+	SSAM_SSH_TC_BKL = 0x17,
+	SSAM_SSH_TC_TAM = 0x18,
+	SSAM_SSH_TC_ACC = 0x19,
+	SSAM_SSH_TC_UFI = 0x1a,
+	SSAM_SSH_TC_USC = 0x1b,
+	SSAM_SSH_TC_PEN = 0x1c,
Quoted 8 lines

include/linux/surface_aggregator/serial_hub.h line 317

Quoted 6 lines
include/linux/surface_aggregator/serial_hub.h new file +6 -0
+	SSAM_SSH_TC_VID = 0x1d,
+	SSAM_SSH_TC_AUD = 0x1e,
+	SSAM_SSH_TC_SMC = 0x1f,
+	SSAM_SSH_TC_KPD = 0x20,
+	SSAM_SSH_TC_REG = 0x21,
+};

Is it known what these abbreviations stand for? Maybe I missed them?

Quoted 4 lines

The comments state all we really know about these (through observation and experimentation). The table itself has been extracted from the Windows driver, but the abbreviations and values are all we're getting from it.

I see, thanks for the clarification. For some reason, I believed the "Known SSH/EC target categories" comment means that those are known, as in, it is known what they are for, etc., not just the abbreviation.

Quoted 1 line

[...]

Regards,
Barnabás Pőcze

author maintainer
#8 lore raw

Re: [PATCH 1/9] platform/surface: Add Surface Aggregator subsystem

Quoted 1 line · Barnabás Pőcze

Hi

[...]

Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Quoted 27 lines
include/linux/surface_aggregator/serial_hub.h new file +27 -0
+enum ssam_ssh_tc {
+	/* Known SSH/EC target categories. */
+				// category 0x00 is invalid for EC use
+	SSAM_SSH_TC_SAM = 0x01,	// generic system functionality, real-time clock
+	SSAM_SSH_TC_BAT = 0x02,	// battery/power subsystem
+	SSAM_SSH_TC_TMP = 0x03,	// thermal subsystem
+	SSAM_SSH_TC_PMC = 0x04,
+	SSAM_SSH_TC_FAN = 0x05,
+	SSAM_SSH_TC_PoM = 0x06,
+	SSAM_SSH_TC_DBG = 0x07,
+	SSAM_SSH_TC_KBD = 0x08,	// legacy keyboard (Laptop 1/2)
+	SSAM_SSH_TC_FWU = 0x09,
+	SSAM_SSH_TC_UNI = 0x0a,
+	SSAM_SSH_TC_LPC = 0x0b,
+	SSAM_SSH_TC_TCL = 0x0c,
+	SSAM_SSH_TC_SFL = 0x0d,
+	SSAM_SSH_TC_KIP = 0x0e,
+	SSAM_SSH_TC_EXT = 0x0f,
+	SSAM_SSH_TC_BLD = 0x10,
+	SSAM_SSH_TC_BAS = 0x11,	// detachment system (Surface Book 2/3)
+	SSAM_SSH_TC_SEN = 0x12,
+	SSAM_SSH_TC_SRQ = 0x13,
+	SSAM_SSH_TC_MCU = 0x14,
+	SSAM_SSH_TC_HID = 0x15,	// generic HID input subsystem
+	SSAM_SSH_TC_TCH = 0x16,
+	SSAM_SSH_TC_BKL = 0x17,
+	SSAM_SSH_TC_TAM = 0x18,

include/linux/surface_aggregator/serial_hub.h line 317

Quoted 10 lines
include/linux/surface_aggregator/serial_hub.h new file +10 -0
+	SSAM_SSH_TC_ACC = 0x19,
+	SSAM_SSH_TC_UFI = 0x1a,
+	SSAM_SSH_TC_USC = 0x1b,
+	SSAM_SSH_TC_PEN = 0x1c,
+	SSAM_SSH_TC_VID = 0x1d,
+	SSAM_SSH_TC_AUD = 0x1e,
+	SSAM_SSH_TC_SMC = 0x1f,
+	SSAM_SSH_TC_KPD = 0x20,
+	SSAM_SSH_TC_REG = 0x21,
+};

Is it known what these abbreviations stand for? Maybe I missed them?

Quoted 8 lines
Quoted 4 lines

The comments state all we really know about these (through observation and experimentation). The table itself has been extracted from the Windows driver, but the abbreviations and values are all we're getting from it.

I see, thanks for the clarification. For some reason, I believed the "Known SSH/EC target categories" comment means that those are known, as in, it is known what they are for, etc., not just the abbreviation.

Right, that can be misunderstood, sorry. It's probably best if I add a comment explaining that in a bit more detail and noting where those values and abbreviations come from.

Thanks,
Max

#23 lore raw
In reply to Maximilian Luz

Hi,

Quoted 17 lines · Maximilian Luz

Hello,

N.B.: the following text is mostly a repeat of cover letter from the previous RFC for the uninitiated, which can be found at

https://lore.kernel.org/linux-serial/[email protected]/

See "Changes" below for an overview of differences between the RFC and this patchset. I hope I have addressed all comments from that in this version, thank you again for those.

The Surface System Aggregator Module (we'll refer to it as Surface Aggregator or SAM below) is an embedded controller (EC) found on various Microsoft Surface devices. Specifically, all 4th and later generation Surface devices, i.e. Surface Pro 4, Surface Book 1 and later, with the exception of the Surface Go series and the Surface Duo. Notably, it seems like this EC can also be found on the ARM-based Surface Pro X [1].

<snip>

Quoted 8 lines

This patch-set can also be found at the following repository and reference, if you prefer to look at a kernel tree instead of these emails:

https://github.com/linux-surface/kernel tags/s/surface-aggregator/v1

Thanks,
Max

Thank you for your work on this. It would be great if we can get better support for the Surface line in the mainline kernel.

Since a lot of people have already commented on this series I think that you have enough feedback to do a v2 addressing that feedback right?

For now I'm going to assume that you will do a v2 addressing the initial round of comments and not review this myself (IOW I'll review this when v2 is posted).

Let me know if you see things differently.

Regards,

Hans

Quoted 122 lines

[1]: The Surface Pro X is, however, currently considered unsupported due
to a lack of test candidates and, as it seems, general lack of Linux support on other parts. AFAIK there is an issue preventing serial devices from being registered, on which the core driver in this series is build on, thus there is no way to even test that at this point. I'd be happy to work out any issues regarding SAM on the Pro X at some point in the future, provided someone can/wants to actually test it.

[2]: https://github.com/linux-surface/surface-aggregator-module
[3]: https://github.com/linux-surface/linux-surface

Note: This patch depends on

[PATCH v4] platform/surface: Create a platform subdirectory for
Microsoft Surface devices

which can be found at

https://lore.kernel.org/platform-driver-x86/[email protected]/

and is currently in platform-drivers-x86/for-next.

Changes from the previous RFC (overview):
- move to platform/surface
- add copyright lines
- change SPDX identifier to GPL-2.0+ (was GPL-2.0-or-later)
- change user-space interface from debugfs to misc-device
- address issues in user-space interface
- fix typos in commit messages and documentation
- fix some bugs, address other issues

Changes regarding specific patches (and more details) can be found on the individual patch.

Maximilian Luz (9):
platform/surface: Add Surface Aggregator subsystem platform/surface: aggregator: Add control packet allocation caching platform/surface: aggregator: Add event item allocation caching platform/surface: aggregator: Add trace points platform/surface: aggregator: Add error injection capabilities platform/surface: aggregator: Add dedicated bus and device type
docs: driver-api: Add Surface Aggregator subsystem documentation platform/surface: Add Surface Aggregator user-space interface platform/surface: Add Surface ACPI Notify driver

 Documentation/driver-api/index.rst            |    1 +
 .../surface_aggregator/client-api.rst         |   38 +
 .../driver-api/surface_aggregator/client.rst  |  394 +++
 .../surface_aggregator/clients/cdev.rst       |   85 +
 .../surface_aggregator/clients/index.rst      |   21 +
 .../surface_aggregator/clients/san.rst        |   44 +
 .../driver-api/surface_aggregator/index.rst   |   21 +
 .../surface_aggregator/internal-api.rst       |   67 +
 .../surface_aggregator/internal.rst           |   50 +
 .../surface_aggregator/overview.rst           |   76 +
 .../driver-api/surface_aggregator/ssh.rst     |  343 +++
 MAINTAINERS                                   |   13 +
 drivers/platform/surface/Kconfig              |   39 +
 drivers/platform/surface/Makefile             |    3 +
 drivers/platform/surface/aggregator/Kconfig   |   65 +
 drivers/platform/surface/aggregator/Makefile  |   17 +
 drivers/platform/surface/aggregator/bus.c     |  424 +++
 drivers/platform/surface/aggregator/bus.h     |   27 +
 .../platform/surface/aggregator/controller.c  | 2557 +++++++++++++++++
 .../platform/surface/aggregator/controller.h  |  288 ++
 drivers/platform/surface/aggregator/core.c    |  831 ++++++
 .../platform/surface/aggregator/ssh_msgb.h    |  201 ++
 .../surface/aggregator/ssh_packet_layer.c     | 2009 +++++++++++++
 .../surface/aggregator/ssh_packet_layer.h     |  175 ++
 .../platform/surface/aggregator/ssh_parser.c  |  229 ++
 .../platform/surface/aggregator/ssh_parser.h  |  157 +
 .../surface/aggregator/ssh_request_layer.c    | 1254 ++++++++
 .../surface/aggregator/ssh_request_layer.h    |  142 +
 drivers/platform/surface/aggregator/trace.h   |  625 ++++
 .../platform/surface/surface_acpi_notify.c    |  884 ++++++
 .../surface/surface_aggregator_cdev.c         |  299 ++
 include/linux/mod_devicetable.h               |   18 +
 include/linux/surface_acpi_notify.h           |   39 +
 include/linux/surface_aggregator/controller.h |  832 ++++++
 include/linux/surface_aggregator/device.h     |  430 +++
 include/linux/surface_aggregator/serial_hub.h |  659 +++++
 include/uapi/linux/surface_aggregator/cdev.h  |   58 +
 scripts/mod/devicetable-offsets.c             |    8 +
 scripts/mod/file2alias.c                      |   23 +
 39 files changed, 13446 insertions(+)
 create mode 100644 Documentation/driver-api/surface_aggregator/client-api.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/client.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/cdev.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/index.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/clients/san.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/index.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/internal-api.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/internal.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/overview.rst
 create mode 100644 Documentation/driver-api/surface_aggregator/ssh.rst
 create mode 100644 drivers/platform/surface/aggregator/Kconfig
 create mode 100644 drivers/platform/surface/aggregator/Makefile
 create mode 100644 drivers/platform/surface/aggregator/bus.c
 create mode 100644 drivers/platform/surface/aggregator/bus.h
 create mode 100644 drivers/platform/surface/aggregator/controller.c
 create mode 100644 drivers/platform/surface/aggregator/controller.h
 create mode 100644 drivers/platform/surface/aggregator/core.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_msgb.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_packet_layer.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_parser.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_parser.h
 create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.c
 create mode 100644 drivers/platform/surface/aggregator/ssh_request_layer.h
 create mode 100644 drivers/platform/surface/aggregator/trace.h
 create mode 100644 drivers/platform/surface/surface_acpi_notify.c
 create mode 100644 drivers/platform/surface/surface_aggregator_cdev.c
 create mode 100644 include/linux/surface_acpi_notify.h
 create mode 100644 include/linux/surface_aggregator/controller.h
 create mode 100644 include/linux/surface_aggregator/device.h
 create mode 100644 include/linux/surface_aggregator/serial_hub.h
 create mode 100644 include/uapi/linux/surface_aggregator/cdev.h
author maintainer
#24 lore raw
Quoted 29 lines · Hans de Goede

Hi,

Quoted 17 lines · Maximilian Luz

Hello,

N.B.: the following text is mostly a repeat of cover letter from the previous RFC for the uninitiated, which can be found at

https://lore.kernel.org/linux-serial/[email protected]/

See "Changes" below for an overview of differences between the RFC and this patchset. I hope I have addressed all comments from that in this version, thank you again for those.

The Surface System Aggregator Module (we'll refer to it as Surface Aggregator or SAM below) is an embedded controller (EC) found on various Microsoft Surface devices. Specifically, all 4th and later generation Surface devices, i.e. Surface Pro 4, Surface Book 1 and later, with the exception of the Surface Go series and the Surface Duo. Notably, it seems like this EC can also be found on the ARM-based Surface Pro X [1].

<snip>

Quoted 6 lines

This patch-set can also be found at the following repository and reference, if you prefer to look at a kernel tree instead of these emails:

https://github.com/linux-surface/kernel tags/s/surface-aggregator/v1

Quoted 8 lines
Quoted 2 lines

Thanks,
Max

Thank you for your work on this. It would be great if we can get better support for the Surface line in the mainline kernel.

Since a lot of people have already commented on this series I think that you have enough feedback to do a v2 addressing that feedback right?

Yes, I'm already working on it.

Quoted 3 lines

For now I'm going to assume that you will do a v2 addressing the initial round of comments and not review this myself (IOW I'll review this when v2 is posted).

Sure, no need for you to review v1 at this point.

Quoted 1 line

Let me know if you see things differently.

Thanks,
Max