Home | History | Annotate | Download | only in usb
History log of /src/sys/dev/usb/usbnet.c
RevisionDateAuthorComments
 1.121  10-Nov-2024  mlelstv Add MBUFTRACE
 1.120  04-May-2024  mlelstv branches: 1.120.2;
Use device_printf instead of autoconf messages for errors.
 1.119  02-Feb-2024  andvar fix various typos in comments.
 1.118  09-Oct-2023  riastradh usbnet(9): Fix typo in comment.

No functional change intended.

PR kern/57645

XXX pullup-10
 1.117  09-Oct-2023  riastradh usbnet(9): On if_init, stop/init if IFF_RUNNING -- not noop.

ether_ioctl(9) relies on this to reinitialize an interface when a
flags change returns ENETRESET. We can't just reprogram the hardware
multicast filter because some drivers have logic in if_init that's
conditional on IFF_PROMISC; perhaps we can reduce the cost of this if
we can change those drivers to do it in uno_mcast but that requires
some analysis to determine.

PR kern/57645

XXX pullup-10
 1.116  09-Oct-2023  riastradh usbnet(9): Fix sense of conditional in usbnet_ifflags_cb.

This appears to have been mistranscribed in revision 1.1 of usbnet.c.

PR kern/57645

XXX pullup-10
 1.115  09-Oct-2023  riastradh usbnet(9): Make sure unp->unp_if_flags is initialized on init.

