Module Name:    src
Committed By:   riastradh
Date:           Fri Apr  5 18:57:10 UTC 2024

Modified Files:
        src/sys/dev/usb: ehci.c motg.c ohci.c uhci.c usbdi.c xhci.c
        src/sys/external/bsd/dwc2: dwc2.c

Log Message:
usb *hci: Always set ux_status before usbd_xfer_schedule_timeout.

Add an assert to usbd_xfer_schedule_timeout to enforce this.

Since access to ux_status is serialized by the bus lock, and nothing
releases the bus lock in the interim, this doesn't make a functional
change.  But it does reduce confusion by readers, who no longer have
to worry if some callers got the order wrong.  It could also now
potentially be factored out in a subsequent commit.


To generate a diff of this commit:
cvs rdiff -u -r1.324 -r1.325 src/sys/dev/usb/ehci.c
cvs rdiff -u -r1.42 -r1.43 src/sys/dev/usb/motg.c
cvs rdiff -u -r1.325 -r1.326 src/sys/dev/usb/ohci.c
cvs rdiff -u -r1.317 -r1.318 src/sys/dev/usb/uhci.c
cvs rdiff -u -r1.252 -r1.253 src/sys/dev/usb/usbdi.c
cvs rdiff -u -r1.180 -r1.181 src/sys/dev/usb/xhci.c
cvs rdiff -u -r1.80 -r1.81 src/sys/external/bsd/dwc2/dwc2.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/usb/ehci.c
diff -u src/sys/dev/usb/ehci.c:1.324 src/sys/dev/usb/ehci.c:1.325
--- src/sys/dev/usb/ehci.c:1.324	Sat Feb 10 09:21:53 2024
+++ src/sys/dev/usb/ehci.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: ehci.c,v 1.324 2024/02/10 09:21:53 andvar Exp $ */
+/*	$NetBSD: ehci.c,v 1.325 2024/04/05 18:57:10 riastradh Exp $ */
 
 /*
  * Copyright (c) 2004-2012,2016,2020 The NetBSD Foundation, Inc.
@@ -54,7 +54,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.324 2024/02/10 09:21:53 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.325 2024/04/05 18:57:10 riastradh Exp $");
 
 #include "ohci.h"
 #include "uhci.h"
@@ -3799,9 +3799,9 @@ ehci_device_ctrl_start(struct usbd_xfer 
 
 	/* Insert qTD in QH list - also does usb_syncmem(sqh) */
 	ehci_set_qh_qtd(sqh, setup);
