Source-Changes-HG archive

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

[src/trunk]: src introduce a library of common code / backends to share code ...



details:   https://anonhg.NetBSD.org/src/rev/c3c55f4c7396
branches:  trunk
changeset: 458543:c3c55f4c7396
user:      mrg <mrg%NetBSD.org@localhost>
date:      Wed Jul 31 09:13:16 2019 +0000

description:
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.

diffstat:

 distrib/sets/lists/modules/mi |     4 +-
 sys/dev/usb/files.usb         |     7 +-
 sys/dev/usb/if_axen.c         |  1441 +++++++---------------------------------
 sys/dev/usb/if_axenreg.h      |    11 +-
 sys/dev/usb/if_cdce.c         |   856 ++---------------------
 sys/dev/usb/usbnet.c          |  1055 ++++++++++++++++++++++++++++++
 sys/dev/usb/usbnet.h          |   270 +++++++
 sys/modules/Makefile          |     3 +-
 sys/modules/usbnet/Makefile   |    12 +
 9 files changed, 1716 insertions(+), 1943 deletions(-)

diffs (truncated from 4355 to 300 lines):

diff -r b8f770622081 -r c3c55f4c7396 distrib/sets/lists/modules/mi
--- a/distrib/sets/lists/modules/mi     Wed Jul 31 06:25:14 2019 +0000
+++ b/distrib/sets/lists/modules/mi     Wed Jul 31 09:13:16 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.122 2019/06/20 03:31:55 pgoyette Exp $
+# $NetBSD: mi,v 1.123 2019/07/31 09:13:16 mrg Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -440,6 +440,8 @@
 ./@MODULEDIR@/umap/umap.kmod                   base-kernel-modules     kmod
 ./@MODULEDIR@/union                            base-kernel-modules     kmod
 ./@MODULEDIR@/union/union.kmod                 base-kernel-modules     kmod
+./@MODULEDIR@/usbnet                           base-kernel-modules     kmod
+./@MODULEDIR@/usbnet/usbnet.kmod               base-kernel-modules     kmod
 ./@MODULEDIR@/usbverbose                       base-kernel-modules     kmod
 ./@MODULEDIR@/usbverbose/usbverbose.kmod       base-kernel-modules     kmod
 ./@MODULEDIR@/v7fs                             base-kernel-modules     kmod
diff -r b8f770622081 -r c3c55f4c7396 sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Wed Jul 31 06:25:14 2019 +0000
+++ b/sys/dev/usb/files.usb     Wed Jul 31 09:13:16 2019 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.usb,v 1.156 2019/05/08 13:40:19 isaki Exp $
+#      $NetBSD: files.usb,v 1.157 2019/07/31 09:13:16 mrg Exp $
 #
 # Config file and device description for machine-independent USB code.
 # Included by ports that need it.  Ports that use it must provide
@@ -103,6 +103,9 @@
 define usb_dma: usb
 file   dev/usb/usb_mem.c               usb_dma                 needs-flag
 
+define usbnet: usb
+file   dev/usb/usbnet.c                usbnet
+
 file   dev/usb/usb_verbose.c           usbverbose & usb
 
 # Hub driver
@@ -365,7 +368,7 @@
 file   dev/usb/if_axe.c                axe
 
 # ASIX AX88178a and AX88179
-device axen: arp, ether, ifnet, mii
+device axen: arp, ether, ifnet, mii, usbnet
 attach axen at usbdevif
 file   dev/usb/if_axen.c               axen
 
diff -r b8f770622081 -r c3c55f4c7396 sys/dev/usb/if_axen.c
--- a/sys/dev/usb/if_axen.c     Wed Jul 31 06:25:14 2019 +0000
+++ b/sys/dev/usb/if_axen.c     Wed Jul 31 09:13:16 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_axen.c,v 1.50 2019/07/15 03:14:22 mrg Exp $ */
+/*     $NetBSD: if_axen.c,v 1.51 2019/07/31 09:13:16 mrg Exp $ */
 /*     $OpenBSD: if_axen.c,v 1.3 2013/10/21 10:10:22 yuo Exp $ */
 
 /*
@@ -23,43 +23,19 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_axen.c,v 1.50 2019/07/15 03:14:22 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_axen.c,v 1.51 2019/07/31 09:13:16 mrg Exp $");
 
 #ifdef _KERNEL_OPT
-#include "opt_inet.h"
 #include "opt_usb.h"
 #endif
 
 #include <sys/param.h>
-#include <sys/bus.h>
-#include <sys/device.h>
-#include <sys/kernel.h>
-#include <sys/mbuf.h>
 #include <sys/module.h>
-#include <sys/socket.h>
-#include <sys/sockio.h>
-#include <sys/systm.h>
-
-#include <sys/rndsource.h>
-
-#include <net/if.h>
-#include <net/if_dl.h>
-#include <net/if_ether.h>
-#include <net/if_media.h>
-
-#include <net/bpf.h>
 
 #include <netinet/in.h>                /* XXX for netinet/ip.h */
 #include <netinet/ip.h>                /* XXX for IP_MAXPACKET */
 
