aboutsummaryrefslogtreecommitdiff
path: root/drivers/thunderbolt/dma_test.c
diff options
context:
space:
mode:
authorLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
committerLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
commit5b7c4cabbb65f5c469464da6c5f614cbd7f730f2 (patch)
treecc5c2d0a898769fd59549594fedb3ee6f84e59a0 /drivers/thunderbolt/dma_test.c
downloadlinux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.tar.gz
linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.zip
Merge tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-nextgrafted
Pull networking updates from Jakub Kicinski: "Core: - Add dedicated kmem_cache for typical/small skb->head, avoid having to access struct page at kfree time, and improve memory use. - Introduce sysctl to set default RPS configuration for new netdevs. - Define Netlink protocol specification format which can be used to describe messages used by each family and auto-generate parsers. Add tools for generating kernel data structures and uAPI headers. - Expose all net/core sysctls inside netns. - Remove 4s sleep in netpoll if carrier is instantly detected on boot. - Add configurable limit of MDB entries per port, and port-vlan. - Continue populating drop reasons throughout the stack. - Retire a handful of legacy Qdiscs and classifiers. Protocols: - Support IPv4 big TCP (TSO frames larger than 64kB). - Add IP_LOCAL_PORT_RANGE socket option, to control local port range on socket by socket basis. - Track and report in procfs number of MPTCP sockets used. - Support mixing IPv4 and IPv6 flows in the in-kernel MPTCP path manager. - IPv6: don't check net.ipv6.route.max_size and rely on garbage collection to free memory (similarly to IPv4). - Support Penultimate Segment Pop (PSP) flavor in SRv6 (RFC8986). - ICMP: add per-rate limit counters. - Add support for user scanning requests in ieee802154. - Remove static WEP support. - Support minimal Wi-Fi 7 Extremely High Throughput (EHT) rate reporting. - WiFi 7 EHT channel puncturing support (client & AP). BPF: - Add a rbtree data structure following the "next-gen data structure" precedent set by recently added linked list, that is, by using kfunc + kptr instead of adding a new BPF map type. - Expose XDP hints via kfuncs with initial support for RX hash and timestamp metadata. - Add BPF_F_NO_TUNNEL_KEY extension to bpf_skb_set_tunnel_key to better support decap on GRE tunnel devices not operating in collect metadata. - Improve x86 JIT's codegen for PROBE_MEM runtime error checks. - Remove the need for trace_printk_lock for bpf_trace_printk and bpf_trace_vprintk helpers. - Extend libbpf's bpf_tracing.h support for tracing arguments of kprobes/uprobes and syscall as a special case. - Significantly reduce the search time for module symbols by livepatch and BPF. - Enable cpumasks to be used as kptrs, which is useful for tracing programs tracking which tasks end up running on which CPUs in different time intervals. - Add support for BPF trampoline on s390x and riscv64. - Add capability to export the XDP features supported by the NIC. - Add __bpf_kfunc tag for marking kernel functions as kfuncs. - Add cgroup.memory=nobpf kernel parameter option to disable BPF memory accounting for container environments. Netfilter: - Remove the CLUSTERIP target. It has been marked as obsolete for years, and we still have WARN splats wrt races of the out-of-band /proc interface installed by this target. - Add 'destroy' commands to nf_tables. They are identical to the existing 'delete' commands, but do not return an error if the referenced object (set, chain, rule...) did not exist. Driver API: - Improve cpumask_local_spread() locality to help NICs set the right IRQ affinity on AMD platforms. - Separate C22 and C45 MDIO bus transactions more clearly. - Introduce new DCB table to control DSCP rewrite on egress. - Support configuration of Physical Layer Collision Avoidance (PLCA) Reconciliation Sublayer (RS) (802.3cg-2019). Modern version of shared medium Ethernet. - Support for MAC Merge layer (IEEE 802.3-2018 clause 99). Allowing preemption of low priority frames by high priority frames. - Add support for controlling MACSec offload using netlink SET. - Rework devlink instance refcounts to allow registration and de-registration under the instance lock. Split the code into multiple files, drop some of the unnecessarily granular locks and factor out common parts of netlink operation handling. - Add TX frame aggregation parameters (for USB drivers). - Add a new attr TCA_EXT_WARN_MSG to report TC (offload) warning messages with notifications for debug. - Allow offloading of UDP NEW connections via act_ct. - Add support for per action HW stats in TC. - Support hardware miss to TC action (continue processing in SW from a specific point in the action chain). - Warn if old Wireless Extension user space interface is used with modern cfg80211/mac80211 drivers. Do not support Wireless Extensions for Wi-Fi 7 devices at all. Everyone should switch to using nl80211 interface instead. - Improve the CAN bit timing configuration. Use extack to return error messages directly to user space, update the SJW handling, including the definition of a new default value that will benefit CAN-FD controllers, by increasing their oscillator tolerance. New hardware / drivers: - Ethernet: - nVidia BlueField-3 support (control traffic driver) - Ethernet support for imx93 SoCs - Motorcomm yt8531 gigabit Ethernet PHY - onsemi NCN26000 10BASE-T1S PHY (with support for PLCA) - Microchip LAN8841 PHY (incl. cable diagnostics and PTP) - Amlogic gxl MDIO mux - WiFi: - RealTek RTL8188EU (rtl8xxxu) - Qualcomm Wi-Fi 7 devices (ath12k) - CAN: - Renesas R-Car V4H Drivers: - Bluetooth: - Set Per Platform Antenna Gain (PPAG) for Intel controllers. - Ethernet NICs: - Intel (1G, igc): - support TSN / Qbv / packet scheduling features of i226 model - Intel (100G, ice): - use GNSS subsystem instead of TTY - multi-buffer XDP support - extend support for GPIO pins to E823 devices - nVidia/Mellanox: - update the shared buffer configuration on PFC commands - implement PTP adjphase function for HW offset control - TC support for Geneve and GRE with VF tunnel offload - more efficient crypto key management method - multi-port eswitch support - Netronome/Corigine: - add DCB IEEE support - support IPsec offloading for NFP3800 - Freescale/NXP (enetc): - support XDP_REDIRECT for XDP non-linear buffers - improve reconfig, avoid link flap and waiting for idle - support MAC Merge layer - Other NICs: - sfc/ef100: add basic devlink support for ef100 - ionic: rx_push mode operation (writing descriptors via MMIO) - bnxt: use the auxiliary bus abstraction for RDMA - r8169: disable ASPM and reset bus in case of tx timeout - cpsw: support QSGMII mode for J721e CPSW9G - cpts: support pulse-per-second output - ngbe: add an mdio bus driver - usbnet: optimize usbnet_bh() by avoiding unnecessary queuing - r8152: handle devices with FW with NCM support - amd-xgbe: support 10Mbps, 2.5GbE speeds and rx-adaptation - virtio-net: support multi buffer XDP - virtio/vsock: replace virtio_vsock_pkt with sk_buff - tsnep: XDP support - Ethernet high-speed switches: - nVidia/Mellanox (mlxsw): - add support for latency TLV (in FW control messages) - Microchip (sparx5): - separate explicit and implicit traffic forwarding rules, make the implicit rules always active - add support for egress DSCP rewrite - IS0 VCAP support (Ingress Classification) - IS2 VCAP filters (protos, L3 addrs, L4 ports, flags, ToS etc.) - ES2 VCAP support (Egress Access Control) - support for Per-Stream Filtering and Policing (802.1Q, 8.6.5.1) - Ethernet embedded switches: - Marvell (mv88e6xxx): - add MAB (port auth) offload support - enable PTP receive for mv88e6390 - NXP (ocelot): - support MAC Merge layer - support for the the vsc7512 internal copper phys - Microchip: - lan9303: convert to PHYLINK - lan966x: support TC flower filter statistics - lan937x: PTP support for KSZ9563/KSZ8563 and LAN937x - lan937x: support Credit Based Shaper configuration - ksz9477: support Energy Efficient Ethernet - other: - qca8k: convert to regmap read/write API, use bulk operations - rswitch: Improve TX timestamp accuracy - Intel WiFi (iwlwifi): - EHT (Wi-Fi 7) rate reporting - STEP equalizer support: transfer some STEP (connection to radio on platforms with integrated wifi) related parameters from the BIOS to the firmware. - Qualcomm 802.11ax WiFi (ath11k): - IPQ5018 support - Fine Timing Measurement (FTM) responder role support - channel 177 support - MediaTek WiFi (mt76): - per-PHY LED support - mt7996: EHT (Wi-Fi 7) support - Wireless Ethernet Dispatch (WED) reset support - switch to using page pool allocator - RealTek WiFi (rtw89): - support new version of Bluetooth co-existance - Mobile: - rmnet: support TX aggregation" * tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (1872 commits) page_pool: add a comment explaining the fragment counter usage net: ethtool: fix __ethtool_dev_mm_supported() implementation ethtool: pse-pd: Fix double word in comments xsk: add linux/vmalloc.h to xsk.c sefltests: netdevsim: wait for devlink instance after netns removal selftest: fib_tests: Always cleanup before exit net/mlx5e: Align IPsec ASO result memory to be as required by hardware net/mlx5e: TC, Set CT miss to the specific ct action instance net/mlx5e: Rename CHAIN_TO_REG to MAPPED_OBJ_TO_REG net/mlx5: Refactor tc miss handling to a single function net/mlx5: Kconfig: Make tc offload depend on tc skb extension net/sched: flower: Support hardware miss to tc action net/sched: flower: Move filter handle initialization earlier net/sched: cls_api: Support hardware miss to tc action net/sched: Rename user cookie and act cookie sfc: fix builds without CONFIG_RTC_LIB sfc: clean up some inconsistent indentings net/mlx4_en: Introduce flexible array to silence overflow warning net: lan966x: Fix possible deadlock inside PTP net/ulp: Remove redundant ->clone() test in inet_clone_ulp(). ...
Diffstat (limited to 'drivers/thunderbolt/dma_test.c')
-rw-r--r--drivers/thunderbolt/dma_test.c760
1 files changed, 760 insertions, 0 deletions
diff --git a/drivers/thunderbolt/dma_test.c b/drivers/thunderbolt/dma_test.c
new file mode 100644
index 000000000..3bedecb23
--- /dev/null
+++ b/drivers/thunderbolt/dma_test.c
@@ -0,0 +1,760 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DMA traffic test driver
+ *
+ * Copyright (C) 2020, Intel Corporation
+ * Authors: Isaac Hazan <isaac.hazan@intel.com>
+ * Mika Westerberg <mika.westerberg@linux.intel.com>
+ */
+
+#include <linux/completion.h>
+#include <linux/debugfs.h>
+#include <linux/module.h>
+#include <linux/sizes.h>
+#include <linux/thunderbolt.h>
+
+#define DMA_TEST_TX_RING_SIZE 64
+#define DMA_TEST_RX_RING_SIZE 256
+#define DMA_TEST_FRAME_SIZE SZ_4K
+#define DMA_TEST_DATA_PATTERN 0x0123456789abcdefLL
+#define DMA_TEST_MAX_PACKETS 1000
+
+enum dma_test_frame_pdf {
+ DMA_TEST_PDF_FRAME_START = 1,
+ DMA_TEST_PDF_FRAME_END,
+};
+
+struct dma_test_frame {
+ struct dma_test *dma_test;
+ void *data;
+ struct ring_frame frame;
+};
+
+enum dma_test_test_error {
+ DMA_TEST_NO_ERROR,
+ DMA_TEST_INTERRUPTED,
+ DMA_TEST_BUFFER_ERROR,
+ DMA_TEST_DMA_ERROR,
+ DMA_TEST_CONFIG_ERROR,
+ DMA_TEST_SPEED_ERROR,
+ DMA_TEST_WIDTH_ERROR,
+ DMA_TEST_BONDING_ERROR,
+ DMA_TEST_PACKET_ERROR,
+};
+
+static const char * const dma_test_error_names[] = {
+ [DMA_TEST_NO_ERROR] = "no errors",
+ [DMA_TEST_INTERRUPTED] = "interrupted by signal",
+ [DMA_TEST_BUFFER_ERROR] = "no memory for packet buffers",
+ [DMA_TEST_DMA_ERROR] = "DMA ring setup failed",
+ [DMA_TEST_CONFIG_ERROR] = "configuration is not valid",
+ [DMA_TEST_SPEED_ERROR] = "unexpected link speed",
+ [DMA_TEST_WIDTH_ERROR] = "unexpected link width",
+ [DMA_TEST_BONDING_ERROR] = "lane bonding configuration error",
+ [DMA_TEST_PACKET_ERROR] = "packet check failed",
+};
+
+enum dma_test_result {
+ DMA_TEST_NOT_RUN,
+ DMA_TEST_SUCCESS,
+ DMA_TEST_FAIL,
+};
+
+static const char * const dma_test_result_names[] = {
+ [DMA_TEST_NOT_RUN] = "not run",
+ [DMA_TEST_SUCCESS] = "success",
+ [DMA_TEST_FAIL] = "failed",
+};
+
+/**
+ * struct dma_test - DMA test device driver private data
+ * @svc: XDomain service the driver is bound to
+ * @xd: XDomain the service belongs to
+ * @rx_ring: Software ring holding RX frames
+ * @rx_hopid: HopID used for receiving frames
+ * @tx_ring: Software ring holding TX frames
+ * @tx_hopid: HopID used for sending fames
+ * @packets_to_send: Number of packets to send
+ * @packets_to_receive: Number of packets to receive
+ * @packets_sent: Actual number of packets sent
+ * @packets_received: Actual number of packets received
+ * @link_speed: Expected link speed (Gb/s), %0 to use whatever is negotiated
+ * @link_width: Expected link width (Gb/s), %0 to use whatever is negotiated
+ * @crc_errors: Number of CRC errors during the test run
+ * @buffer_overflow_errors: Number of buffer overflow errors during the test
+ * run
+ * @result: Result of the last run
+ * @error_code: Error code of the last run
+ * @complete: Used to wait for the Rx to complete
+ * @lock: Lock serializing access to this structure
+ * @debugfs_dir: dentry of this dma_test
+ */
+struct dma_test {
+ const struct tb_service *svc;
+ struct tb_xdomain *xd;
+ struct tb_ring *rx_ring;
+ int rx_hopid;
+ struct tb_ring *tx_ring;
+ int tx_hopid;
+ unsigned int packets_to_send;
+ unsigned int packets_to_receive;
+ unsigned int packets_sent;
+ unsigned int packets_received;
+ unsigned int link_speed;
+ unsigned int link_width;
+ unsigned int crc_errors;
+ unsigned int buffer_overflow_errors;
+ enum dma_test_result result;
+ enum dma_test_test_error error_code;
+ struct completion complete;
+ struct mutex lock;
+ struct dentry *debugfs_dir;
+};
+
+/* DMA test property directory UUID: 3188cd10-6523-4a5a-a682-fdca07a248d8 */
+static const uuid_t dma_test_dir_uuid =
+ UUID_INIT(0x3188cd10, 0x6523, 0x4a5a,
+ 0xa6, 0x82, 0xfd, 0xca, 0x07, 0xa2, 0x48, 0xd8);
+
+static struct tb_property_dir *dma_test_dir;
+static void *dma_test_pattern;
+
+static void dma_test_free_rings(struct dma_test *dt)
+{
+ if (dt->rx_ring) {
+ tb_xdomain_release_in_hopid(dt->xd, dt->rx_hopid);
+ tb_ring_free(dt->rx_ring);
+ dt->rx_ring = NULL;
+ }
+ if (dt->tx_ring) {
+ tb_xdomain_release_out_hopid(dt->xd, dt->tx_hopid);
+ tb_ring_free(dt->tx_ring);
+ dt->tx_ring = NULL;
+ }
+}
+
+static int dma_test_start_rings(struct dma_test *dt)
+{
+ unsigned int flags = RING_FLAG_FRAME;
+ struct tb_xdomain *xd = dt->xd;
+ int ret, e2e_tx_hop = 0;
+ struct tb_ring *ring;
+
+ /*
+ * If we are both sender and receiver (traffic goes over a
+ * special loopback dongle) enable E2E flow control. This avoids
+ * losing packets.
+ */
+ if (dt->packets_to_send && dt->packets_to_receive)
+ flags |= RING_FLAG_E2E;
+
+ if (dt->packets_to_send) {
+ ring = tb_ring_alloc_tx(xd->tb->nhi, -1, DMA_TEST_TX_RING_SIZE,
+ flags);
+ if (!ring)
+ return -ENOMEM;
+
+ dt->tx_ring = ring;
+ e2e_tx_hop = ring->hop;
+
+ ret = tb_xdomain_alloc_out_hopid(xd, -1);
+ if (ret < 0) {
+ dma_test_free_rings(dt);
+ return ret;
+ }
+
+ dt->tx_hopid = ret;
+ }
+
+ if (dt->packets_to_receive) {
+ u16 sof_mask, eof_mask;
+
+ sof_mask = BIT(DMA_TEST_PDF_FRAME_START);
+ eof_mask = BIT(DMA_TEST_PDF_FRAME_END);
+
+ ring = tb_ring_alloc_rx(xd->tb->nhi, -1, DMA_TEST_RX_RING_SIZE,
+ flags, e2e_tx_hop, sof_mask, eof_mask,
+ NULL, NULL);
+ if (!ring) {
+ dma_test_free_rings(dt);
+ return -ENOMEM;
+ }
+
+ dt->rx_ring = ring;
+
+ ret = tb_xdomain_alloc_in_hopid(xd, -1);
+ if (ret < 0) {
+ dma_test_free_rings(dt);
+ return ret;
+ }
+
+ dt->rx_hopid = ret;
+ }
+
+ ret = tb_xdomain_enable_paths(dt->xd, dt->tx_hopid,
+ dt->tx_ring ? dt->tx_ring->hop : 0,
+ dt->rx_hopid,
+ dt->rx_ring ? dt->rx_ring->hop : 0);
+ if (ret) {
+ dma_test_free_rings(dt);
+ return ret;
+ }
+
+ if (dt->tx_ring)
+ tb_ring_start(dt->tx_ring);
+ if (dt->rx_ring)
+ tb_ring_start(dt->rx_ring);
+
+ return 0;
+}
+
+static void dma_test_stop_rings(struct dma_test *dt)
+{
+ int ret;
+
+ if (dt->rx_ring)
+ tb_ring_stop(dt->rx_ring);
+ if (dt->tx_ring)
+ tb_ring_stop(dt->tx_ring);
+
+ ret = tb_xdomain_disable_paths(dt->xd, dt->tx_hopid,
+ dt->tx_ring ? dt->tx_ring->hop : 0,
+ dt->rx_hopid,
+ dt->rx_ring ? dt->rx_ring->hop : 0);
+ if (ret)
+ dev_warn(&dt->svc->dev, "failed to disable DMA paths\n");
+
+ dma_test_free_rings(dt);
+}
+
+static void dma_test_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
+ bool canceled)
+{
+ struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
+ struct dma_test *dt = tf->dma_test;
+ struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
+
+ dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
+ DMA_FROM_DEVICE);
+ kfree(tf->data);
+
+ if (canceled) {
+ kfree(tf);
+ return;
+ }
+
+ dt->packets_received++;
+ dev_dbg(&dt->svc->dev, "packet %u/%u received\n", dt->packets_received,
+ dt->packets_to_receive);
+
+ if (tf->frame.flags & RING_DESC_CRC_ERROR)
+ dt->crc_errors++;
+ if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN)
+ dt->buffer_overflow_errors++;
+
+ kfree(tf);
+
+ if (dt->packets_received == dt->packets_to_receive)
+ complete(&dt->complete);
+}
+
+static int dma_test_submit_rx(struct dma_test *dt, size_t npackets)
+{
+ struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
+ int i;
+
+ for (i = 0; i < npackets; i++) {
+ struct dma_test_frame *tf;
+ dma_addr_t dma_addr;
+
+ tf = kzalloc(sizeof(*tf), GFP_KERNEL);
+ if (!tf)
+ return -ENOMEM;
+
+ tf->data = kzalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
+ if (!tf->data) {
+ kfree(tf);
+ return -ENOMEM;
+ }
+
+ dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
+ DMA_FROM_DEVICE);
+ if (dma_mapping_error(dma_dev, dma_addr)) {
+ kfree(tf->data);
+ kfree(tf);
+ return -ENOMEM;
+ }
+
+ tf->frame.buffer_phy = dma_addr;
+ tf->frame.callback = dma_test_rx_callback;
+ tf->dma_test = dt;
+ INIT_LIST_HEAD(&tf->frame.list);
+
+ tb_ring_rx(dt->rx_ring, &tf->frame);
+ }
+
+ return 0;
+}
+
+static void dma_test_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
+ bool canceled)
+{
+ struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
+ struct dma_test *dt = tf->dma_test;
+ struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
+
+ dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
+ DMA_TO_DEVICE);
+ kfree(tf->data);
+ kfree(tf);
+}
+
+static int dma_test_submit_tx(struct dma_test *dt, size_t npackets)
+{
+ struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
+ int i;
+
+ for (i = 0; i < npackets; i++) {
+ struct dma_test_frame *tf;
+ dma_addr_t dma_addr;
+
+ tf = kzalloc(sizeof(*tf), GFP_KERNEL);
+ if (!tf)
+ return -ENOMEM;
+
+ tf->frame.size = 0; /* means 4096 */
+ tf->dma_test = dt;
+
+ tf->data = kmemdup(dma_test_pattern, DMA_TEST_FRAME_SIZE, GFP_KERNEL);
+ if (!tf->data) {
+ kfree(tf);
+ return -ENOMEM;
+ }
+
+ dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
+ DMA_TO_DEVICE);
+ if (dma_mapping_error(dma_dev, dma_addr)) {
+ kfree(tf->data);
+ kfree(tf);
+ return -ENOMEM;
+ }
+
+ tf->frame.buffer_phy = dma_addr;
+ tf->frame.callback = dma_test_tx_callback;
+ tf->frame.sof = DMA_TEST_PDF_FRAME_START;
+ tf->frame.eof = DMA_TEST_PDF_FRAME_END;
+ INIT_LIST_HEAD(&tf->frame.list);
+
+ dt->packets_sent++;
+ dev_dbg(&dt->svc->dev, "packet %u/%u sent\n", dt->packets_sent,
+ dt->packets_to_send);
+
+ tb_ring_tx(dt->tx_ring, &tf->frame);
+ }
+
+ return 0;
+}
+
+#define DMA_TEST_DEBUGFS_ATTR(__fops, __get, __validate, __set) \
+static int __fops ## _show(void *data, u64 *val) \
+{ \
+ struct tb_service *svc = data; \
+ struct dma_test *dt = tb_service_get_drvdata(svc); \
+ int ret; \
+ \
+ ret = mutex_lock_interruptible(&dt->lock); \
+ if (ret) \
+ return ret; \
+ __get(dt, val); \
+ mutex_unlock(&dt->lock); \
+ return 0; \
+} \
+static int __fops ## _store(void *data, u64 val) \
+{ \
+ struct tb_service *svc = data; \
+ struct dma_test *dt = tb_service_get_drvdata(svc); \
+ int ret; \
+ \
+ ret = __validate(val); \
+ if (ret) \
+ return ret; \
+ ret = mutex_lock_interruptible(&dt->lock); \
+ if (ret) \
+ return ret; \
+ __set(dt, val); \
+ mutex_unlock(&dt->lock); \
+ return 0; \
+} \
+DEFINE_DEBUGFS_ATTRIBUTE(__fops ## _fops, __fops ## _show, \
+ __fops ## _store, "%llu\n")
+
+static void lanes_get(const struct dma_test *dt, u64 *val)
+{
+ *val = dt->link_width;
+}
+
+static int lanes_validate(u64 val)
+{
+ return val > 2 ? -EINVAL : 0;
+}
+
+static void lanes_set(struct dma_test *dt, u64 val)
+{
+ dt->link_width = val;
+}
+DMA_TEST_DEBUGFS_ATTR(lanes, lanes_get, lanes_validate, lanes_set);
+
+static void speed_get(const struct dma_test *dt, u64 *val)
+{
+ *val = dt->link_speed;
+}
+
+static int speed_validate(u64 val)
+{
+ switch (val) {
+ case 20:
+ case 10:
+ case 0:
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+static void speed_set(struct dma_test *dt, u64 val)
+{
+ dt->link_speed = val;
+}
+DMA_TEST_DEBUGFS_ATTR(speed, speed_get, speed_validate, speed_set);
+
+static void packets_to_receive_get(const struct dma_test *dt, u64 *val)
+{
+ *val = dt->packets_to_receive;
+}
+
+static int packets_to_receive_validate(u64 val)
+{
+ return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
+}
+
+static void packets_to_receive_set(struct dma_test *dt, u64 val)
+{
+ dt->packets_to_receive = val;
+}
+DMA_TEST_DEBUGFS_ATTR(packets_to_receive, packets_to_receive_get,
+ packets_to_receive_validate, packets_to_receive_set);
+
+static void packets_to_send_get(const struct dma_test *dt, u64 *val)
+{
+ *val = dt->packets_to_send;
+}
+
+static int packets_to_send_validate(u64 val)
+{
+ return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
+}
+
+static void packets_to_send_set(struct dma_test *dt, u64 val)
+{
+ dt->packets_to_send = val;
+}
+DMA_TEST_DEBUGFS_ATTR(packets_to_send, packets_to_send_get,
+ packets_to_send_validate, packets_to_send_set);
+
+static int dma_test_set_bonding(struct dma_test *dt)
+{
+ switch (dt->link_width) {
+ case 2:
+ return tb_xdomain_lane_bonding_enable(dt->xd);
+ case 1:
+ tb_xdomain_lane_bonding_disable(dt->xd);
+ fallthrough;
+ default:
+ return 0;
+ }
+}
+
+static bool dma_test_validate_config(struct dma_test *dt)
+{
+ if (!dt->packets_to_send && !dt->packets_to_receive)
+ return false;
+ if (dt->packets_to_send && dt->packets_to_receive &&
+ dt->packets_to_send != dt->packets_to_receive)
+ return false;
+ return true;
+}
+
+static void dma_test_check_errors(struct dma_test *dt, int ret)
+{
+ if (!dt->error_code) {
+ if (dt->link_speed && dt->xd->link_speed != dt->link_speed) {
+ dt->error_code = DMA_TEST_SPEED_ERROR;
+ } else if (dt->link_width &&
+ dt->xd->link_width != dt->link_width) {
+ dt->error_code = DMA_TEST_WIDTH_ERROR;
+ } else if (dt->packets_to_send != dt->packets_sent ||
+ dt->packets_to_receive != dt->packets_received ||
+ dt->crc_errors || dt->buffer_overflow_errors) {
+ dt->error_code = DMA_TEST_PACKET_ERROR;
+ } else {
+ return;
+ }
+ }
+
+ dt->result = DMA_TEST_FAIL;
+}
+
+static int test_store(void *data, u64 val)
+{
+ struct tb_service *svc = data;
+ struct dma_test *dt = tb_service_get_drvdata(svc);
+ int ret;
+
+ if (val != 1)
+ return -EINVAL;
+
+ ret = mutex_lock_interruptible(&dt->lock);
+ if (ret)
+ return ret;
+
+ dt->packets_sent = 0;
+ dt->packets_received = 0;
+ dt->crc_errors = 0;
+ dt->buffer_overflow_errors = 0;
+ dt->result = DMA_TEST_SUCCESS;
+ dt->error_code = DMA_TEST_NO_ERROR;
+
+ dev_dbg(&svc->dev, "DMA test starting\n");
+ if (dt->link_speed)
+ dev_dbg(&svc->dev, "link_speed: %u Gb/s\n", dt->link_speed);
+ if (dt->link_width)
+ dev_dbg(&svc->dev, "link_width: %u\n", dt->link_width);
+ dev_dbg(&svc->dev, "packets_to_send: %u\n", dt->packets_to_send);
+ dev_dbg(&svc->dev, "packets_to_receive: %u\n", dt->packets_to_receive);
+
+ if (!dma_test_validate_config(dt)) {
+ dev_err(&svc->dev, "invalid test configuration\n");
+ dt->error_code = DMA_TEST_CONFIG_ERROR;
+ goto out_unlock;
+ }
+
+ ret = dma_test_set_bonding(dt);
+ if (ret) {
+ dev_err(&svc->dev, "failed to set lanes\n");
+ dt->error_code = DMA_TEST_BONDING_ERROR;
+ goto out_unlock;
+ }
+
+ ret = dma_test_start_rings(dt);
+ if (ret) {
+ dev_err(&svc->dev, "failed to enable DMA rings\n");
+ dt->error_code = DMA_TEST_DMA_ERROR;
+ goto out_unlock;
+ }
+
+ if (dt->packets_to_receive) {
+ reinit_completion(&dt->complete);
+ ret = dma_test_submit_rx(dt, dt->packets_to_receive);
+ if (ret) {
+ dev_err(&svc->dev, "failed to submit receive buffers\n");
+ dt->error_code = DMA_TEST_BUFFER_ERROR;
+ goto out_stop;
+ }
+ }
+
+ if (dt->packets_to_send) {
+ ret = dma_test_submit_tx(dt, dt->packets_to_send);
+ if (ret) {
+ dev_err(&svc->dev, "failed to submit transmit buffers\n");
+ dt->error_code = DMA_TEST_BUFFER_ERROR;
+ goto out_stop;
+ }
+ }
+
+ if (dt->packets_to_receive) {
+ ret = wait_for_completion_interruptible(&dt->complete);
+ if (ret) {
+ dt->error_code = DMA_TEST_INTERRUPTED;
+ goto out_stop;
+ }
+ }
+
+out_stop:
+ dma_test_stop_rings(dt);
+out_unlock:
+ dma_test_check_errors(dt, ret);
+ mutex_unlock(&dt->lock);
+
+ dev_dbg(&svc->dev, "DMA test %s\n", dma_test_result_names[dt->result]);
+ return ret;
+}
+DEFINE_DEBUGFS_ATTRIBUTE(test_fops, NULL, test_store, "%llu\n");
+
+static int status_show(struct seq_file *s, void *not_used)
+{
+ struct tb_service *svc = s->private;
+ struct dma_test *dt = tb_service_get_drvdata(svc);
+ int ret;
+
+ ret = mutex_lock_interruptible(&dt->lock);
+ if (ret)
+ return ret;
+
+ seq_printf(s, "result: %s\n", dma_test_result_names[dt->result]);
+ if (dt->result == DMA_TEST_NOT_RUN)
+ goto out_unlock;
+
+ seq_printf(s, "packets received: %u\n", dt->packets_received);
+ seq_printf(s, "packets sent: %u\n", dt->packets_sent);
+ seq_printf(s, "CRC errors: %u\n", dt->crc_errors);
+ seq_printf(s, "buffer overflow errors: %u\n",
+ dt->buffer_overflow_errors);
+ seq_printf(s, "error: %s\n", dma_test_error_names[dt->error_code]);
+
+out_unlock:
+ mutex_unlock(&dt->lock);
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(status);
+
+static void dma_test_debugfs_init(struct tb_service *svc)
+{
+ struct dma_test *dt = tb_service_get_drvdata(svc);
+
+ dt->debugfs_dir = debugfs_create_dir("dma_test", svc->debugfs_dir);
+
+ debugfs_create_file("lanes", 0600, dt->debugfs_dir, svc, &lanes_fops);
+ debugfs_create_file("speed", 0600, dt->debugfs_dir, svc, &speed_fops);
+ debugfs_create_file("packets_to_receive", 0600, dt->debugfs_dir, svc,
+ &packets_to_receive_fops);
+ debugfs_create_file("packets_to_send", 0600, dt->debugfs_dir, svc,
+ &packets_to_send_fops);
+ debugfs_create_file("status", 0400, dt->debugfs_dir, svc, &status_fops);
+ debugfs_create_file("test", 0200, dt->debugfs_dir, svc, &test_fops);
+}
+
+static int dma_test_probe(struct tb_service *svc, const struct tb_service_id *id)
+{
+ struct tb_xdomain *xd = tb_service_parent(svc);
+ struct dma_test *dt;
+
+ dt = devm_kzalloc(&svc->dev, sizeof(*dt), GFP_KERNEL);
+ if (!dt)
+ return -ENOMEM;
+
+ dt->svc = svc;
+ dt->xd = xd;
+ mutex_init(&dt->lock);
+ init_completion(&dt->complete);
+
+ tb_service_set_drvdata(svc, dt);
+ dma_test_debugfs_init(svc);
+
+ return 0;
+}
+
+static void dma_test_remove(struct tb_service *svc)
+{
+ struct dma_test *dt = tb_service_get_drvdata(svc);
+
+ mutex_lock(&dt->lock);
+ debugfs_remove_recursive(dt->debugfs_dir);
+ mutex_unlock(&dt->lock);
+}
+
+static int __maybe_unused dma_test_suspend(struct device *dev)
+{
+ /*
+ * No need to do anything special here. If userspace is writing
+ * to the test attribute when suspend started, it comes out from
+ * wait_for_completion_interruptible() with -ERESTARTSYS and the
+ * DMA test fails tearing down the rings. Once userspace is
+ * thawed the kernel restarts the write syscall effectively
+ * re-running the test.
+ */
+ return 0;
+}
+
+static int __maybe_unused dma_test_resume(struct device *dev)
+{
+ return 0;
+}
+
+static const struct dev_pm_ops dma_test_pm_ops = {
+ SET_SYSTEM_SLEEP_PM_OPS(dma_test_suspend, dma_test_resume)
+};
+
+static const struct tb_service_id dma_test_ids[] = {
+ { TB_SERVICE("dma_test", 1) },
+ { },
+};
+MODULE_DEVICE_TABLE(tbsvc, dma_test_ids);
+
+static struct tb_service_driver dma_test_driver = {
+ .driver = {
+ .owner = THIS_MODULE,
+ .name = "thunderbolt_dma_test",
+ .pm = &dma_test_pm_ops,
+ },
+ .probe = dma_test_probe,
+ .remove = dma_test_remove,
+ .id_table = dma_test_ids,
+};
+
+static int __init dma_test_init(void)
+{
+ u64 data_value = DMA_TEST_DATA_PATTERN;
+ int i, ret;
+
+ dma_test_pattern = kmalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
+ if (!dma_test_pattern)
+ return -ENOMEM;
+
+ for (i = 0; i < DMA_TEST_FRAME_SIZE / sizeof(data_value); i++)
+ ((u32 *)dma_test_pattern)[i] = data_value++;
+
+ dma_test_dir = tb_property_create_dir(&dma_test_dir_uuid);
+ if (!dma_test_dir) {
+ ret = -ENOMEM;
+ goto err_free_pattern;
+ }
+
+ tb_property_add_immediate(dma_test_dir, "prtcid", 1);
+ tb_property_add_immediate(dma_test_dir, "prtcvers", 1);
+ tb_property_add_immediate(dma_test_dir, "prtcrevs", 0);
+ tb_property_add_immediate(dma_test_dir, "prtcstns", 0);
+
+ ret = tb_register_property_dir("dma_test", dma_test_dir);
+ if (ret)
+ goto err_free_dir;
+
+ ret = tb_register_service_driver(&dma_test_driver);
+ if (ret)
+ goto err_unregister_dir;
+
+ return 0;
+
+err_unregister_dir:
+ tb_unregister_property_dir("dma_test", dma_test_dir);
+err_free_dir:
+ tb_property_free_dir(dma_test_dir);
+err_free_pattern:
+ kfree(dma_test_pattern);
+
+ return ret;
+}
+module_init(dma_test_init);
+
+static void __exit dma_test_exit(void)
+{
+ tb_unregister_service_driver(&dma_test_driver);
+ tb_unregister_property_dir("dma_test", dma_test_dir);
+ tb_property_free_dir(dma_test_dir);
+ kfree(dma_test_pattern);
+}
+module_exit(dma_test_exit);
+
+MODULE_AUTHOR("Isaac Hazan <isaac.hazan@intel.com>");
+MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
+MODULE_DESCRIPTION("DMA traffic test driver");
+MODULE_LICENSE("GPL v2");