-	usbd_xfer_schedule_timeout(xfer);
 	ehci_add_intr_list(sc, exfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 #if 0
 #ifdef EHCI_DEBUG
@@ -3985,9 +3985,9 @@ ehci_device_bulk_start(struct usbd_xfer 
 
 	/* also does usb_syncmem(sqh) */
 	ehci_set_qh_qtd(sqh, exfer->ex_sqtdstart);
-	usbd_xfer_schedule_timeout(xfer);
 	ehci_add_intr_list(sc, exfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 #if 0
 #ifdef EHCI_DEBUG
@@ -4184,9 +4184,9 @@ ehci_device_intr_start(struct usbd_xfer 
 
 	/* also does usb_syncmem(sqh) */
 	ehci_set_qh_qtd(sqh, exfer->ex_sqtdstart);
-	usbd_xfer_schedule_timeout(xfer);
 	ehci_add_intr_list(sc, exfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 #if 0
 #ifdef EHCI_DEBUG

Index: src/sys/dev/usb/motg.c
diff -u src/sys/dev/usb/motg.c:1.42 src/sys/dev/usb/motg.c:1.43
--- src/sys/dev/usb/motg.c:1.42	Sun Feb  4 05:43:06 2024
+++ src/sys/dev/usb/motg.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: motg.c,v 1.42 2024/02/04 05:43:06 mrg Exp $	*/
+/*	$NetBSD: motg.c,v 1.43 2024/04/05 18:57:10 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004, 2011, 2012, 2014 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: motg.c,v 1.42 2024/02/04 05:43:06 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: motg.c,v 1.43 2024/04/05 18:57:10 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1321,8 +1321,8 @@ motg_device_ctrl_start1(struct motg_soft
 		goto end;
 	}
 	if (xfer->ux_status == USBD_NOT_STARTED) {
-		usbd_xfer_schedule_timeout(xfer);
 		xfer->ux_status = USBD_IN_PROGRESS;
+		usbd_xfer_schedule_timeout(xfer);
 	} else {
 		KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
 	}
@@ -1764,8 +1764,8 @@ motg_device_data_start1(struct motg_soft
 		goto end;
 	}
 	if (xfer->ux_status == USBD_NOT_STARTED) {
-		usbd_xfer_schedule_timeout(xfer);
 		xfer->ux_status = USBD_IN_PROGRESS;
+		usbd_xfer_schedule_timeout(xfer);
 	} else {
 		KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
 	}

Index: src/sys/dev/usb/ohci.c
diff -u src/sys/dev/usb/ohci.c:1.325 src/sys/dev/usb/ohci.c:1.326
--- src/sys/dev/usb/ohci.c:1.325	Fri Feb  9 22:08:37 2024
+++ src/sys/dev/usb/ohci.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: ohci.c,v 1.325 2024/02/09 22:08:37 andvar Exp $	*/
+/*	$NetBSD: ohci.c,v 1.326 2024/04/05 18:57:10 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004, 2005, 2012, 2016, 2020 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ohci.c,v 1.325 2024/02/09 22:08:37 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ohci.c,v 1.326 2024/04/05 18:57:10 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -2906,12 +2906,11 @@ ohci_device_ctrl_start(struct usbd_xfer 
 	    sizeof(sed->ed.ed_tailp),
 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
+	xfer->ux_status = USBD_IN_PROGRESS;
 	usbd_xfer_schedule_timeout(xfer);
 
 	DPRINTF("done", 0, 0, 0, 0);
 
-	xfer->ux_status = USBD_IN_PROGRESS;
-
 	return USBD_IN_PROGRESS;
 }
 
@@ -3113,8 +3112,8 @@ ohci_device_bulk_start(struct usbd_xfer 
 	usb_syncmem(&sed->dma, sed->offs, sizeof(sed->ed),
 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
-	usbd_xfer_schedule_timeout(xfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 	return USBD_IN_PROGRESS;
 }

Index: src/sys/dev/usb/uhci.c
diff -u src/sys/dev/usb/uhci.c:1.317 src/sys/dev/usb/uhci.c:1.318
--- src/sys/dev/usb/uhci.c:1.317	Sun Feb  4 05:43:06 2024
+++ src/sys/dev/usb/uhci.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: uhci.c,v 1.317 2024/02/04 05:43:06 mrg Exp $	*/
+/*	$NetBSD: uhci.c,v 1.318 2024/04/05 18:57:10 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2004, 2011, 2012, 2016, 2020 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.317 2024/02/04 05:43:06 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.318 2024/04/05 18:57:10 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -2312,8 +2312,8 @@ uhci_device_bulk_start(struct usbd_xfer 
 
 	uhci_add_bulk(sc, sqh);
 	uhci_add_intr_list(sc, ux);
-	usbd_xfer_schedule_timeout(xfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 	return USBD_IN_PROGRESS;
 }
@@ -2603,8 +2603,8 @@ uhci_device_ctrl_start(struct usbd_xfer 
 		DPRINTF("--- dump end ---", 0, 0, 0, 0);
 	}
 #endif
-	usbd_xfer_schedule_timeout(xfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 	return USBD_IN_PROGRESS;
 }

Index: src/sys/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.252 src/sys/dev/usb/usbdi.c:1.253
--- src/sys/dev/usb/usbdi.c:1.252	Fri Feb  9 22:08:37 2024
+++ src/sys/dev/usb/usbdi.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdi.c,v 1.252 2024/02/09 22:08:37 andvar Exp $	*/
+/*	$NetBSD: usbdi.c,v 1.253 2024/04/05 18:57:10 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.252 2024/02/09 22:08:37 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.253 2024/04/05 18:57:10 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1867,6 +1867,8 @@ usbd_xfer_schedule_timeout(struct usbd_x
 	    xfer->ux_timeout_set, xfer->ux_timeout_reset);
 
 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
+	KASSERTMSG(xfer->ux_status == USBD_IN_PROGRESS, "xfer=%p status=%d",
+	    xfer, xfer->ux_status);
 
 	if (xfer->ux_timeout_set) {
 		/*

Index: src/sys/dev/usb/xhci.c
diff -u src/sys/dev/usb/xhci.c:1.180 src/sys/dev/usb/xhci.c:1.181
--- src/sys/dev/usb/xhci.c:1.180	Thu Jul 20 11:59:04 2023
+++ src/sys/dev/usb/xhci.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhci.c,v 1.180 2023/07/20 11:59:04 riastradh Exp $	*/
+/*	$NetBSD: xhci.c,v 1.181 2024/04/05 18:57:10 riastradh Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.180 2023/07/20 11:59:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.181 2024/04/05 18:57:10 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -4495,8 +4495,8 @@ xhci_device_ctrl_start(struct usbd_xfer 
 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
 
 out:	if (xfer->ux_status == USBD_NOT_STARTED) {
-		usbd_xfer_schedule_timeout(xfer);
 		xfer->ux_status = USBD_IN_PROGRESS;
+		usbd_xfer_schedule_timeout(xfer);
 	} else {
 		/*
 		 * We must be coming from xhci_pipe_restart -- timeout
@@ -4649,8 +4649,8 @@ xhci_device_isoc_enter(struct usbd_xfer 
 	if (!polling)
 		mutex_exit(&tr->xr_lock);
 
-	xfer->ux_status = USBD_IN_PROGRESS;
 	xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
+	xfer->ux_status = USBD_IN_PROGRESS;
 	usbd_xfer_schedule_timeout(xfer);
 
 	return USBD_IN_PROGRESS;

Index: src/sys/external/bsd/dwc2/dwc2.c
diff -u src/sys/external/bsd/dwc2/dwc2.c:1.80 src/sys/external/bsd/dwc2/dwc2.c:1.81
--- src/sys/external/bsd/dwc2/dwc2.c:1.80	Thu Mar  3 06:12:11 2022
+++ src/sys/external/bsd/dwc2/dwc2.c	Fri Apr  5 18:57:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: dwc2.c,v 1.80 2022/03/03 06:12:11 riastradh Exp $	*/
+/*	$NetBSD: dwc2.c,v 1.81 2024/04/05 18:57:10 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: dwc2.c,v 1.80 2022/03/03 06:12:11 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dwc2.c,v 1.81 2024/04/05 18:57:10 riastradh Exp $");
 
 #include "opt_usb.h"
 
@@ -1092,8 +1092,8 @@ dwc2_device_start(struct usbd_xfer *xfer
 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
 	if (retval)
 		goto fail2;
-	usbd_xfer_schedule_timeout(xfer);
 	xfer->ux_status = USBD_IN_PROGRESS;
+	usbd_xfer_schedule_timeout(xfer);
 
 	if (alloc_bandwidth) {
 		dwc2_allocate_bus_bandwidth(hsotg,

Reply via email to