-#include <dev/mii/mii.h>
-#include <dev/mii/miivar.h>
-
-#include <dev/usb/usb.h>
-#include <dev/usb/usbdi.h>
-#include <dev/usb/usbdi_util.h>
-#include <dev/usb/usbdivar.h>
-#include <dev/usb/usbdevs.h>
+#include <dev/usb/usbnet.h>
 
 #include <dev/usb/if_axenreg.h>
 
@@ -72,70 +48,13 @@
 #define DPRINTFN(n, x)
 #endif
 
-struct axen_softc;
-
-struct axen_chain {
-       struct axen_softc       *axen_sc;
-       struct usbd_xfer        *axen_xfer;
-       uint8_t                 *axen_buf;
-};
-
-struct axen_cdata {
-       struct axen_chain       axen_tx_chain[AXEN_TX_LIST_CNT];
-       struct axen_chain       axen_rx_chain[AXEN_RX_LIST_CNT];
-       int                     axen_tx_prod;
-       int                     axen_tx_cnt;
+struct axen_softc {
+       struct usbnet           axen_un;
+       int                     axen_rev;
 };
 
-struct axen_softc {
-       device_t                axen_dev;
-       struct ethercom         axen_ec;
-       struct mii_data         axen_mii;
-       krndsource_t            rnd_source;
-       struct usbd_device *    axen_udev;
-       struct usbd_interface * axen_iface;
-
-       uint16_t                axen_vendor;
-       uint16_t                axen_product;
-       uint16_t                axen_flags;
-       uint16_t                axen_timer;
-
-       int                     axen_ed[AXEN_ENDPT_MAX];
-       struct usbd_pipe        *axen_ep[AXEN_ENDPT_MAX];
-       int                     axen_if_flags;
-       struct axen_cdata       axen_cdata;
-       struct callout          axen_stat_ch;
-
-       int                     axen_refcnt;
-       bool                    axen_dying;
-       bool                    axen_stopping;
-       bool                    axen_attached;
-
-       struct usb_task         axen_tick_task;
-
-       kmutex_t                axen_lock;
-       kmutex_t                axen_mii_lock;
-       kmutex_t                axen_rxlock;
-       kmutex_t                axen_txlock;
-       kcondvar_t              axen_detachcv;
-
-       int                     axen_link;
-
-       int                     axen_phyno;
-       struct timeval          axen_rx_notice;
-       struct timeval          axen_tx_notice;
-       u_int                   axen_rx_bufsz;
-       u_int                   axen_tx_bufsz;
-       int                     axen_rev;
-
-#define sc_if  axen_ec.ec_if
-};
-
-#define GET_MII(sc) (&(sc)->axen_mii)
-#define GET_IFP(sc) (&(sc)->sc_if)
-
 struct axen_type {
-       struct usb_devno        axen_dev;
+       struct usb_devno        axen_devno;
        uint16_t                axen_flags;
 #define AX178A 0x0001          /* AX88178a */
 #define AX179  0x0002          /* AX88179 */
@@ -156,94 +75,34 @@
 
 static int     axen_match(device_t, cfdata_t, void *);
 static void    axen_attach(device_t, device_t, void *);
-static int     axen_detach(device_t, int);
-static int     axen_activate(device_t, devact_t);
 
 CFATTACH_DECL_NEW(axen, sizeof(struct axen_softc),
-       axen_match, axen_attach, axen_detach, axen_activate);
+       axen_match, axen_attach, usbnet_detach, usbnet_activate);
 
-static int     axen_tx_list_init(struct axen_softc *);
-static int     axen_rx_list_init(struct axen_softc *);
-static struct mbuf *axen_newbuf(void);
-static int     axen_encap(struct axen_softc *, struct mbuf *, int);
-static void    axen_rxeof(struct usbd_xfer *, void *, usbd_status);
-static int     axen_csum_flags_rx(struct ifnet *, uint32_t);
-static void    axen_txeof(struct usbd_xfer *, void *, usbd_status);
-static void    axen_tick(void *);
-static void    axen_tick_task(void *);
-static void    axen_start(struct ifnet *);
-static void    axen_start_locked(struct ifnet *);
-static int     axen_ioctl(struct ifnet *, u_long, void *);
+static unsigned        axen_tx_prepare(struct usbnet *, struct mbuf *,
+                               struct usbnet_chain *);
 static int     axen_init(struct ifnet *);
-static void    axen_stop(struct ifnet *, int);
-static void    axen_stop_locked(struct ifnet *, int);
-static void    axen_watchdog(struct ifnet *);
-static int     axen_miibus_readreg(device_t, int, int, uint16_t *);
-static int     axen_miibus_writereg(device_t, int, int, uint16_t);
-static void    axen_miibus_statchg(struct ifnet *);
 static int     axen_cmd(struct axen_softc *, int, int, int, void *);
-static int     axen_ifmedia_upd(struct ifnet *);
-static void    axen_ifmedia_sts(struct ifnet *, struct ifmediareq *);
 static void    axen_reset(struct axen_softc *);
 static int     axen_get_eaddr(struct axen_softc *, void *);
-static void    axen_iff(struct axen_softc *);
+static void    axen_stop_cb(struct ifnet *, int);
 static void    axen_ax88179_init(struct axen_softc *);
-static void    axen_setcoe(struct axen_softc *);
-
-/*
- * Access functions for MII.  Take the MII lock to call axen_cmd().
- * Two forms: softc lock currently held or not.
- */
-static void
-axen_lock_mii(struct axen_softc *sc)
-{
-
-       mutex_enter(&sc->axen_lock);
-       sc->axen_refcnt++;
-       mutex_exit(&sc->axen_lock);
-
-       mutex_enter(&sc->axen_mii_lock);
-}
-
-static void
-axen_lock_mii_sc_locked(struct axen_softc *sc)
-{
-       KASSERT(mutex_owned(&sc->axen_lock));
 
-       sc->axen_refcnt++;
-       mutex_enter(&sc->axen_mii_lock);
-}
-
-static void
-axen_unlock_mii(struct axen_softc *sc)
-{
-
-       mutex_exit(&sc->axen_mii_lock);
-       mutex_enter(&sc->axen_lock);
-       if (--sc->axen_refcnt < 0)
-               cv_broadcast(&sc->axen_detachcv);
-       mutex_exit(&sc->axen_lock);
-}
-
-static void
-axen_unlock_mii_sc_locked(struct axen_softc *sc)
-{
-       KASSERT(mutex_owned(&sc->axen_lock));
-
-       mutex_exit(&sc->axen_mii_lock);
-       if (--sc->axen_refcnt < 0)
-               cv_broadcast(&sc->axen_detachcv);
-}
+static usbd_status axen_mii_read_reg(struct usbnet *, int, int, uint16_t *);
+static usbd_status axen_mii_write_reg(struct usbnet *, int, int, uint16_t);
+static void    axen_rxeof_loop(struct usbnet *, struct usbd_xfer *,
+                   struct usbnet_chain *, uint32_t);
 
 static int
 axen_cmd(struct axen_softc *sc, int cmd, int index, int val, void *buf)
 {
+       struct usbnet * const un = &sc->axen_un;
        usb_device_request_t req;
        usbd_status err;
 
-       KASSERT(mutex_owned(&sc->axen_mii_lock));
+       KASSERT(mutex_owned(&un->un_miilock));
 
-       if (sc->axen_dying)
+       if (un->un_dying)
                return 0;
 
        if (AXEN_CMD_DIR(cmd))
@@ -255,7 +114,7 @@
        USETW(req.wIndex, index);
        USETW(req.wLength, AXEN_CMD_LEN(cmd));
 
-       err = usbd_do_request(sc->axen_udev, &req, buf);
+       err = usbd_do_request(un->un_udev, &req, buf);
        DPRINTFN(5, ("axen_cmd: cmd 0x%04x val 0x%04x len %d\n",
            cmd, val, AXEN_CMD_LEN(cmd)));
 
@@ -267,93 +126,55 @@
        return 0;
 }
 
-static int
-axen_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
+static usbd_status
+axen_mii_read_reg(struct usbnet *un, int reg, int phy, uint16_t *val)
 {



Home | Main Index | Thread Index | Old Index