All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mathias Nyman <mathias.nyman@linux.intel.com>
To: <gregkh@linuxfoundation.org>
Cc: <linux-usb@vger.kernel.org>,
	Niklas Neronin <niklas.neronin@linux.intel.com>,
	Mathias Nyman <mathias.nyman@linux.intel.com>
Subject: [PATCH 16/33] usb: xhci: add xhci_initialize_ring_segments()
Date: Wed,  6 Nov 2024 12:14:42 +0200	[thread overview]
Message-ID: <20241106101459.775897-17-mathias.nyman@linux.intel.com> (raw)
In-Reply-To: <20241106101459.775897-1-mathias.nyman@linux.intel.com>

From: Niklas Neronin <niklas.neronin@linux.intel.com>

A ring consists of a list of segments, each containing a specific number of
TRBs. The xhci driver allocates and initializes ring segments and TRBs in
the same functions. This combined allocation and initialization process
leads to an issue where, after hibernation (S4 state), the xhci driver
frees all its memory and re-creates the rings, segments, and TRBs from
scratch.

Move all default ring segment initialization into function
xhci_initialize_ring_segments(). This function can be called to
reinitialize a ring without freeing and reallocating it.

Since xhci_alloc_segments_for_ring() no longer initializes segment TRBs,
xhci_initialize_ring_segments() is added to xhci_ring_expansion(). This
results in the last segment of the source ring having the 'LINK_TOGGLE'
bit set. Therefore, if the last source ring segment is not the last in
the destination ring, the 'LINK_TOGGLE' bit must be cleared.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
---
 drivers/usb/host/xhci-mem.c | 41 +++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 41a5e67e1c4f..4295e9a4de50 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -113,6 +113,22 @@ static void xhci_set_link_trb(struct xhci_segment *seg, bool chain_links)
 	trb->link.segment_ptr = cpu_to_le64(seg->next->dma);
 }
 
+static void xhci_initialize_ring_segments(struct xhci_hcd *xhci, struct xhci_ring *ring)
+{
+	struct xhci_segment *seg;
+	bool chain_links;
+
+	if (ring->type == TYPE_EVENT)
+		return;
+
+	chain_links = xhci_link_chain_quirk(xhci, ring->type);
+	xhci_for_each_ring_seg(ring->first_seg, seg)
+		xhci_set_link_trb(seg, chain_links);
+
+	/* See section 4.9.2.1 and 6.4.4.1 */
+	ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |= cpu_to_le32(LINK_TOGGLE);
+}
+
 /*
  * Link the src ring segments to the dst ring.
  * Set Toggle Cycle for the new ring if needed.
@@ -143,13 +159,13 @@ static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *src, struct
 	dst->num_segs += src->num_segs;
 
 	if (dst->enq_seg == dst->last_seg) {
-		if (dst->type != TYPE_EVENT) {
+		if (dst->type != TYPE_EVENT)
 			dst->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control
 				&= ~cpu_to_le32(LINK_TOGGLE);
-			src->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control
-				|= cpu_to_le32(LINK_TOGGLE);
-		}
+
 		dst->last_seg = src->last_seg;
+	} else if (dst->type != TYPE_EVENT) {
+		src->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control &= ~cpu_to_le32(LINK_TOGGLE);
 	}
 
 	for (seg = dst->enq_seg; seg != dst->last_seg; seg = seg->next)
@@ -315,9 +331,6 @@ static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci, struct xhci_ring
 {
 	struct xhci_segment *prev;
 	unsigned int num = 0;
-	bool chain_links;
-
-	chain_links = xhci_link_chain_quirk(xhci, ring->type);
 
 	prev = xhci_segment_alloc(xhci, ring->bounce_buf_len, num, flags);
 	if (!prev)
@@ -333,17 +346,12 @@ static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci, struct xhci_ring
 			goto free_segments;
 
 		prev->next = next;
-		if (ring->type != TYPE_EVENT)
-			xhci_set_link_trb(prev, chain_links);
 		prev = next;
 		num++;
 	}
 	ring->last_seg = prev;
 
 	ring->last_seg->next = ring->first_seg;
-	if (ring->type != TYPE_EVENT)
-		xhci_set_link_trb(prev, chain_links);
-
 	return 0;
 
 free_segments:
@@ -381,12 +389,7 @@ struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci, unsigned int num_segs,
 	if (ret)
 		goto fail;
 
-	/* Only event ring does not use link TRB */
-	if (type != TYPE_EVENT) {
-		/* See section 4.9.2.1 and 6.4.4.1 */
-		ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
-			cpu_to_le32(LINK_TOGGLE);
-	}
+	xhci_initialize_ring_segments(xhci, ring);
 	xhci_initialize_ring_info(ring);
 	trace_xhci_ring_alloc(ring);
 	return ring;
@@ -424,6 +427,8 @@ int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
 	if (ret)
 		return -ENOMEM;
 