usbnet_ifflags_cb is only called if the flags change while up and
running. (XXX Maybe it should be called in other circumstances too
so there's only one path here?)

Out of paranoia, clear the cache on stop.

PR kern/57645

XXX pullup-10
 1.114  15-Jul-2023  andvar fix tripple "p" (ppp) to double "p" (pp) in some words in comments.
 1.113  22-Sep-2022  riastradh branches: 1.113.4;
usbnet(9): Omit needless miilock around uno_stop.

This time for real!
 1.112  20-Sep-2022  mrg revert rev 1.111 (which was 1.106 again, without the 1.107 changes).

fixes an assert reported by msaitoh@. also fix another missing miilock
assert in usbnet_stop() that triggered for me.
 1.111  13-Sep-2022  riastradh usbnet(9): Call mii_down once we've finished with mii_tick.
 1.110  23-Aug-2022  riastradh usbnet(9): Don't touch ifp->if_flags in usbnet_start_locked.

Instead, consult unp->unp_txstopped -- but the caller already
guarantees it is not stopped, so turn the conditional into an
assertion anyway.
 1.109  20-Aug-2022  riastradh usbnet(9): New usbnet_ispromisc(un).

Replaces ifp->if_flags & IFF_PROMISC in multicast filter updates.
 1.108  20-Aug-2022  riastradh usbnet(9): Rename core lock -> mii lock.

No functional change intended.
 1.107  20-Aug-2022  riastradh usbnet(9): Limit scope of core lock to mii and tick scheduling.

Bringing the interface up or down is serialized by IFNET_LOCK, and we
prevent further mii callbacks with mii_down, so there's no need for
another lock to serialize uno_init, uno_stop, and the mii callbacks.
 1.106  20-Aug-2022  riastradh usbnet(9): Call mii_down once we've finished with mii_tick.
 1.105  20-Aug-2022  riastradh usbnet(9): Simplify core lock use in usbnet_tick_task.
 1.104  20-Aug-2022  riastradh usbnet(9): Call uno_tick before mii stuff.

The one driver that uses it, cue(4), uses it just for statistics
gathering; hard to imagine that order could be important here. But
this will allow for some simplification of the surrounding code.
 1.103  20-Aug-2022  riastradh usbnet(9): Assert core lock is held on usbnet_set_link.

This is only allowed to be called via the uno_statchg callback, which
in turn is called only with the core lock held. (usbnet_set_link is
also called internally in usbnet(9) with the core lock held.)
 1.102  20-Aug-2022  riastradh usbnet(9): Split unp_stopping into stopped/txstopped/rxstopped.

In practical terms this could be done with one variable and an atomic
store, but serializing all access with a lock makes reasoning easier,
and the locks have to be taken by the logic that queries the
variables anyway, and the variables are set only under heavy-weight
configuration changes anyway.

What this accomplishes is disentangling lock order between rxlock and
txlock: they are never taken at the same time, so no order is needed.

I renamed unp_stopping to unp_stopped for a compiler-assisted audit
to make sure I reviewed every case of it.
 1.101  20-Aug-2022  riastradh usbnet(9): Don't touch unp_stopping in usbnet_pipe_intr.

This access was unprotected by a lock, but it's not necessary anyway:
usbnet_stop aborts the pipes, and the xfer doesn't call usbd_transfer
to reschedule itself -- it's an intr pipe, so it's rescheduled
internally by usbdi(9) in a way that usbd_abort_pipe atomically
prevents.
 1.100  20-Aug-2022  riastradh usbnet(9): Omit needless un->un_intr test in usbnet_pipe_intr.

un->un_intr can't change after attach, and we don't open the pipe if
it's null. So no need to test it.
 1.99  20-Aug-2022  riastradh usbnet(9): Revert previous -- usbnet_media_upd does have IFNET_LOCK.

Not sure why I thought otherwise.
 1.98  20-Aug-2022  riastradh usbnet(9): Avoid ether_mediachange if stopped.

We are called without IFNET_LOCK held here, so touching ifp->if_flags
is forbidden, but that's the first thing ether_mediachange does.

XXX not right either, need to eliminate the check from
ether_mediachange
 1.97  16-Aug-2022  riastradh usbnet(9): Omit needless usbnet_core_mutex function.

While here, use inline, not __inline__, since this is not a header
file where inline might be redefined by the user.
 1.96  12-Aug-2022  riastradh usbnet(9): Fix mbuf alignment and narrow bounds check.

In usbnet.c rev. 1.16, usbnet_newbuf was first passed a buffer length
to verify it fits within MCLBYTES. It also changed m_adj to go
before, not after, setting m_len and m_pkthdr.len -- which had the
effect of making the m_adj a no-op, because after MGETHDR the mbuf
has zero length and m_adj stops at the length of the mbuf, so nothing
was aligned as intended.

To make this aligned as intended, we require the buffer length to be
_below_ MCLBYTES, by ETHER_ALIGN, so there's room for the ethernet
header in a maximum-length payload. Once we do that, it is safe to
initialize m_len = m_pkthdr.len = ETHER_ALIGN + buflen, which is
below the actual size of the mbuf (MHLEN or MCLBYTES, depending), and
_then_ do m_adj to align the pointer.
 1.95  07-Aug-2022  riastradh usbnet(9): Simplify assertions now that urndis(4) is less sketchy.
 1.94  05-Mar-2022  riastradh usbnet(9): uno_init is now optional.

Update assertion and man page accordingly.
 1.93  03-Mar-2022  riastradh usb: usbd_close_pipe never fails. Make it return void.

Prune dead branches as a result of this change.
 1.92  03-Mar-2022  riastradh usb: usbd_abort_pipe never fails. Make it return void.

Prune dead branches as a result of this change.
 1.91  03-Mar-2022  riastradh usbnet: On if_stop, abort xfers before resetting hardware.

uno_stop is supposed to have exclusive access to the hardware; this
ensures that any concurrent uno_rx_loop has completed before we enter
uno_stop.
 1.90  03-Mar-2022  riastradh usbnet: Omit needless detachcv name parameter to usbnet_attach.
 1.89  03-Mar-2022  riastradh usbnet: Omit empty uno_init functions.
 1.88  03-Mar-2022  riastradh usbnet: Factor usbnet_init_rx_tx out into usbnet_if_init.

Make it private; no need for drivers to call it any more.
 1.87  03-Mar-2022  riastradh usbnet: Handle usbnet_set_link for drivers with no media detect.
 1.86  03-Mar-2022  riastradh usbnet drivers: From *_uno_init, call *_uno_stop, not usbnet_stop.

Make usbnet_stop private now that no drivers use it.

None of the driver-independent logic in usbnet_stop has any effect at
this point because we are guaranteed not to be running, so only the
driver-dependent logic in *_uno_stop (at most) is needed.

For drivers with no *_uno_stop, just omit the call to usbnet_stop
altogether.

Some of this logic is obviously redundant with the subsequent call to
*_reset -- to be addressed in a subsequent commit.
 1.85  03-Mar-2022  riastradh usbnet: Do nothing on if_init/stop if already in the target state.

The network stack _shouldn't_ ever call us if so, but I'm not yet
sure it _won't_.
 1.84  03-Mar-2022  riastradh usbnet: Delete the core lock from the API.

Init/stop and ioctl happen under IFNET_LOCK. Multicast updates only
happen after init and before stop. Core lock is no longer a relevant
part of the API. Internally, it serves essentially just to lock out
asynchronous mii activity during init/stop.
 1.83  03-Mar-2022  riastradh usbnet: Make usbnet_mii_readreg/writereg/statchg private to usbnet.c.

No drivers need to use these.
 1.82  03-Mar-2022  riastradh usbnet: Apply hardware multicast filter updates synchronously again.

To make this work:

1. Do it only under a new lock, unp_mcastlock. This lock lives at
IPL_SOFTCLOCK so it can be taken from network stack callouts. It
is forbidden to acquire the usbnet core lock under unp_mcastlock.

2. Do it only after usbnet_init_rx_tx and before usbnet_stop; if
issued at any other time, drop the update on the floor.

3. Make usbnet_init_rx_tx apply any pending multicast filter updates
under the lock before setting the flag that allows SIOCADDMULTI or
SIOCDELMULTI to apply the updates.

4. Remove core lock asserts from various drivers' register access
routines. This is necessary because the multicast filter updates
are done with register reads/writes, but _cannot_ take the core
lock when the caller holds softnet_lock.

This now programs the hardware multicast filter redundantly in many
drivers which already explicitly call *_uno_mcast from the *_uno_init
routines. This is probably harmless, but it will likely be better to
remove the explicit calls.
 1.81  03-Mar-2022  riastradh usbnet: Take the core lock around uno_mcast.

Every driver does this already. This will enable us to change the
lock that serializes access to the registers so we can go back to
doing this synchronously in SIOCADDMULTI/SIOCDELMULTI.
 1.80  03-Mar-2022  riastradh usbnet: No need for the core lock in usbnet_ifflags_cb.

The only state this touches is unp_if_flags, and all paths touching
it also hold IFNET_LOCK -- not to mention this is the only path that
touches unp_if_flags in the first place!
 1.79  03-Mar-2022  riastradh usbnet: Make the tx/rx locks private to usbnet.c.

Suffice it for the drivers to know that uno_tx_prepare and
uno_rx_loop have exclusive access to the chain, and, for tx,
exclusive access to the mbuf.
 1.78  03-Mar-2022  riastradh usbnet: usbnet_busy is no longer referenced; release it!
 1.77  03-Mar-2022  riastradh usbnet: No need for usbnet_busy in mii callbacks.

After mii_detach, these have all completed and no new ones can be
made, and detach doesn't start destroying anything until after
mii_detach has returned, so there is no need to hang onto a reference
count here.
 1.76  03-Mar-2022  riastradh usbnet: No need for usbnet_busy in usbnet_init_rx_tx or usbnet_stop.

These run with IFNET_LOCK held, and the interface cannot be detached
until the IFNET_LOCK is released, so there is no need to hang onto a
reference count here.
 1.75  03-Mar-2022  riastradh usbnet: Split multicast filter reprogramming into separate operation.
 1.74  03-Mar-2022  riastradh usbnet: Omit needless locking around usbnet_isdying.

Now that is tested and set with atomic_load/store, there is no need
to hold the lock -- which means we can set it while the core lock is
held during, e.g., a reset sequence, and use that to interrupt the
sequence so it doesn't get stuck waiting to time out when the device
is physically removed.
 1.73  03-Mar-2022  riastradh usbnet: Use atomic_load/store_relaxed for unp_dying.

This way we don't need to hold the core lock to avoid upsetting
sanitizers (which probably find the current code upsetting), and we
can use it to exit early from timeout loops that run under the core
lock (which is probably not necessary for them to do anyway, but
let's worry about that later).
 1.72  03-Mar-2022  riastradh usbnet: Print diagnostic about refcnt stragglers.

I don't think there can be any, but this message, if printed, would
falsify my hypothesis!
 1.71  03-Mar-2022  riastradh usbnet: Enter uno_init with the core lock held.

This reduces code in all drivers except urndis(4) and aue(4).

However, it's still safe for urndis to drop the core lock because the
ifnet is locked, and the ifnet lock covers the DOWN->UP (uno_init)
and UP->DOWN (uno_stop) transitions.
 1.70  03-Mar-2022  riastradh usbnet: Assert ioctl locking.
 1.69  03-Mar-2022  riastradh usbnet: Impart blame on whose ifnet is unlocked in uno_init.
 1.68  03-Mar-2022  riastradh usbnet: Don't waste time calling uno_stop if device is detaching.

The hardware is most likely gone, so trying to write to its registers
(and, in some cases, wait until a timeout for a device to reset) is a
waste of time. Even if it was detached only in software with drvctl,
reattaching it will reset the device anyway.
 1.67  03-Mar-2022  riastradh usbnet: Avoid IFNET_LOCK on detach if we never attached the ifp.
 1.66  03-Mar-2022  riastradh usbnet: Clear watchdog timer before stopping hardware.

No need to take the lock again -- which might not be necessary
because the callout and task have completed, but let's obviate the
need to think about that.
 1.65  03-Mar-2022  riastradh usbnet: Omit needless locking/busying/testing in usbnet_tick_task.

usbnet_stop waits for the task to complete before resetting the
hardware, and usbnet_detach waits for usbnet_stop to complete before
destroying anything, so there's no need for any of this.
 1.64  03-Mar-2022  riastradh usbnet: Omit needless tests in usbnet_tick.

It's harmless for us to schedule the tick task even if unp_dying or
unp_stopping is set by now, because usbnet_stop will just wait for it
to finish anyway, and the callout can't be scheduled again until the
interface is done stopping and is brought back up again.

No need for unp == NULL test -- un->un_pri is initialized well before
this callout can be scheduled, and is nulled out only at the end of
usbnet_detach, at which point we have already halted this callout.
 1.63  03-Mar-2022  riastradh usbnet: Uncomment and fix assertion for ifp->if_flags |= IFF_RUNNING.

We always hold IFNET_LOCK for ioctls that end up here -- the ones
that don't hold it are only SIOCADDMULTI/SIOCDELMULTI, which don't
end up here. However, urndis(4) throws a spanner in the works by
doing weird device initialization.
 1.62  03-Mar-2022  riastradh usbnet: Don't issue a detach event if we never issued an attach one.
 1.61  03-Mar-2022  riastradh usbnet: Make detach order reverse attach order, for unp_stat_ch.

No functional change intended.
 1.60  03-Mar-2022  riastradh usbnet: Detach interface and mii before waiting for refcnt to drain.

All outstanding software activity under usbnet's control -- which is
all that participates in the refcnting -- should be quiesced by
stopping and detaching everything.
 1.59  03-Mar-2022  riastradh usbnet: Omit needless callout_halt and usb_rem_task_wait.

The callout and tasks cannot be pending at this point -- it is a bug
if usbnet_if_stop failed to quiesce everything, so turn these into
KASSERTs.
 1.58  03-Mar-2022  riastradh usbnet: Refuse to bring interfaces back up once dying.

Make this happen uniformly across all usbnet drivers, not on a
per-driver basis.

This ensures new activity on the interface can't happen by the time
we have stopped existing activity and waited for it to complete.
 1.57  03-Mar-2022  riastradh usbnet: Assert IFNET_LOCKED in usbnet_media_upd.

This ensures, if the device is being initialized or stopped,
usbnet_media_upd will not run until it's done, so the reset sequence
has exclusive access to the device registers used by mii.
 1.56  03-Mar-2022  riastradh usbnet: Fix ordering of actions in usbnet_stop.

Make sure all software activity is quiescent (callouts and tasks,
including ifmedia and mii callbacks -- anything that might trigger
register access) before asking the driver to stop the hardware. This
way, the driver uno_stop routine is guaranteed exclusive access to
the registers.

This will also enable us to simplify the callouts and tasks so they
don't have to check the software state -- to be done in a separate
commit.
 1.55  03-Mar-2022  riastradh usbnet: Remove usbnet_set_dying.

Not necessary for the one caller that did it (url(4)): usbnet_detach
handles failed attach just fine without it.
 1.54  03-Mar-2022  riastradh usbnet: Omit needless unp == NULL test in usbnet_tick_task.

The task is never scheduled until after un->un_pri is initialized,
and un->un_pri isn't nulled until after the callout and task are
quiescent.
 1.53  03-Mar-2022  riastradh usbnet: Don't check if_flags for IFF_RUNNING in usbnet_pipe_intr.

The one user of this interface in tree, aue(4), doesn't care --
if_statinc is safe whether IFF_RUNNING or not.
 1.52  03-Mar-2022  riastradh usbnet: Don't check if_flags for IFF_RUNNING in usbnet_rxeof.

This can only run after we start the pipes in usbnet_init_rx_tx, and
before we abort the pipes in usbnet_stop, during which time if_flags
& IFF_RUNNING is stably set.
 1.51  03-Mar-2022  riastradh usbnet: Assert IFNET_LOCKED in usbnet_init_rx_tx, usbnet_stop.

Exception: urndis(4) abuses this API to start this logic before the
ifp is actually initialized. So for the sake of urndis(4), until
sense can be beaten into it, allow the !unp_ifp_attached case to run
without IFNET_LOCK.
 1.50  03-Mar-2022  riastradh usbnet: Assert IFNET_LOCKED on if_flags change callbacks.

- if_init
- if_stop
- ethersubr(9) ifflags_cb
 1.49  03-Mar-2022  riastradh usbnet: Ensure access to unp_timer is protected by unp_txlock.
 1.48  03-Mar-2022  riastradh usbnet: Ensure ifp->if_softc is initialized _before_ publishing ifp.

Otherwise other parts of the system might start using ifp the moment
we if_register it, and trip over null if_softc.
 1.47  03-Mar-2022  riastradh usbnet: Take IFNET_LOCK around access to if_flags in usbnet_detach.

This is not stable without IFNET_LOCK. Extraneous calls to
usbnet_stop arising from this race might be harmless, but let's
render it unnecessary to even think about that.
 1.46  03-Mar-2022  riastradh usbnet: Set and clear IFF_RUNNING slightly earlier and later.

- Set IFF_RUNNING before any calls to usbnet_rxeof are possible.
- Don't clear IFF_RUNNING until all transfers have been aborted.
 1.45  03-Mar-2022  riastradh usbnet: Simplify usbnet_isdying.

usbnet_detach (or its caller) stops all users before it returns.

If un->un_pri is null at this point, there's a bug -- something
didn't wait for everything to finish before calling usbnet_detach.
 1.44  29-Jan-2022  riastradh usbnet: Defer hardware multicast filter updates to USB task.

Breaks deadlock:

- usbnet_detach holds usbnet lock, awaits kpause in ure_reset
- callout holds softclock `lock' (sequential softints, blocks kpause
wakeup), awaits softnet_lock in tcp_timer_keep, frag6_fasttimo, &c.
- soclose holds softnet_lock, awaits usbnet lock in SIOCDELMULTI

This change breaks the deadlock by not passing the SIOCADDMULTI or
SIOCDELMULTI ioctl synchronously to the driver, which typically takes
the usbnet lock.

With this change, the ethernet layer still maintains the list of
multicast addresses synchronously, but we defer the driver logic that
updates the hardware multicast filter to an asynchronous USB task
without softnet_lock held.

This doesn't cause exactly the same ioctl to be sent to the driver --
usbnet just sends SIOCDELMULTI with an all-zero struct ifreq, and
might drop some ioctls if issued in quick succession. This is OK
because none of the drivers actually distinguish between SIOCADDMULTI
and SIOCDELMULTI, or examine the argument; the drivers just commit
whatever multicast addresses are listed in the ethercom.

Other than the different ioctl submitted, there is no change to the
ABI or locking scheme of usbnet, so this is safe to pull up to
netbsd-9. This means we unfortunately can't guarantee that if a
process issues SIOCADDMULTI and then sendto, the multicast filter
update will be done by the time of the sendto -- and, more
importantly, the packets received in reply to it. But failing to
guarantee that is better than deadlocking! Later changes on HEAD
will restore the synchronous multicast filter updates with much more
extensive ABI changes and API simplifications in usbnet(9).

Proposed on tech-net:
https://mail-index.netbsd.org/tech-net/2021/12/30/msg008164.html

XXX pullup-9
 1.43  11-Dec-2021  mrg remove clause 3 from all my licenses that aren't conflicting with
another copyright claim line. again. (i did this in 2008 and then
did not update all of my personal templates.)
 1.42  16-Jun-2021  riastradh if_attach and if_initialize cannot fail, don't test return value

These were originally made failable back in 2017 when if_initialize
allocated a softint in every interface for link state changes, so
that it could fail gracefully instead of panicking:

https://mail-index.NetBSD.org/source-changes/2017/10/23/msg089053.html

However, this spawned many seldom- or never-tested error branches,
which are risky to have around. And that softint in every interface
has since been replaced by a single global workqueue, because link
state changes require thread context but not low latency or high
throughput:

https://mail-index.NetBSD.org/source-changes/2020/02/06/msg113759.html

So there is no longer any reason for if_initialize to fail. (The
subroutine if_stats_init can't fail because percpu_alloc can't fail
either.)

There is a snag: the softint_establish in if_percpuq_create could
fail, potentially leading to bad consequences later on trying to use
the softint. This change doesn't introduce any new bugs because of
the snag -- if_percpuq_attach was already broken. However, the snag
can be better addressed without spawning error branches, either by
using a single softint or making softints less scarce.

(Separate commit will change the signatures of if_attach and
if_initialize to return void, scheduled to ride whatever is the next
convenient kernel bump.)

Patch and testing on amd64 and evbmips64-eb by maya@; commit message
soliloquy, and compile-testing on evbppc/i386/earmv7hf, by me.
 1.41  25-Apr-2021  rin branches: 1.41.2;
Sample # of TX packets as entropy source.

For RX packets, individual drivers need to be modified.
 1.40  01-Mar-2021  jakllsch reduce aprint_error(9) abuse
 1.39  28-Aug-2020  riastradh branches: 1.39.2;
usbnet: Reject buflen>MCLBYTES in usbnet_newbuf.
 1.38  15-Mar-2020  thorpej branches: 1.38.4;
Define and implement a locking protocol for the ifmedia / mii layers:
- MP-safe drivers provide a mutex to ifmedia that is used to serialize
access to media-related structures / hardware regsiters. Converted
drivers use the new ifmedia_init_with_lock() function for this. The
new name is provided to ease the transition.
- Un-converted drivers continue to call ifmedia_init(), which will supply
a compatibility lock to be used instead. Several media-related entry
points must be aware of this compatibility lock, and are able to acquire
it recursively a limited number of times, if needed. This is a SPIN
mutex with priority IPL_NET.
- This same lock is used to serialize access to PHY registers and other
MII-related data structures.

The PHY drivers are modified to acquire and release the lock, as needed,
and assert the lock is held as a diagnostic aid.

The "usbnet" framework has had an overhaul of its internal locking
protocols to fit in with the media / mii changes, and the drivers adapted.

USB wifi drivers have been changed to provide their own adaptive mutex
to the ifmedia later via a new ieee80211_media_init_with_lock() function.
This is required because the USB drivers need an adaptive mutex.

Besised "usbnet", a few other drivers are converted: vmx, wm, ixgbe / ixv.

mcx also now calls ifmedia_init_with_lock() because it needs to also use
an adaptive mutex. The mcx driver still needs to be fully converted to
NET_MPSAFE.
 1.37  14-Mar-2020  christos fix more broken kernhist formats (now I got them all).
 1.36  13-Mar-2020  christos PR/55068: sc.dying: Fix printf formats:
- no %s/%p for kernel log
- 0x% -> %#
- always %j for kernel log
 1.35  04-Feb-2020  thorpej Use ifmedia_fini().
 1.34  29-Jan-2020  thorpej Adopt <net/if_stats.h>.
 1.33  14-Dec-2019  maya branches: 1.33.2;
Don't assume mii is not null here. Some drivers like urndis don't use
mii, so they always have mii == NULL.

ok riastradh. fixes PR kern/54762
 1.32  03-Dec-2019  riastradh Fix inequality for refcnt drain: -1 here means all refs gone.
 1.31  03-Dec-2019  riastradh Fix order of nulling un->un_pri->unp_ec.ec_mii.

Can't null it until after if_detach prevents further use.

While here, fix conditionals in usbnet_tick_task to use the unp_dying
flag, not the nullness of mii (or of ifp, which never null because
it's an embedded member).
 1.30  06-Nov-2019  mrg add a note about ifnet lock vs usbnet locks.
 1.29  13-Sep-2019  msaitoh if_flags is neither int nor short. It's unsigned short.
 1.28  09-Sep-2019  mrg document usbnet_private locking. minor knf.
 1.27  08-Sep-2019  mrg normalise an error message.
 1.26  08-Sep-2019  mrg - use CALLARGS vs CALLED for better usbhist
- turn off usbnetdebug default
- log for all entry/exit points of usbnet_pipe_intr()
- in usbnet_start_locked() track whether any packet has been
transmitted for setting the timer. avoids spurious
"watchdog timeouts"
- in usbnet_stop() use callout_halt() vs callout_halt, and
also stop the usb task. fixes crash of usbtask after the
phy has detached.
- add a little more defensive checking in the tick task, and
add some high-log-level logs.
- in usbnet_detach() move the call to usbnet_stop_ifp() above
the calls to callout/usbtask stopping.
- set ec_mii and unp_pri to NULL when freeing their data
 1.25  29-Aug-2019  mrg branches: 1.25.2;
fix a lock hang reported by sc.dying in PR#54495.

remove locking in usbnet_tick(). assume that all locking
needs are handled inside usbnet_tick_task(), which runs in
the usbtask thread. ensure that usbnet private is valid
before using it.

also check NULL private pointer in usbnet_isdying().
all the other cases should never happen.
 1.24  28-Aug-2019  mrg in usbnet_detach(), check both that the private pointer has
been allocated and that unp_attached is true before trying
to access anything else.

fixes a detach problem noticed by maxv when, eg, the call
to usbd_set_config_no() fails and attach bails early.
 1.23  23-Aug-2019  mrg - remove unused uncd_rx_cnt
- add USBNETHIST_CALLARGSN and use it in many places
- add more history logs
- add a log if watchdog abort leaves uncd_tx_cnt non zero
- add a unique (32-bit) number for a each usbnet device and
use it for logging
- explicitly check for full tx ring in usbnet_start_locked()
and return early, which avoids setting the 5 second timer
and triggering a watchdog
- reset uncd_tx_prod and uncd_tx_cnt to zero in stop
 1.22  20-Aug-2019  mrg couple more changes to usbnet(9):

- MII read/write reg return int instead of usbd_status (requested by skrll)
- usbnet_attach_ifp(9) changes arg, two mii-specific flags are placed by a
pointer to new struct usbnet_mii. if not NULL, then attach an MII to this
interface like previous have_mii parameter. use this to allow ure(4) to
properly pass PHY location to mii_attach().

welcome netbsd 9.99.10.
 1.21  20-Aug-2019  mrg properly fix the kernhist calls.

one must cast and cast and cast and maybe a fish will be caught...
 1.20  19-Aug-2019  mrg move the check against un_phyno from usbnet back into the drivers
that do this (axe, axen, mue, smsc, ure.) it made mii scanning
only work for phy 0, and aue needs it for at least one device.

fix smsc to return usbd_status not -1 on failure. XXX smsc was
writing to '*val' even in error cases, it does not now.

remove a double call to IFQ_SET_READY() (noticed by chuq).

avoid unlock+instant relock by using usbnet_lock_mii_un_locked().
 1.19  19-Aug-2019  mrg add USBHIST_CALLARGS() frontend to KERNHIST_CALLARGS().
add USBNETHIST_CALLARGS() frontend to USBHIST_CALLARGS().

use both in read/write reg, instead of aprint.

use %jx and (uintptr_t) and fix the 32 bit debug build.
 1.18  18-Aug-2019  mrg fix a think-o in the previous: don't return when saving the first error.
from skrll@.
 1.17  18-Aug-2019  mrg update usbnet slight:
- drivers that want to use if_input() will also set _if_input. for
now, avoid attaching a per-cpu queue for them. use if_initialize()
and if_register().
- when stopping pipes, don't give up after the first failure, but
keep the first failure error for return and keep going
- if 0 a KASSERT() in usbnet_init_rx_tx(). there's a path via
if_mcast_op() that can have the ifnet unlocked today..
- in usbnet_watchdog(), abort the pipe instead of faking tx
completion. avoids issues with devices with more than one tx
descriptor, as well as avoiding triggering usb asserts.

with these, upl(4) port to usbnet(9) now works. (would be a version
bump, but upl(4) and the unported umb(4) are the only consumers that
would care.)
 1.16  16-Aug-2019  mrg internal to usbnet:

add buflen param to usbnet_newbuf(). use this to skip allocating
an mbuf cluster for small packets (ported from kue(4).)

remove usbnet_rx_start_pipes()'s always usbnet_rxeof argument.
 1.15  15-Aug-2019  mrg - usbnet_rx_loop_cb's usbd_xfer parameter is never used and available
in the usbnet_chain if needed. remove it
- usbnet media status change already set link to false, don't repeat
this in every driver
- don't clear link in stop, nothing was re-enabling it for non-MII
- add optional uno_tick_cb(struct usbnet *un) that is called from the
usbnet tick timer
- remove wrong debug sysctl prototype

rx_loop and timer are kernel versions changes, but hopefully this is
the last one for usbnet. working with 3 more drivers now (cue, mue
and url), leaving only aue, kue, upl and umb undone (aue may work
with previously supported devices, mine doesn't work with our driver,
kue and upl have patches for testing and umb is undone.)
 1.14  14-Aug-2019  mrg introduce usbnet_set_dying(). will be used by url(4) conversion.
bump version.

introduce USBNET_MODULE() that encompasses almost all the module
specific code for usbnet modules. they still need to include
the relevant ioconf.c, but everything else is now just, eg,

USBNET_MODULE(axen)
 1.13  11-Aug-2019  mrg don't set dv_private. it's a bad pattern and only didn't
trip kmem_free() lossage because struct usbnet is at the
start of the softc.

for now, enforce this as part of the ABI.

catch up urndis with tx_prepare checking buffer length,
and also add an assert to usbnet_start_locked() to match.
 1.12  11-Aug-2019  mrg - use usbnet_isowned*() more
- more const
 1.11  10-Aug-2019  mrg reduce the scope of struct usbnet:
- move a large number of members internal to usbnet.c's new
"struct usbnet_private".
- provide accessors for a few of these
- move struct usbnet_cdata into usbnet.c as well, but move
bufsz, list count, and xfer flags back out into struct usbnet,
and have them set as part of the setup efore usbnet_attach()
- split the intr pipe parts into their own structure
- move all the main usbnet*lock* code into usbnet.c too

usbnet_attach() goes down to 2 args, and the inputs needed are
now the full contents of 'struct usbnet' besides the driver
owned 'un_flags' and usbnet owned 'un_pri'.

welcome netbsd 9.99.6.
 1.10  09-Aug-2019  mrg update usbnet some:
- move rx/tx xfer flags into usbnet_cdata
- move the callbacks into usbnet_ops structure
- move rx/tx xfer flags arguments from usbnet_init_rx_tx()
and move them all into usbnet_attach() arguments
- s/miibus/mii/ in some places for consistency

other clean up:
- create wrapper functions for callbacks, move knowledge about
special handling (OK to be missing, error eating) there.
- use cdata pointer if already available
- provide some more macros (will be real functions later) for
accessing usbnet members, use existing ones more

bump kernel version.
 1.9  07-Aug-2019  maya Don't tell userland about ENETRESET even if a usbnet driver didn't define
un_ioctl_cb.
 1.8  07-Aug-2019  mrg fix two issues, found by maya@ while testing urndis:
- avoid an KASSERT() in usbnet_rx_tx_init() when called early
- move the pmf and usb driver event calls out of the mii specific code,
now suspend is able to work on them again.

urndis port to usbnet works with additional ipv6 lossage that
may indicate it doesn't work (not commited yet.)
 1.7  07-Aug-2019  pgoyette Many years ago someone created a new __link_set_sysctl_funcs to hold
the list of routines that need to be called for setting up sysctl
variables. This worked great for all code included in the kernel
itself, but didn't deal with modules that want to create their own
sysctl data. So, we ended up with a lot of #ifdef _MODULE blocks
so modules could explicitly call their setup functions when loaded
as non-built-in modules.

So today, we complete the task that was started so many years ago.

When modules are loaded, after we've called xxx_modcmd(INIT...) we
check if the module contains its own __link_set_sysctl_funcs, and
if so we call the functions listed. We add a struct sysctllog member
to the struct module so we can call sysctl_teardown() when the module
gets unloaded. (The sequence of events ensures that the sysctl stuff
doesn't get created until the rest of the module's init code does any
required memory allocation.)

So, no more need to explicitly call the sysctl setup routines when
built as a loadable module.
 1.6  06-Aug-2019  mrg for ethernet usb network devices, print the mac addr in usbnet.
 1.5  06-Aug-2019  mrg extend usbnet to cope with if_upl, if_smsc, and if_umb needs:

- usbnet_enqueue() can set mbuf flags and csum_data
- usbnet_input() for non-ethernet based devices (upl, umb)
- allow a complete override for ioctl()
- remove converted list -- we have compiling and/or working patches for
all the devices except for umb(4), will be merged as testing happens

hopefully this is the last ABI change, though it may end up being
extended for additional smsc(4) support.


hello for real netbsd 9.99.3!
 1.4  04-Aug-2019  mrg - adjust usbnet interface to allow usbd_open_pipe_intr(), from the
new comment:
* if un_intr_buf is not NULL, use usbd_open_pipe_intr() not
* usbd_open_pipe() for USBNET_ENDPT_INTR, with this buffer,
* size, and interval.
the standard handling is in usbnet.c, with a callback to deal with
the interrupt it self. not fully tested, designed for if_aue.c
and a few others not yet converted.
- make usbhist for usbnet.c work, thanks paulg
- usbnet_init_rx_tx() clears out all allocations upon failure now
- add usbnet_ec() to get a pointer to the struct ethercom
- add usbnet_{lock,unlock,owned}*() to lock/unlock the various locks
and *owned*() for asserting

welcome 9.99.3!
 1.3  03-Aug-2019  skrll Trailing white space
 1.2  31-Jul-2019  mrg couple of minor API updates:
- change the read/write register callbacks to have the same phy/reg
order as the MII code.
- add "mii_flags" param to usbnet_attach_ifp(). axe(4) wants it.

also:
- add usbnet debug code, sysctl node support
- remove commented DPRINTF()s accidentally left in place
- add usbnet_softc()
- reorder some attach code to be consistent
- re-add USBD_FORCE_SHORT_XFER for axen rx chain

ride 9.99.2 bump.
 1.1  31-Jul-2019  mrg introduce a library of common code / backends to share code between
USB ethernet drivers.

usbnet.h introduces a new set of APIs to provide common solutions
for these driver features:
- USB endpoint pipe handling
- rx and tx chain handling
- generic handlers or support for several struct ifnet callbacks
- MII bus locking
- interrupt handling
- partial autoconf handling: much of attach, and detach/activate
can use common versions directly.

currently, only axen(4) and cdce(4) are converted. the reductions
in these drivers are quite significant: if_cdce.c is reduced from
1000 lines to 320 lines, and if_axen is reduced from 1902 lines
to 1021 lines.

add a "usbnet" module and make the if_axen module depend upon it.
 1.25.2.6  31-Jan-2022  martin Pull up following revision(s) (requested by riastradh in ticket #1415):

sys/dev/usb/usbnet.c: revision 1.44 (via patch)

usbnet: Defer hardware multicast filter updates to USB task.

Breaks deadlock:
- usbnet_detach holds usbnet lock, awaits kpause in ure_reset
- callout holds softclock `lock' (sequential softints, blocks kpause
wakeup), awaits softnet_lock in tcp_timer_keep, frag6_fasttimo, &c.
- soclose holds softnet_lock, awaits usbnet lock in SIOCDELMULTI

This change breaks the deadlock by not passing the SIOCADDMULTI or
SIOCDELMULTI ioctl synchronously to the driver, which typically takes
the usbnet lock.

With this change, the ethernet layer still maintains the list of
multicast addresses synchronously, but we defer the driver logic that
updates the hardware multicast filter to an asynchronous USB task
without softnet_lock held.

This doesn't cause exactly the same ioctl to be sent to the driver --
usbnet just sends SIOCDELMULTI with an all-zero struct ifreq, and
might drop some ioctls if issued in quick succession. This is OK
because none of the drivers actually distinguish between SIOCADDMULTI
and SIOCDELMULTI, or examine the argument; the drivers just commit
whatever multicast addresses are listed in the ethercom.

Other than the different ioctl submitted, there is no change to the
ABI or locking scheme of usbnet, so this is safe to pull up to
netbsd-9. This means we unfortunately can't guarantee that if a
process issues SIOCADDMULTI and then sendto, the multicast filter
update will be done by the time of the sendto -- and, more
importantly, the packets received in reply to it. But failing to
guarantee that is better than deadlocking! Later changes on HEAD
will restore the synchronous multicast filter updates with much more
extensive ABI changes and API simplifications in usbnet(9).
Proposed on tech-net:

https://mail-index.netbsd.org/tech-net/2021/12/30/msg008164.html

XXX pullup-9
 1.25.2.5  28-Aug-2020  martin Pull up following revision(s) (requested by riastradh in ticket #1067):

sys/dev/usb/usbnet.c: revision 1.39
sys/dev/usb/if_atu.c: revision 1.73

usbnet: Reject buflen>MCLBYTES in usbnet_newbuf.
atu(4): Reject packets larger than MCLBYTES.
 1.25.2.4  17-Dec-2019  martin Pull up following revision(s) (requested by riastradh in ticket #565):

sys/dev/usb/usbnet.c: revision 1.31
sys/dev/usb/usbnet.c: revision 1.32
sys/dev/usb/usbnet.c: revision 1.33

Fix order of nulling un->un_pri->unp_ec.ec_mii.

Can't null it until after if_detach prevents further use.

While here, fix conditionals in usbnet_tick_task to use the unp_dying
flag, not the nullness of mii (or of ifp, which never null because
it's an embedded member).

Fix inequality for refcnt drain: -1 here means all refs gone.

Don't assume mii is not null here. Some drivers like urndis don't use
mii, so they always have mii == NULL.
ok riastradh. fixes PR kern/54762
 1.25.2.3  13-Sep-2019  martin Pull up following revision(s) (requested by mrg in ticket #197):

sys/dev/usb/if_url.c: revision 1.71
sys/dev/usb/usbnet.h: revision 1.15
sys/dev/usb/usb_mem.c: revision 1.72
sys/dev/usb/if_urndis.c: revision 1.24
sys/dev/usb/if_upl.c: revision 1.67
sys/dev/usb/usbnet.c: revision 1.25
sys/dev/usb/usbnet.c: revision 1.26
sys/dev/usb/usbnet.c: revision 1.27
sys/dev/usb/usbnet.c: revision 1.28

Teach urndis to handle some REMOTE_NDIS_INDICATE_STATUS_MSG. If the status
is reasonable, don't tell userland we got an error. Stops spurious EIO.
From openbsd.

Fix bug, remove {0,0} because we switched to usb_lookup().

s/no free/no freelist entry/ in a debug message.

fix a lock hang reported by sc.dying in PR#54495.

remove locking in usbnet_tick(). assume that all locking
needs are handled inside usbnet_tick_task(), which runs in
the usbtask thread. ensure that usbnet private is valid
before using it.

also check NULL private pointer in usbnet_isdying().
all the other cases should never happen.

don't try to set dying when we haven't usbnet_attach()d yet.
reported by maxv.

- use CALLARGS vs CALLED for better usbhist
- turn off usbnetdebug default
- log for all entry/exit points of usbnet_pipe_intr()
- in usbnet_start_locked() track whether any packet has been
transmitted for setting the timer. avoids spurious
"watchdog timeouts"
- in usbnet_stop() use callout_halt() vs callout_halt, and
also stop the usb task. fixes crash of usbtask after the
phy has detached.
- add a little more defensive checking in the tick task, and
add some high-log-level logs.
- in usbnet_detach() move the call to usbnet_stop_ifp() above
the calls to callout/usbtask stopping.
- set ec_mii and unp_pri to NULL when freeing their data

normalise an error message.

document usbnet_private locking. minor knf.
 1.25.2.2  01-Sep-2019  martin Pull up following revision(s) (requested by mrg in ticket #135):

distrib/sets/lists/comp/mi 1.2279
distrib/sets/lists/modules/mi 1.123
share/man/man9/Makefile 1.438
share/man/man9/usbnet.9 1.1-1.9
sys/dev/ic/rndisreg.h 1.3
sys/dev/usb/TODO 1.47-1.52
sys/dev/usb/TODO.usbmp 1.15,1.16
sys/dev/usb/files.usb 1.157-1.167
sys/dev/usb/if_aue.c 1.155-1.161
sys/dev/usb/if_auereg.h 1.30-1.32
sys/dev/usb/if_axe.c 1.103-1.119
sys/dev/usb/if_axen.c 1.51-1.53,1.55-1.67
sys/dev/usb/if_axenreg.h 1.15
sys/dev/usb/if_cdce.c 1.54-1.67
sys/dev/usb/if_cue.c 1.85,1.86
sys/dev/usb/if_cuereg.h 1.23
sys/dev/usb/if_kue.c 1.97-1.100
sys/dev/usb/if_kuereg.h 1.23,1.24
sys/dev/usb/if_mue.c 1.51-1.55
sys/dev/usb/if_muereg.h 1.6
sys/dev/usb/if_muevar.h 1.9
sys/dev/usb/if_smsc.c 1.46-1.61
sys/dev/usb/if_smscreg.h 1.6
sys/dev/usb/if_smscvar.h delete
sys/dev/usb/if_udav.c 1.60-1.71
sys/dev/usb/if_udavreg.h 1.14,1.15
sys/dev/usb/if_upl.c 1.65,1.66
sys/dev/usb/if_ure.c 1.15-1.31
sys/dev/usb/if_urevar.h 1.4,1.5
sys/dev/usb/if_url.c 1.67-1.70
sys/dev/usb/if_urlreg.h 1.14
sys/dev/usb/if_urndis.c 1.22-1.33
sys/dev/usb/if_urtwn.c 1.72
sys/dev/usb/ohci.c 1.290
sys/dev/usb/uhub.c 1.143
sys/dev/usb/usb.c 1.180
sys/dev/usb/usb.h 1.118
sys/dev/usb/usb_mem.c 1.71
sys/dev/usb/usb_subr.c 1.238,1.239
sys/dev/usb/usbdevs 1.772
sys/dev/usb/usbdi.c 1.183,1.186
sys/dev/usb/usbdi.h 1.97
sys/dev/usb/usbdi_util.c 1.75
sys/dev/usb/usbhist.h 1.5,1.6
sys/dev/usb/usbnet.c 1.1-1.24
sys/dev/usb/usbnet.h 1.1-1.14
sys/dev/usb/usbroothub.c 1.9
sys/dev/usb/xhci.c 1.109,1.110
sys/modules/Makefile 1.223
sys/modules/usbnet/Makefile 1.1

usbnet(9): Add common framework for USB network devices.
This bring various safety fixes to all updated drivers,
and includes locking clean up, detach safety when being
used or not, separate rx/tx locks to improve performance,
porting to NET_MPSAFE, many edge/error case bugs in
drivers fixed, as well as resovling PRs 54303 and 54308.
These drivers are converted: axe(4), axen(4), aue(4),
cdce(4), cue(4), kue(4), mue(4), smsc(4), udav(4),
upl(4), ure(4), url(4), and urndis(4).
 1.25.2.1  29-Aug-2019  martin file usbnet.c was added on branch netbsd-9 on 2019-09-01 13:00:36 +0000
 1.33.2.1  29-Feb-2020  ad Sync with head.
 1.38.4.2  13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.38.4.1  15-Mar-2020  martin file usbnet.c was added on branch phil-wifi on 2020-04-13 08:04:51 +0000
 1.39.2.1  03-Apr-2021  thorpej Sync with HEAD.
 1.41.2.1  17-Jun-2021  thorpej Sync w/ HEAD.
 1.113.4.1  14-Oct-2023  martin Pull up following revision(s) (requested by riastradh in ticket #415):

sys/dev/usb/usbnet.c: revision 1.115
sys/dev/usb/usbnet.c: revision 1.116
sys/dev/usb/usbnet.c: revision 1.117
sys/dev/usb/usbnet.c: revision 1.118

usbnet(9): Make sure unp->unp_if_flags is initialized on init.
usbnet_ifflags_cb is only called if the flags change while up and
running. (XXX Maybe it should be called in other circumstances too
so there's only one path here?)
Out of paranoia, clear the cache on stop.
PR kern/57645

usbnet(9): Fix sense of conditional in usbnet_ifflags_cb.
This appears to have been mistranscribed in revision 1.1 of usbnet.c.
PR kern/57645

usbnet(9): On if_init, stop/init if IFF_RUNNING -- not noop.
ether_ioctl(9) relies on this to reinitialize an interface when a
flags change returns ENETRESET. We can't just reprogram the hardware
multicast filter because some drivers have logic in if_init that's
conditional on IFF_PROMISC; perhaps we can reduce the cost of this if
we can change those drivers to do it in uno_mcast but that requires
some analysis to determine.
PR kern/57645

usbnet(9): Fix typo in comment.
No functional change intended.
PR kern/57645
 1.120.2.1  02-Aug-2025  perseant Sync with HEAD

RSS XML Feed