Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src-draft/trunk]: src Rearrange usbwifi following the changes for usbnet, ma...



details:   https://anonhg.NetBSD.org/src-all/rev/3e280b553ced
branches:  trunk
changeset: 377055:3e280b553ced
user:      Martin Husemann <martin%NetBSD.org@localhost>
date:      Tue Feb 08 21:03:01 2022 +0100

description:
Rearrange usbwifi following the changes for usbnet, make more details
internal to usbwifi.
Make the core lock identical with the net80211 "ic" lock.
Make all rx/tx locking internal to usbwifi.
Add random source.
Try to make attach / detach race free.

diffstat:

 share/man/man9/usbwifi.9 |  123 +++---------------
 sys/dev/usb/usbwifi.c    |  310 +++++++++++++++++-----------------------------
 sys/dev/usb/usbwifi.h    |  105 +++++++--------
 3 files changed, 186 insertions(+), 352 deletions(-)

diffs (truncated from 1022 to 300 lines):

diff -r f89d72fe1698 -r 3e280b553ced share/man/man9/usbwifi.9
--- a/share/man/man9/usbwifi.9  Tue Jan 18 19:30:49 2022 +0100
+++ b/share/man/man9/usbwifi.9  Tue Feb 08 21:03:01 2022 +0100
@@ -33,46 +33,24 @@
 .Sh SYNOPSIS
 .In dev/usb/usbwifi.h
 .Ss Functions offered by usbwifi.h
-.Ft void
-.Fn usbwifi_set_dying "struct usbwifi *uw" "bool dying"
 .Ft void *
 .Fn usbwifi_softc "struct usbwifi *uw"
+.Ft "struct ieee80211com *"
+.Fn usbwifi_ic "struct usbwifi *"
 .Ft bool
 .Fn usbwifi_isdying "struct usbwifi *uw"
 .Ft void
-.Fn usbwifi_lock_core "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_unlock_core "struct usbwifi *uw"
-.Ft kmutex_t *
-.Fn usbwifi_mutex_core "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_isowned_core "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_busy "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_unbusy "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_lock_rx "struct usbwifi *uw"
+.Fn usbwifi_lock_ic "struct usbwifi *uw"
 .Ft void
-.Fn usbwifi_unlock_rx "struct usbwifi *uw"
+.Fn usbwifi_unlock_ic "struct usbwifi *uw"
 .Ft kmutex_t *
-.Fn usbwifi_mutex_rx "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_isowned_rx "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_lock_tx "struct usbwifi *uw"
+.Fn usbwifi_mutex_ic "struct usbwifi *uw"
 .Ft void
-.Fn usbwifi_unlock_tx "struct usbwifi *uw"
-.Ft kmutex_t *
-.Fn usbwifi_mutex_tx "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_isowned_tx "struct usbwifi *uw"
-.Ft int
-.Fn usbwifi_init_rx_tx "struct usbwifi *uw"
+.Fn usbwifi_isowned_ic "struct usbwifi *uw"
 .Ft void
 .Fn usbwifi_enqueue "struct usbwifi *uw" "uint8_t *buf" "size_t buflen" "int rssi" "int csum_flags" "uint32_t csum_data" "int mbuf_flags"
 .Ft void
-.Fn usbwifi_attach "struct usbwifi *uw" "const char *detname"
+.Fn usbwifi_attach "struct usbwifi *uw"
 .Ft void
 .Fn usbwifi_ic_attach "struct usbwifi *uw" "int num_tx_chains" "int num_rx_chains" "int flags"
 .Ft void
@@ -82,10 +60,6 @@
 .Ft int
 .Fn usbwifi_activate "device_t self" "devact_t act"
 .Ft void
-.Fn usbwifi_stop "struct usbwifi *uw"
-.Ft void
-.Fn usbwifi_start "struct usbwifi *uw"
-.Ft void
 .Fn usbwifi_parent "struct ieee80211com *"
 .Sh DESCRIPTION
 The
@@ -153,22 +127,7 @@ To manage all Rx and Tx chains the
 .Dq uwo_init
 callback of
 .Va struct usbwifi_ops
-should perform any device specific initialization and then call
-.Fn usbwifi_init_rx_tx
-which will allocate chains, set up and open pipes, and start the
-Rx transfers so that packets can arrived.
-These allocations and pipes can be closed and destroyed by calling
-.Fn usbwifi_stop .
-Both of
-.Fn usbwifi_init_rx_tx
-and
-.Fn usbwifi_stop
-must be called with the
-.Nm
-lock held, see
-.Fn usbwifi_lock
-and
-.Fn usbwifi_unlock .
+should perform any device specific initialization.
 See the
 .Sx RECEIVE AND SEND
 section for details on using the chains.