+	xhci_initialize_ring_segments(xhci, &new_ring);
+
 	if (ring->type == TYPE_STREAM) {
 		ret = xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
 							 new_ring.first_seg, flags);
-- 
2.25.1


  parent reply	other threads:[~2024-11-06 10:13 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-06 10:14 [PATCH 00/33] xhci features and fixes for usb-next Mathias Nyman
2024-11-06 10:14 ` [PATCH 01/33] xhci: Add Isochronous TRB fields to TRB tracer Mathias Nyman
2024-11-06 10:14 ` [PATCH 02/33] usb: xhci: Remove unused parameters of next_trb() Mathias Nyman
2024-11-06 10:14 ` [PATCH 03/33] usb: xhci: Fix sum_trb_lengths() Mathias Nyman
2024-11-06 10:14 ` [PATCH 04/33] xhci: Cleanup Candence controller PCI device and vendor ID usage Mathias Nyman
2024-11-06 10:14 ` [PATCH 05/33] xhci: show DMA address of TRB when tracing TRBs Mathias Nyman
2024-11-06 10:14 ` [PATCH 06/33] xhci: Don't trace ring at every enqueue or dequeue increase Mathias Nyman
2024-11-06 10:14 ` [PATCH 07/33] xhci: add stream context tracing Mathias Nyman
2024-11-06 10:14 ` [PATCH 08/33] xhci: trace stream context at Set TR Deq command completion Mathias Nyman
2024-11-06 10:14 ` [PATCH 09/33] xhci: debugfs: Add virt endpoint state to xhci debugfs Mathias Nyman
2024-11-06 10:14 ` [PATCH 10/33] usb: xhci: introduce macro for ring segment list iteration Mathias Nyman
2024-11-06 10:14 ` [PATCH 11/33] usb: xhci: remove option to change a default ring's TRB cycle bit Mathias Nyman
2024-11-06 10:14 ` [PATCH 12/33] usb: xhci: adjust xhci_alloc_segments_for_ring() arguments Mathias Nyman
2024-11-06 10:14 ` [PATCH 13/33] usb: xhci: rework xhci_free_segments_for_ring() Mathias Nyman
2024-11-06 10:14 ` [PATCH 14/33] usb: xhci: refactor xhci_link_rings() to use source and destination rings Mathias Nyman
2024-11-06 10:14 ` [PATCH 15/33] usb: xhci: rework xhci_link_segments() Mathias Nyman
2024-11-06 10:14 ` Mathias Nyman [this message]
2024-11-06 10:14 ` [PATCH 17/33] xhci: Combine two if statements for Etron xHCI host Mathias Nyman
2024-11-06 10:14 ` [PATCH 18/33] xhci: Don't issue Reset Device command to " Mathias Nyman
2024-11-06 10:14 ` [PATCH 19/33] xhci: Fix control transfer error on " Mathias Nyman
2024-11-06 10:14 ` [PATCH 20/33] xhci: Don't perform Soft Retry for " Mathias Nyman
2024-11-06 10:14 ` [PATCH 21/33] xhci: pci: Use standard pattern for device IDs Mathias Nyman
2024-11-06 10:14 ` [PATCH 22/33] xhci: pci: Fix indentation in the PCI device ID definitions Mathias Nyman
2024-11-06 10:14 ` [PATCH 23/33] usb: xhci: simplify TDs start and end naming scheme in struct 'xhci_td' Mathias Nyman
2024-11-06 10:14 ` [PATCH 24/33] usb: xhci: move link TRB quirk to xhci_gen_setup() Mathias Nyman
2024-11-06 10:14 ` [PATCH 25/33] usb: xhci: request MSI/-X according to requested amount Mathias Nyman
2024-11-06 10:14 ` [PATCH 26/33] usb: xhci: improve xhci_clear_command_ring() Mathias Nyman
2024-11-06 10:14 ` [PATCH 27/33] usb: xhci: remove unused arguments from td_to_noop() Mathias Nyman
2024-11-06 10:14 ` [PATCH 28/33] usb: xhci: refactor xhci_td_cleanup() to return void Mathias Nyman
2024-11-06 10:14 ` [PATCH 29/33] usb: xhci: add help function xhci_dequeue_td() Mathias Nyman
2024-11-06 10:14 ` [PATCH 30/33] usb: xhci: remove irrelevant comment Mathias Nyman
2024-11-06 10:14 ` [PATCH 31/33] usb: xhci: Limit Stop Endpoint retries Mathias Nyman
2024-11-06 10:14 ` [PATCH 32/33] usb: xhci: Fix TD invalidation under pending Set TR Dequeue Mathias Nyman
2024-11-06 10:14 ` [PATCH 33/33] usb: xhci: Avoid queuing redundant Stop Endpoint commands Mathias Nyman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241106101459.775897-17-mathias.nyman@linux.intel.com \
    --to=mathias.nyman@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=niklas.neronin@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.