Skip to contentSkip to conversation

usb: xhci: improve Endpoint Context Field 0x08 macros

PATCH Superseded Niklas Neronin 1 participant 4 messages Feb 26, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover usb: xhci: improve Endpoint Context Field 0x08 macros

Improve and utilize Endpoint Context Field 0x08 mask macros.

In multiple places, the Endpoint Context mask values are hardcoded or include reserved bits. Replacing hardcoded masks with their correct corresponding macros improves code readability, maintainability.

Niklas Neronin (3):
usb: xhci: use correct TR Dequeue pointer mask
usb: xhci: improve TR Dequeue Pointer macro
usb: xhci: replace hardcoded Endpoint context masks with corresponding
macros

 drivers/usb/host/xhci-ring.c | 13 +++++++------
 drivers/usb/host/xhci.h      |  3 ++-
 2 files changed, 9 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/3 usb: xhci: use correct TR Dequeue pointer mask

The Endpoint Context Field at Offset 0x08 is defined as follows:
- Bit 0: Dequeue Cycle State (DCS)
- Bits 3:1: RsvdZ (Reserved and Zero)
- Bits 63:4: TR Dequeue Pointer

When reading the TR Dequeue Pointer for an Endpoint without Streams, the inverted Dequeue Cycle State mask ('~EP_CTX_CYCLE_MASK') is used, which include the Reserved bits. Although bits 3:1 are typically zero, using the incorrect mask could potentially cause issues.

Swap mask 'EP_CTX_CYCLE_MASK' for 'SCTX_DEQ_MASK'. i.e. ~(1 << 0) for (~0xfL).

---
 drivers/usb/host/xhci-ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +1 -1
index 965bffce301e..d4ec1e707107 100644
@@ -1464,7 +1464,7 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
 				stream_ctx->reserved[1] = 0;
 			}
 		} else {
-			deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK;
+			deq = le64_to_cpu(ep_ctx->deq) & SCTX_DEQ_MASK;
 		}
 		xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
 			"Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
Signature · 1 line
2.47.2
#2 lore raw

PATCH 2/3 usb: xhci: improve TR Dequeue Pointer macro

In reply to Niklas Neronin

The Endpoint Context Field at Offset 0x08 is defined as follows:
- Bit 0: Dequeue Cycle State (DCS)
- Bits 3:1: RsvdZ (Reserved and Zero)
- Bits 63:4: TR Dequeue Pointer

Rename 'SCTX_DEQ_MASK' macro to 'TR_DEQ_PTR_MASK' for better clarity. The acronym "SCTX" stands for Stream Context, but the TR Dequeue Pointer is not exclusively for Stream Context.

Use GENMASK_ULL() to get mask for bits 63:4 instead of inverted 3:0 mask.

---
 drivers/usb/host/xhci-ring.c | 4 ++--
 drivers/usb/host/xhci.h      | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-ring.c +2 -2
index d4ec1e707107..9ff5ca4d5c1c 100644
@@ -1448,7 +1448,7 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
 		u64 deq;
 		/* 4.6.10 deq ptr is written to the stream ctx for streams */
 		if (ep->ep_state & EP_HAS_STREAMS) {
-			deq = le64_to_cpu(stream_ctx->stream_ring) & SCTX_DEQ_MASK;
+			deq = le64_to_cpu(stream_ctx->stream_ring) & TR_DEQ_PTR_MASK;
 
 			/*
 			 * Cadence xHCI controllers store some endpoint state
@@ -1464,7 +1464,7 @@ static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id,
 				stream_ctx->reserved[1] = 0;
 			}
 		} else {
-			deq = le64_to_cpu(ep_ctx->deq) & SCTX_DEQ_MASK;
+			deq = le64_to_cpu(ep_ctx->deq) & TR_DEQ_PTR_MASK;
 		}
 		xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
 			"Successful Set TR Deq Ptr cmd, deq = @%08llx", deq);
drivers/usb/host/xhci.h +2 -1
index 8c164340a2c3..621ffe9df283 100644
@@ -500,7 +500,8 @@ struct xhci_ep_ctx {
 
 /* deq bitmasks */
 #define EP_CTX_CYCLE_MASK		(1 << 0)
-#define SCTX_DEQ_MASK			(~0xfL)
+/* bits 63:4 - TR Dequeue Pointer */
+#define TR_DEQ_PTR_MASK			GENMASK_ULL(63, 4)
 
 
 /**
Signature · 1 line
2.47.2
#3 lore raw

PATCH 3/3 usb: xhci: replace hardcoded Endpoint context masks with corresponding macros

In reply to Niklas Neronin

The Endpoint Context Field at Offset 0x08 is defined as follows:
- Bit 0: Dequeue Cycle State (DCS)
- Bits 3:1: RsvdZ (Reserved and Zero)
- Bits 63:4: TR Dequeue Pointer

Replace hardcoded Endpoint context masks with their corresponding macros.

In xhci_move_dequeue_past_td() move the use of TR Dequeue Pointer mask out of the while loop. The TR dequeue pointer does not change during the loop.

---
 drivers/usb/host/xhci-ring.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-ring.c +5 -4
index 9ff5ca4d5c1c..c45eabe34772 100644
@@ -657,9 +657,10 @@ static int xhci_move_dequeue_past_td(struct xhci_hcd *xhci,
 	}
 
 	hw_dequeue = xhci_get_hw_deq(xhci, dev, ep_index, stream_id);
+	new_cycle = hw_dequeue & EP_CTX_CYCLE_MASK;
+	hw_dequeue &= TR_DEQ_PTR_MASK;
 	new_seg = ep_ring->deq_seg;
 	new_deq = ep_ring->dequeue;
-	new_cycle = hw_dequeue & 0x1;
 
 	/*
 	 * We want to find the pointer, segment and cycle state of the new trb
@@ -669,7 +670,7 @@ static int xhci_move_dequeue_past_td(struct xhci_hcd *xhci,
 	 */
 	do {
 		if (!cycle_found && xhci_trb_virt_to_dma(new_seg, new_deq)
-		    == (dma_addr_t)(hw_dequeue & ~0xf)) {
+		    == (dma_addr_t)hw_dequeue) {
 			cycle_found = true;
 			if (td_last_trb_found)
 				break;
@@ -1012,7 +1013,7 @@ static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep)
 		 */
 		hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index,
 					 td->urb->stream_id);
-		hw_deq &= ~0xf;
+		hw_deq &= TR_DEQ_PTR_MASK;
 
 		if (td->cancel_status == TD_HALTED || trb_in_td(xhci, td, hw_deq, false)) {
 			switch (td->cancel_status) {
@@ -1102,7 +1103,7 @@ static struct xhci_td *find_halted_td(struct xhci_virt_ep *ep)
 
 	if (!list_empty(&ep->ring->td_list)) { /* Not streams compatible */
 		hw_deq = xhci_get_hw_deq(ep->xhci, ep->vdev, ep->ep_index, 0);
-		hw_deq &= ~0xf;
+		hw_deq &= TR_DEQ_PTR_MASK;
 		td = list_first_entry(&ep->ring->td_list, struct xhci_td, td_list);
 		if (trb_in_td(ep->xhci, td, hw_deq, false))
 			return td;
Signature · 1 line
2.47.2