@@ -192,58 +151,32 @@ the network frame up the stack via eithe
 General accessor functions for
 .Fa struct usbwifi :
 .Bl -tag -width 4n
-.It Fn usbwifi_set_dying uw dying
-Set the dying status for this
-.Fa uw
-to
-.Fa dying .
 .It Fn usbwifi_softc uw
 Returns pointer to this
 .Fa uw's
 device softc.
+.It Fn usbwifi_ic uw
+Returns a pointer to the ieee80211com strucuture of this
+.Fa uw .
 .It Fn usbwifi_isdying uw
 Returns true if device is dying (has been pulled or deactivated,
 pending detach.)
 .El
 .Pp
-Reference counting functions for
-.Fa struct usbwifi :
-.Bl -tag -width 4n
-.It Fn usbwifi_busy uw
-Increases the reference count on the driver instance, preventing
-detach from occurring while the driver is blocked accessing the
-device.
-Must be called with the core lock held.
-.It Fn usbwifi_unbusy uw
-Decreases the reference count on the driver instance.
-Once the final reference has been dropped, if a detach event
-is pending, it is allowed to proceed.
-Must be called with the core lock held.
-.El
-.Pp
 Lock handling functions for
 .Fa struct usbwifi :
 .Pp
 .Bl -tag -width 4n -compact
-.It Fn usbwifi_lock_core un
-.It Fn usbwifi_unlock_core un
-.It Fn usbwifi_isowned_core un
-.It Fn usbwifi_lock_rx un
-.It Fn usbwifi_unlock_rx un
-.It Fn usbwifi_isowned_rx un
-.It Fn usbwifi_lock_tx un
-.It Fn usbwifi_unlock_tx un
-.It Fn usbwifi_isowned_tx un
+.It Fn usbwifi_lock_ic un
+.It Fn usbwifi_unlock_ic un
+.It Fn usbwifi_isowned_ic un
 These groups of three functions provide methods to lock,
-unlock, and assert ownership of one of the three locks provided by
-.Nm .
-The three locks are the
-.Dq core
-lock, the
-.Dq Tx
-lock, and the
-.Dq Rx
-lock.
+unlock, and assert ownership of the
+.Dq IEEE80211_LOCK
+of the
+.Vt struct ieee80211com
+in the
+.Vt struct usbwifi ) .
 .El
 .Pp
 Buffer enqueue handling for
@@ -271,11 +204,8 @@ See the
 .Sx AUTOCONFIGURATION
 section for more details about these functions.
 .Bl -tag -width 4n
-.It Fn usbwifi_attach uw detachname
+.It Fn usbwifi_attach uw
 Initial stage attach of a usb wifi device.
-The
-.Fa detachname
-will be used while waiting for final references to drain when detaching.
 .It Fn usbwifi_ic_attach uw num_tx_chains num_rx_chains flags
 Second stage of the usb wifi attachment.
 This will connect the wifi chip to the ieee80211 stack and make the
@@ -318,13 +248,11 @@ structure which contains these members:
 .It Ft void Fn (*uwo_stop) "struct usbwifi *uw"
 Stop interface.
 .Pq Optional .
-Called with the core lock held and with a busy reference.
+Called with the ic lock held and with a busy reference.
 .It Ft void Fn (*uwo_init) "struct usbwifi *uw"
 Initialize (bring up) interface.
 .Pq Required .
-Called with the core lock held.
-Must call
-.Fn usbwifi_rx_tx_init .
+Called with the ic lock held.
 .It Ft void Fn (*uwo_rx_loop) "struct usbwifi *uw" "struct usbwifi_chain *chain" "uint32_t total_len"
 Prepare one or more chain for enqueue.
 .Pq Required .
@@ -456,10 +384,7 @@ will be called with the usbnet lock held
 .Pp
 The
 .Fn uwo_init
-callback both performs device-specific enablement and then calls
-.Fn usbwifi_rx_tx_init ,
-which sets up the receive, transmit, and, optionally, the interrupt
-pipes, as well as starting the receive pipes.
+callback performs device-specific enablement.
 All USB transfer setup is handled internally to the framework, and
 the driver callbacks merely copy data in or out of a chain entry using
 what is typically a device-specific method.
