public inbox for linux-usb@vger.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 06/24] usb: xhci: refactor xhci_set_cmd_ring_deq()
Date: Thu, 15 May 2025 16:56:03 +0300	[thread overview]
Message-ID: <20250515135621.335595-7-mathias.nyman@linux.intel.com> (raw)
In-Reply-To: <20250515135621.335595-1-mathias.nyman@linux.intel.com>

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

Refactor xhci_set_cmd_ring_deq() making the code more understandable by
using more descriptive constants and separating operations logically.

- Remove 'CMD_RING_RSVD_BITS' the macro is misleading, the reserved bits
  are 5:4, yet the mask is for bits 5:0.
- Introduce masks 'CMD_RING_PTR_MASK' and 'CMD_RING_CYCLE' to clearly
  define the bits for the Command Ring pointer and Command Ring Cycle.
- Simplifying the process of setting the command ring address by separating
  the DMA address calculation and the Command Ring Control register (crcr)
  updates.
- Remove the "// " prefix from trace messages, as it is unnecessary and
  distracting.

Note: In the current implementation, the cycle bit is not cleared before
applying the OR operation. Although this hasn't caused issues so far
because the bit is '0' before reaching this function, the bit is now
cleared before being set to prevent potential future problems and simplify
the process.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
---
 drivers/usb/host/xhci.c | 28 +++++++++++++++-------------
 drivers/usb/host/xhci.h |  8 ++++----
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 66a9106d8b31..4c9174c5c7c7 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -497,19 +497,21 @@ static void xhci_enable_max_dev_slots(struct xhci_hcd *xhci)
 
 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
 {
-	u64	val_64;
-
-	/* step 2: initialize command ring buffer */
-	val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
-	val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
-		(xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
-					xhci->cmd_ring->dequeue) &
-			(u64) ~CMD_RING_RSVD_BITS) |
-		xhci->cmd_ring->cycle_state;
-	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
-			"// Setting command ring address to 0x%llx",
-			(unsigned long long) val_64);
-	xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
+	dma_addr_t deq_dma;
+	u64 crcr;
+
+	deq_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, xhci->cmd_ring->dequeue);
+	deq_dma &= CMD_RING_PTR_MASK;
+
+	crcr = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
+	crcr &= ~CMD_RING_PTR_MASK;
+	crcr |= deq_dma;
+
+	crcr &= ~CMD_RING_CYCLE;
+	crcr |= xhci->cmd_ring->cycle_state;
+
+	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Setting command ring address to 0x%llx", crcr);
+	xhci_write_64(xhci, crcr, &xhci->op_regs->cmd_ring);
 }
 
 /*
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index f8198ec02981..6c1758f8fd01 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -191,16 +191,16 @@ struct xhci_op_regs {
 #define	DEV_NOTE_FWAKE		ENABLE_DEV_NOTE(1)
 
 /* CRCR - Command Ring Control Register - cmd_ring bitmasks */
-/* bit 0 is the command ring cycle state */
+/* bit 0 - Cycle bit indicates the ownership of the command ring */
+#define CMD_RING_CYCLE		(1 << 0)
 /* stop ring operation after completion of the currently executing command */
 #define CMD_RING_PAUSE		(1 << 1)
 /* stop ring immediately - abort the currently executing command */
 #define CMD_RING_ABORT		(1 << 2)
 /* true: command ring is running */
 #define CMD_RING_RUNNING	(1 << 3)
-/* bits 4:5 reserved and should be preserved */
-/* Command Ring pointer - bit mask for the lower 32 bits. */
-#define CMD_RING_RSVD_BITS	(0x3f)
+/* bits 63:6 - Command Ring pointer */
+#define CMD_RING_PTR_MASK	GENMASK_ULL(63, 6)
 
 /* CONFIG - Configure Register - config_reg bitmasks */
 /* bits 0:7 - maximum number of device slots enabled (NumSlotsEn) */
