/*-
* Copyright (c) 1998, 2000, 2001, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1997
* Jonathan Stone and Jason R. Thorpe. All rights reserved.
*
* This software is derived from information provided by Matt Thomas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jonathan Stone
* and Jason R. Thorpe for the NetBSD Project.
* 4. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _NET_IF_MEDIA_H_
#define _NET_IF_MEDIA_H_
/*
* Prototypes and definitions for BSD/OS-compatible network interface
* media selection.
*
* Where it is safe to do so, this code strays slightly from the BSD/OS
* design. Software which uses the API (device drivers, basically)
* shouldn't notice any difference.
*
* Many thanks to Matt Thomas for providing the information necessary
* to implement this interface.
*/
/*
* Status bits. THIS IS NOT A MEDIA WORD.
*/
#define IFM_AVALID 0x00000001 /* Active bit valid */
#define IFM_ACTIVE 0x00000002 /* Interface attached to working net */
/*
* if_media Options word:
* Bits Use
* ---- -------
* 0-4 Media subtype MAX SUBTYPE == 255 for ETH and 31 for others
* See below (IFM_ETHER part) for the detail.
* 5-7 Media type
* 8-15 Type specific options
* 16-18 Mode (for multi-mode devices)
* 19 (Reserved for Future Use)
* 20-27 Shared (global) options
* 28-31 Instance
*
* 3 2 1
* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
* +-------+---------------+-+-----+---------------+-----+---------+
* | | |R| | | | |
* | IMASK | GMASK |F|MMASK+-----+ OMASK |NMASK| TMASK |
* | | |U| |XTMSK| | | |
* +-------+---------------+-+-----+-----+---------+-----+---------+
* <-----> <---> <--->
* IFM_INST() IFM_MODE() IFM_TYPE()
*
* IFM_SUBTYPE(other than ETH)<------->
*
* <---> IFM_SUBTYPE(ETH)<------->
*
*
* <-------------> <------------->
* IFM_OPTIONS()
*/
/* Mask of "status valid" bits, for ifconfig(8). */
#define IFM_STATUS_VALID IFM_AVALID
/* List of "status valid" bits, for ifconfig(8). */
#define IFM_STATUS_VALID_LIST { \
IFM_AVALID, \
0, \
}
/*
* Macro to create a media word.
*/
#define IFM_MAKEWORD(type, subtype, options, instance) \
((type) | (subtype) | (options) | ((instance) << IFM_ISHIFT))
#define IFM_MAKEMODE(mode) \
(((mode) << IFM_MSHIFT) & IFM_MMASK)
/*
* Media type (IFM_NMASK).
*/
#define IFM_GENERIC 0x00000000 /* Only used for link status reporting */
#define IFM_ETHER 0x00000020
#define IFM_TOKEN 0x00000040
#define IFM_FDDI 0x00000060
#define IFM_IEEE80211 0x00000080
#define IFM_CARP 0x000000c0 /* Common Address Redundancy Protocol */
#define IFM_NMIN IFM_ETHER /* lowest Network type */
#define IFM_NMAX IFM_NMASK /* highest Network type */
/*
* Shared media sub-types (IFM_TMASK)
*/
#define IFM_AUTO 0 /* Autoselect best media */
#define IFM_MANUAL 1 /* Jumper/dipswitch selects media */
#define IFM_NONE 2 /* Deselect all media */
/*
* Shared (global) options (IFM_GMASK)
*/
#define IFM_FDX 0x00100000 /* Force full duplex */
#define IFM_HDX 0x00200000 /* Force half duplex */
#define IFM_FLOW 0x00400000 /* enable hardware flow control */
#define IFM_FLAG0 0x01000000 /* Driver defined flag */
#define IFM_FLAG1 0x02000000 /* Driver defined flag */
#define IFM_FLAG2 0x04000000 /* Driver defined flag */
#define IFM_LOOP 0x08000000 /* Put hardware in loopback */
/*
* 0: Generic (IFM_GENERIC). Only used for link status reporting.
* No any media specific flag.
*/
/*
* 1: Ethernet (IFM_ETHER)
*
* In order to use more than 31 subtypes, Ethernet uses some of the option
* bits as part of the subtype field. See the options section below for
* relevant definitions.
*/
#define IFM_ETHER_SUBTYPE(x) (((x) & IFM_TMASK) | \
(((x) & (_IFM_ETH_XTMASK >> IFM_ETH_XSHIFT)) << IFM_ETH_XSHIFT))
#define IFM_ETHER_SUBTYPE_GET(x) ((x) & (IFM_TMASK | _IFM_ETH_XTMASK))
#define _IFM_EX(x) IFM_ETHER_SUBTYPE(x) /* internal shorthand */
/*
* 6: Common Address Redundancy Protocol (IFM_CARP)
* No any media specific flag.
*/
/*
* NetBSD extension not defined in the BSDI API. This is used in various
* places to get the canonical description for a given type/subtype.
*
* In the subtype and mediaopt descriptions, the valid TYPE bits are OR'd
* in to indicate which TYPE the subtype/option corresponds to. If no
* TYPE is present, it is a shared media/mediaopt.
*
* Note that these are parsed case-insensitive.
*
* Order is important. The first matching entry is the canonical name
* for a media type; subsequent matches are aliases.
*/
struct ifmedia_description {
int ifmt_word; /* word value; may be masked */
const char *ifmt_string; /* description */
};
/*
* Baudrate descriptions for the various media types.
*/
struct ifmedia_baudrate {
int ifmb_word; /* media word */
uint64_t ifmb_baudrate; /* corresponding baudrate */
};
/*
* Status bit descriptions for the various media types.
*/
struct ifmedia_status_description {
int ifms_type;
int ifms_valid;
int ifms_bit;
const char *ifms_string[2];
};
/*
* Driver callbacks for media status and change requests.
*/
typedef int (*ifm_change_cb_t)(struct ifnet *);
typedef void (*ifm_stat_cb_t)(struct ifnet *, struct ifmediareq *);
/*
* In-kernel representation of a single supported media type.
*/
struct ifmedia_entry {
TAILQ_ENTRY(ifmedia_entry) ifm_list;
u_int ifm_media; /* IFMWD: description of this media */
u_int ifm_data; /* for driver-specific use */
void *ifm_aux; /* for driver-specific use */
};
/*
* One of these goes into a network interface's softc structure.
* It is used to keep general media state.
*
* LOCKING
* =======
* The ifmedia is protected by a lock provided by the interface
* driver. All ifmedia API entry points (with the exception of one)
* are expect to be called with this mutex NOT HELD.
*
* ifmedia_ioctl() is called with the interface's if_ioctl_lock held,
* and thus the locking order is:
*
* IFNET_LOCK -> ifm_lock
*
* Driver callbacks (ifm_change / ifm_status) are called with ifm_lock HELD.
*
* Field markings and the corresponding locks:
*
* m: ifm_lock
* :: unlocked, stable
*/
struct ifmedia {
kmutex_t *ifm_lock; /* :: mutex (provided by interface driver) */
u_int ifm_mask; /* :: IFMWD: mask of changes we don't care */
u_int ifm_media; /*
* m: IFMWD: current user-set media word.
*
* XXX some drivers misuse this entry as
* current active media word. Don't use this
* entry as this purpose but use driver
* specific entry if you don't use mii(4).
*/
struct ifmedia_entry *ifm_cur; /*
* m: entry corresponding to
* ifm_media
*/
TAILQ_HEAD(, ifmedia_entry) ifm_list; /*
* m: list of all supported
* media
*/
ifm_change_cb_t ifm_change; /* :: media change driver callback */
ifm_stat_cb_t ifm_status; /* :: media status driver callback */
uintptr_t ifm_legacy; /* m: legacy driver handling */
};
/* Release resources associated with an ifmedia. */
void ifmedia_fini(struct ifmedia *);
/* Add one supported medium to a struct ifmedia. */
void ifmedia_add(struct ifmedia *, int, int, void *);
/* Add an array (of ifmedia_entry) media to a struct ifmedia. */
void ifmedia_list_add(struct ifmedia *, struct ifmedia_entry *, int);
/* Set default media type on initialization. */
void ifmedia_set(struct ifmedia *ifm, int mword);
/* Common ioctl function for getting/setting media, called by driver. */
int ifmedia_ioctl(struct ifnet *, struct ifreq *, struct ifmedia *, u_long);
/* Look up a media entry. */
struct ifmedia_entry *ifmedia_match(struct ifmedia *, u_int, u_int);
/* Delete all media for a given media instance */
void ifmedia_delete_instance(struct ifmedia *, u_int);
/* Remove all media */
void ifmedia_removeall(struct ifmedia *);
/* Compute baudrate for a given media. */
uint64_t ifmedia_baudrate(int);
/*
* This is a thin wrapper around the ifmedia "change" callback that
* is available to drivers to use within their own initialization
* routines.
*
* IFMEDIA must be LOCKED.
*/
int ifmedia_change(struct ifmedia *, struct ifnet *);
#else
/* Functions for converting media to/from strings, in libutil/if_media.c */
const char *get_media_type_string(int);
const char *get_media_subtype_string(int);
const char *get_media_mode_string(int);
const char *get_media_option_string(int *);
int get_media_mode(int, const char *);
int get_media_subtype(int, const char *);
int get_media_options(int, const char *, char **);
int lookup_media_word(struct ifmedia_description *, int, const char *);
#endif /* _KERNEL */