diff -r f89d72fe1698 -r 3e280b553ced sys/dev/usb/usbwifi.c
--- a/sys/dev/usb/usbwifi.c     Tue Jan 18 19:30:49 2022 +0100
+++ b/sys/dev/usb/usbwifi.c     Tue Feb 08 21:03:01 2022 +0100
@@ -57,21 +57,18 @@ struct usbwifi_cdata {
 
 struct usbwifi_private {
        /*
-        * - uwp_core_lock protects most of this structure and the public one.
+        * - IEEE80211_LOCK protects most of this structure and the public one.
         * - uwp_rxlock protects the rx path and its data
         * - uwp_txlock protects the tx path and its data
-        * - uwp_detachcv handles detach vs open references
         *
         * the lock ordering is:
-        *      IFNET_LOCK(ifp) -> IEEE80211_LOCK(&uw->uw_ic) ->
-        *          uwp_core_lock -> uwp_rxlock -> uwp_txlock
-        * - ifnet lock and ic lock are not needed for uwp_core_lock,
-        *   but if either is involved, they must be taken first.
+        *      IFNET_LOCK(ifp) -> IEEE80211_LOCK
+        *          -> uwp_rxlock -> uwp_txlock
+        * - ifnet lock is not needed for any of the locks,
+        *   but if it is involved, it must be taken first.
         */
-       kmutex_t                uwp_core_lock;
        kmutex_t                uwp_rxlock;
        kmutex_t                uwp_txlock;
-       kcondvar_t              uwp_detachcv;
 
        struct usbwifi_cdata    uwp_cdata;
 
@@ -85,15 +82,16 @@ struct usbwifi_private {
        struct usbd_pipe        *uwp_ep[USBWIFI_ENDPT_MAX];
        struct ifqueue          uwp_sendq[WME_NUM_AC];  /* send queues */
 
-       bool                    uwp_dying;
+       volatile bool           uwp_dying;
        bool                    uwp_stopping;
        bool                    uwp_attached;
 
-       int                     uwp_refcnt;
        int                     uwp_timer;
        unsigned short          uwp_if_flags;
        unsigned                uwp_number;
 
+       krndsource_t            uwp_rndsrc;
+
        struct timeval          uwp_rx_notice;
        struct timeval          uwp_tx_notice;
        struct timeval          uwp_intr_notice;
@@ -160,25 +158,26 @@ uwo_stop(struct usbwifi *uw)
                (*uw->uw_ops->uwo_stop)(uw);
 }
 
-static void
+static int
 uwo_init(struct usbwifi *uw)
 {
        if (uw->uw_ops->uwo_init)
-               (*uw->uw_ops->uwo_init)(uw);
+               return (*uw->uw_ops->uwo_init)(uw);
+       return ENXIO;
 }
 
 static unsigned
 uwo_tx_prepare(struct usbwifi *uw, struct usbwifi_chain *c, uint8_t qid)
 {
-       usbwifi_isowned_tx(uw);
+       KASSERT(mutex_owned(&uw->uw_pri->uwp_txlock));
        return (*uw->uw_ops->uwo_tx_prepare)(uw, c, qid);
 }
 
 static void
-uwo_rx_loop(struct usbwifi *un, struct usbwifi_chain *c, uint32_t total_len)
+uwo_rx_loop(struct usbwifi *uw, struct usbwifi_chain *c, uint32_t total_len)
 {
-       usbwifi_isowned_rx(un);
-       (*un->uw_ops->uwo_rx_loop)(un, c, total_len);
+       KASSERT(mutex_owned(&uw->uw_pri->uwp_rxlock));
+       (*uw->uw_ops->uwo_rx_loop)(uw, c, total_len);
 }
 
 static void
@@ -230,24 +229,24 @@ usbwifi_newbuf(size_t buflen)
  * enqueue a packet for higher levels.
  */
 void
-usbwifi_enqueue(struct usbwifi * const un, uint8_t *buf, size_t buflen,
+usbwifi_enqueue(struct usbwifi * const uw, uint8_t *buf, size_t buflen,
               int rssi, int csum_flags, uint32_t csum_data, int mbuf_flags)
 {
        USBWIFIHIST_FUNC();
-       struct usbwifi_private * const unp __unused = un->uw_pri;
+       struct usbwifi_private * const uwp __unused = uw->uw_pri;
        struct mbuf *m;
-       struct ieee80211com *ic = &un->uw_ic;
+       struct ieee80211com *ic = &uw->uw_ic;



Home | Main Index | Thread Index | Old Index