-- 
2.43.0


  parent reply	other threads:[~2025-05-15 13:56 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-15 13:55 [PATCH 00/24] xhci features for usb-next Mathias Nyman
2025-05-15 13:55 ` [PATCH 01/24] usb: xhci: Don't log transfer ring segment list on errors Mathias Nyman
2025-05-15 13:55 ` [PATCH 02/24] usb: xhci: Add debugfs support for xHCI port bandwidth Mathias Nyman
2025-05-15 13:56 ` [PATCH 03/24] usb: xhci: relocate pre-allocation initialization Mathias Nyman
2025-05-15 13:56 ` [PATCH 04/24] usb: xhci: move device slot enabling register write Mathias Nyman
2025-05-15 13:56 ` [PATCH 05/24] usb: xhci: move command ring pointer write Mathias Nyman
2025-05-15 13:56 ` Mathias Nyman [this message]
2025-05-15 13:56 ` [PATCH 07/24] usb: xhci: move DCBAA " Mathias Nyman
2025-05-15 13:56 ` [PATCH 08/24] usb: xhci: move doorbell array pointer assignment Mathias Nyman
2025-05-15 13:56 ` [PATCH 09/24] usb: xhci: move enabling of USB 3 device notifications Mathias Nyman
2025-05-15 13:56 ` [PATCH 10/24] usb: xhci: remove error handling from xhci_add_interrupter() Mathias Nyman
2025-05-15 13:56 ` [PATCH 11/24] usb: xhci: move initialization of the primary interrupter Mathias Nyman
2025-05-15 13:56 ` [PATCH 12/24] usb: xhci: add individual allocation checks in xhci_mem_init() Mathias Nyman
2025-05-15 13:56 ` [PATCH 13/24] usb: xhci: cleanup xhci_mem_init() Mathias Nyman
2025-05-15 13:56 ` [PATCH 14/24] usb: xhci: set requested IMODI to the closest supported value Mathias Nyman
2025-05-15 13:56 ` [PATCH 15/24] usb: xhci: improve Interrupt Management register macros Mathias Nyman
2025-05-15 13:56 ` [PATCH 16/24] usb: xhci: guarantee that IMAN register is flushed Mathias Nyman
2025-05-15 13:56 ` [PATCH 17/24] usb: xhci: remove '0' write to write-1-to-clear register Mathias Nyman
2025-05-15 13:56 ` [PATCH 18/24] usb: xhci: rework Event Ring Segment Table Size mask Mathias Nyman
2025-05-15 13:56 ` [PATCH 19/24] usb: xhci: rework Event Ring Segment Table Address mask Mathias Nyman
2025-05-15 13:56 ` [PATCH 20/24] usb: xhci: cleanup IMOD register comments Mathias Nyman
2025-05-15 13:56 ` [PATCH 21/24] usb: xhci: rename 'irq_pending' to 'iman' Mathias Nyman
2025-05-15 13:56 ` [PATCH 22/24] usb: xhci: rename 'irq_control' to 'imod' Mathias Nyman
2025-05-15 13:56 ` [PATCH 23/24] usb: xhci: add warning message specifying which Set TR Deq failed Mathias Nyman
2025-05-16 12:43   ` Michał Pecio
2025-05-16 14:32     ` Neronin, Niklas
2025-05-19 14:33     ` Mathias Nyman
2025-05-21 10:34       ` Greg KH
2025-05-21 13:22         ` Mathias Nyman
2025-05-15 13:56 ` [PATCH 24/24] xhci: Add host support for eUSB2 double isochronous bandwidth devices Mathias Nyman
2025-05-16  0:27   ` Thinh Nguyen
2025-05-16  0:40     ` Thinh Nguyen
2025-05-16 11:58       ` Mathias Nyman
2025-05-17  0:16         ` Thinh Nguyen
2025-05-21 10:36 ` [PATCH 00/24] xhci features for usb-next Greg KH
2025-05-21 13:24   ` 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=20250515135621.335595-7-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox