]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
thunderbolt: Add tb_ring_flush()
authorMika Westerberg <mika.westerberg@linux.intel.com>
Fri, 27 Jun 2025 17:25:42 +0000 (20:25 +0300)
committerMika Westerberg <mika.westerberg@linux.intel.com>
Tue, 19 May 2026 12:20:18 +0000 (14:20 +0200)
This allows the caller to wait for the ring to be empty. We are going to
need this in the upcoming userspace tunneling support.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/thunderbolt/nhi.c
include/linux/thunderbolt.h

index 13009246e617613996c4bae06350a9e82fc32f68..a0a789bfb680ff2a97c0cc19e88573fb9de75bbe 100644 (file)
@@ -325,6 +325,8 @@ invoke_callback:
                if (frame->callback)
                        frame->callback(ring, frame, canceled);
        }
+
+       wake_up(&ring->wait);
 }
 
 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
@@ -601,6 +603,7 @@ static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
        INIT_LIST_HEAD(&ring->queue);
        INIT_LIST_HEAD(&ring->in_flight);
        INIT_WORK(&ring->work, ring_work);
+       init_waitqueue_head(&ring->wait);
 
        ring->nhi = nhi;
        ring->hop = hop;
@@ -760,6 +763,31 @@ err:
 }
 EXPORT_SYMBOL_GPL(tb_ring_start);
 
+static bool tb_ring_empty(struct tb_ring *ring)
+{
+       guard(spinlock_irqsave)(&ring->lock);
+       return list_empty(&ring->in_flight);
+}
+
+/**
+ * tb_ring_flush() - Waits for a ring to be empty
+ * @ring: Ring to wait
+ * @timeout_msec: Timeout in ms how long to wait.
+ *
+ * This can be called before stopping a ring to make sure all the frames
+ * submitted prior have been completed.
+ *
+ * Return: %true if the ring is empty now, %false otherwise.
+ */
+bool tb_ring_flush(struct tb_ring *ring, unsigned int timeout_msec)
+{
+       if (!wait_event_timeout(ring->wait, tb_ring_empty(ring),
+                               msecs_to_jiffies(timeout_msec)))
+               return false;
+       return tb_ring_empty(ring);
+}
+EXPORT_SYMBOL_GPL(tb_ring_flush);
+
 /**
  * tb_ring_stop() - shutdown a ring
  * @ring: Ring to stop
index 9df8a356396fd77cb52fb5f86b1d402358b63383..9c5cb5e4f23d3afc94e01431fb96c372fa54e191 100644 (file)
@@ -556,6 +556,7 @@ struct tb_nhi {
  * @poll_data: Data passed to @start_poll
  * @interval_nsec: Interval counter if interrupt throttling is to be
  *                used with this ring (in ns)
+ * @wait: Used to signal that the ring may be empty now
  */
 struct tb_ring {
        spinlock_t lock;
@@ -580,6 +581,7 @@ struct tb_ring {
        void (*start_poll)(void *data);
        void *poll_data;
        unsigned int interval_nsec;
+       wait_queue_head_t wait;
 };
 
 /* Leave ring interrupt enabled on suspend */
@@ -653,6 +655,7 @@ struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
                                 u16 sof_mask, u16 eof_mask,
                                 void (*start_poll)(void *), void *poll_data);
 void tb_ring_start(struct tb_ring *ring);
+bool tb_ring_flush(struct tb_ring *ring, unsigned int timeout_msec);
 void tb_ring_stop(struct tb_ring *ring);
 void tb_ring_free(struct tb_ring *ring);