13d09a7e4SHugo Santos/*	$NetBSD: lancevar.h,v 1.10 2005/12/11 12:21:27 christos Exp $	*/
23d09a7e4SHugo Santos
33d09a7e4SHugo Santos/*-
42b5a056eSAugustin Cavalier * SPDX-License-Identifier: BSD-2-Clause-NetBSD
52b5a056eSAugustin Cavalier *
63d09a7e4SHugo Santos * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
73d09a7e4SHugo Santos * All rights reserved.
83d09a7e4SHugo Santos *
93d09a7e4SHugo Santos * This code is derived from software contributed to The NetBSD Foundation
103d09a7e4SHugo Santos * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
113d09a7e4SHugo Santos * Simulation Facility, NASA Ames Research Center.
123d09a7e4SHugo Santos *
133d09a7e4SHugo Santos * Redistribution and use in source and binary forms, with or without
143d09a7e4SHugo Santos * modification, are permitted provided that the following conditions
153d09a7e4SHugo Santos * are met:
163d09a7e4SHugo Santos * 1. Redistributions of source code must retain the above copyright
173d09a7e4SHugo Santos *    notice, this list of conditions and the following disclaimer.
183d09a7e4SHugo Santos * 2. Redistributions in binary form must reproduce the above copyright
193d09a7e4SHugo Santos *    notice, this list of conditions and the following disclaimer in the
203d09a7e4SHugo Santos *    documentation and/or other materials provided with the distribution.
213d09a7e4SHugo Santos *
223d09a7e4SHugo Santos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
233d09a7e4SHugo Santos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
243d09a7e4SHugo Santos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
253d09a7e4SHugo Santos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
263d09a7e4SHugo Santos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
273d09a7e4SHugo Santos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
283d09a7e4SHugo Santos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
293d09a7e4SHugo Santos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
303d09a7e4SHugo Santos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
313d09a7e4SHugo Santos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
323d09a7e4SHugo Santos * POSSIBILITY OF SUCH DAMAGE.
333d09a7e4SHugo Santos */
343d09a7e4SHugo Santos
352b5a056eSAugustin Cavalier/* $FreeBSD: releng/12.0/sys/dev/le/lancevar.h 326255 2017-11-27 14:52:40Z pfg $ */
363d09a7e4SHugo Santos
373d09a7e4SHugo Santos#ifndef _DEV_LE_LANCEVAR_H_
383d09a7e4SHugo Santos#define	_DEV_LE_LANCEVAR_H_
393d09a7e4SHugo Santos
403d09a7e4SHugo Santosextern devclass_t le_devclass;
413d09a7e4SHugo Santos
423d09a7e4SHugo Santosstruct lance_softc {
433d09a7e4SHugo Santos	struct ifnet	*sc_ifp;
443d09a7e4SHugo Santos	struct ifmedia	sc_media;
453d09a7e4SHugo Santos	struct mtx	sc_mtx;
46648db733SJérôme Duval	struct callout	sc_wdog_ch;
47648db733SJérôme Duval	int		sc_wdog_timer;
483d09a7e4SHugo Santos
493d09a7e4SHugo Santos	/*
503d09a7e4SHugo Santos	 * Memory functions:
513d09a7e4SHugo Santos	 *
523d09a7e4SHugo Santos	 *	copy to/from descriptor
533d09a7e4SHugo Santos	 *	copy to/from buffer
543d09a7e4SHugo Santos	 *	zero bytes in buffer
553d09a7e4SHugo Santos	 */
563d09a7e4SHugo Santos	void	(*sc_copytodesc)(struct lance_softc *, void *, int, int);
573d09a7e4SHugo Santos	void	(*sc_copyfromdesc)(struct lance_softc *, void *, int, int);
583d09a7e4SHugo Santos	void	(*sc_copytobuf)(struct lance_softc *, void *, int, int);
593d09a7e4SHugo Santos	void	(*sc_copyfrombuf)(struct lance_softc *, void *, int, int);
603d09a7e4SHugo Santos	void	(*sc_zerobuf)(struct lance_softc *, int, int);
613d09a7e4SHugo Santos
623d09a7e4SHugo Santos	/*
633d09a7e4SHugo Santos	 * Machine-dependent functions:
643d09a7e4SHugo Santos	 *
653d09a7e4SHugo Santos	 *	read/write CSR
663d09a7e4SHugo Santos	 *	hardware reset hook - may be NULL
673d09a7e4SHugo Santos	 *	hardware init hook - may be NULL
683d09a7e4SHugo Santos	 *	no carrier hook - may be NULL
693d09a7e4SHugo Santos	 *	media change hook - may be NULL
703d09a7e4SHugo Santos	 */
713d09a7e4SHugo Santos	uint16_t	(*sc_rdcsr)(struct lance_softc *, uint16_t);
723d09a7e4SHugo Santos	void	(*sc_wrcsr)(struct lance_softc *, uint16_t, uint16_t);
733d09a7e4SHugo Santos	void	(*sc_hwreset)(struct lance_softc *);
743d09a7e4SHugo Santos	void	(*sc_hwinit)(struct lance_softc *);
753d09a7e4SHugo Santos	int	(*sc_hwintr)(struct lance_softc *);
763d09a7e4SHugo Santos	void	(*sc_nocarrier)(struct lance_softc *);
773d09a7e4SHugo Santos	int	(*sc_mediachange)(struct lance_softc *);
783d09a7e4SHugo Santos	void	(*sc_mediastatus)(struct lance_softc *, struct ifmediareq *);
793d09a7e4SHugo Santos
803d09a7e4SHugo Santos	/*
813d09a7e4SHugo Santos	 * Media-supported by this interface.  If this is NULL,
823d09a7e4SHugo Santos	 * the only supported media is assumed to be "manual".
833d09a7e4SHugo Santos	 */
843d09a7e4SHugo Santos	const int	*sc_supmedia;
853d09a7e4SHugo Santos	int	sc_nsupmedia;
863d09a7e4SHugo Santos	int	sc_defaultmedia;
873d09a7e4SHugo Santos
883d09a7e4SHugo Santos	uint16_t	sc_conf3;	/* CSR3 value */
893d09a7e4SHugo Santos
903d09a7e4SHugo Santos	void	*sc_mem;		/* base address of RAM - CPU's view */
913d09a7e4SHugo Santos	bus_addr_t	sc_addr;	/* base address of RAM - LANCE's view */
923d09a7e4SHugo Santos
933d09a7e4SHugo Santos	bus_size_t	sc_memsize;	/* size of RAM */
943d09a7e4SHugo Santos
953d09a7e4SHugo Santos	int	sc_nrbuf;	/* number of receive buffers */
963d09a7e4SHugo Santos	int	sc_ntbuf;	/* number of transmit buffers */
973d09a7e4SHugo Santos	int	sc_last_rd;
983d09a7e4SHugo Santos	int	sc_first_td;
993d09a7e4SHugo Santos	int	sc_last_td;
1003d09a7e4SHugo Santos	int	sc_no_td;
1013d09a7e4SHugo Santos
1023d09a7e4SHugo Santos	int	sc_initaddr;
1033d09a7e4SHugo Santos	int	sc_rmdaddr;
1043d09a7e4SHugo Santos	int	sc_tmdaddr;
1053d09a7e4SHugo Santos	int	sc_rbufaddr;
1063d09a7e4SHugo Santos	int	sc_tbufaddr;
1073d09a7e4SHugo Santos
1083d09a7e4SHugo Santos	uint8_t	sc_enaddr[ETHER_ADDR_LEN];
1093d09a7e4SHugo Santos
1103d09a7e4SHugo Santos	void	(*sc_meminit)(struct lance_softc *);
1113d09a7e4SHugo Santos	void	(*sc_start_locked)(struct lance_softc *);
1123d09a7e4SHugo Santos
1133d09a7e4SHugo Santos	int	sc_flags;
1143d09a7e4SHugo Santos#define	LE_ALLMULTI	(1 << 0)
1153d09a7e4SHugo Santos#define	LE_BSWAP	(1 << 1)
1163d09a7e4SHugo Santos#define	LE_CARRIER	(1 << 2)
1173d09a7e4SHugo Santos#define	LE_DEBUG	(1 << 3)
1183d09a7e4SHugo Santos#define	LE_PROMISC	(1 << 4)
11980df1125SHugo Santos
120648db733SJérôme Duval#ifdef __HAIKU__
12180df1125SHugo Santos	uint16_t sc_lastisr;
122648db733SJérôme Duval#endif
1233d09a7e4SHugo Santos};
1243d09a7e4SHugo Santos
1253d09a7e4SHugo Santos#define	LE_LOCK_INIT(_sc, _name)					\
1263d09a7e4SHugo Santos	mtx_init(&(_sc)->sc_mtx, _name, MTX_NETWORK_LOCK, MTX_DEF)
1273d09a7e4SHugo Santos#define	LE_LOCK_INITIALIZED(_sc)	mtx_initialized(&(_sc)->sc_mtx)
1283d09a7e4SHugo Santos#define	LE_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
1293d09a7e4SHugo Santos#define	LE_UNLOCK(_sc)			mtx_unlock(&(_sc)->sc_mtx)
1303d09a7e4SHugo Santos#define	LE_LOCK_ASSERT(_sc, _what)	mtx_assert(&(_sc)->sc_mtx, (_what))
1313d09a7e4SHugo Santos#define	LE_LOCK_DESTROY(_sc)		mtx_destroy(&(_sc)->sc_mtx)
1323d09a7e4SHugo Santos
1333d09a7e4SHugo Santos/*
1343d09a7e4SHugo Santos * Unfortunately, manual byte swapping is only necessary for the PCnet-PCI
1353d09a7e4SHugo Santos * variants but not for the original LANCE or ILACC so we cannot do this
1363d09a7e4SHugo Santos * with #ifdefs resolved at compile time.
1373d09a7e4SHugo Santos */
1383d09a7e4SHugo Santos#define	LE_HTOLE16(v)	(((sc)->sc_flags & LE_BSWAP) ? htole16(v) : (v))
1393d09a7e4SHugo Santos#define	LE_HTOLE32(v)	(((sc)->sc_flags & LE_BSWAP) ? htole32(v) : (v))
1403d09a7e4SHugo Santos#define	LE_LE16TOH(v)	(((sc)->sc_flags & LE_BSWAP) ? le16toh(v) : (v))
1413d09a7e4SHugo Santos#define	LE_LE32TOH(v)	(((sc)->sc_flags & LE_BSWAP) ? le32toh(v) : (v))
1423d09a7e4SHugo Santos
1433d09a7e4SHugo Santosint lance_config(struct lance_softc *, const char*, int);
1443d09a7e4SHugo Santosvoid lance_attach(struct lance_softc *);
1453d09a7e4SHugo Santosvoid lance_detach(struct lance_softc *);
1463d09a7e4SHugo Santosvoid lance_suspend(struct lance_softc *);
1473d09a7e4SHugo Santosvoid lance_resume(struct lance_softc *);
1483d09a7e4SHugo Santosvoid lance_init_locked(struct lance_softc *);
1493d09a7e4SHugo Santosint lance_put(struct lance_softc *, int, struct mbuf *);
1503d09a7e4SHugo Santosstruct mbuf *lance_get(struct lance_softc *, int, int);
1513d09a7e4SHugo Santosvoid lance_setladrf(struct lance_softc *, u_int16_t *);
1523d09a7e4SHugo Santos
1533d09a7e4SHugo Santos/*
1543d09a7e4SHugo Santos * The following functions are only useful on certain CPU/bus
1553d09a7e4SHugo Santos * combinations.  They should be written in assembly language for
1563d09a7e4SHugo Santos * maximum efficiency, but machine-independent versions are provided
1573d09a7e4SHugo Santos * for drivers that have not yet been optimized.
1583d09a7e4SHugo Santos */
1593d09a7e4SHugo Santosvoid lance_copytobuf_contig(struct lance_softc *, void *, int, int);
1603d09a7e4SHugo Santosvoid lance_copyfrombuf_contig(struct lance_softc *, void *, int, int);
1613d09a7e4SHugo Santosvoid lance_zerobuf_contig(struct lance_softc *, int, int);
1623d09a7e4SHugo Santos
1633d09a7e4SHugo Santos#if 0	/* Example only - see lance.c */
1643d09a7e4SHugo Santosvoid lance_copytobuf_gap2(struct lance_softc *, void *, int, int);
1653d09a7e4SHugo Santosvoid lance_copyfrombuf_gap2(struct lance_softc *, void *, int, int);
1663d09a7e4SHugo Santosvoid lance_zerobuf_gap2(struct lance_softc *, int, int);
1673d09a7e4SHugo Santos
1683d09a7e4SHugo Santosvoid lance_copytobuf_gap16(struct lance_softc *, void *, int, int);
1693d09a7e4SHugo Santosvoid lance_copyfrombuf_gap16(struct lance_softc *, void *, int, int);
1703d09a7e4SHugo Santosvoid lance_zerobuf_gap16(struct lance_softc *, int, int);
1713d09a7e4SHugo Santos#endif /* Example only */
1723d09a7e4SHugo Santos
1733d09a7e4SHugo Santos/*
1743d09a7e4SHugo Santos * Compare two Ether/802 addresses for equality, inlined and
1753d09a7e4SHugo Santos * unrolled for speed.  Use this like memcmp().
1763d09a7e4SHugo Santos *
1773d09a7e4SHugo Santos * XXX: Add <machine/inlines.h> for stuff like this?
1783d09a7e4SHugo Santos * XXX: or maybe add it to libkern.h instead?
1793d09a7e4SHugo Santos *
1803d09a7e4SHugo Santos * "I'd love to have an inline assembler version of this."
1813d09a7e4SHugo Santos * XXX: Who wanted that? mycroft?  I wrote one, but this
1823d09a7e4SHugo Santos * version in C is as good as hand-coded assembly. -gwr
1833d09a7e4SHugo Santos *
1843d09a7e4SHugo Santos * Please do NOT tweak this without looking at the actual
1853d09a7e4SHugo Santos * assembly code generated before and after your tweaks!
1863d09a7e4SHugo Santos */
1873d09a7e4SHugo Santosstatic inline uint16_t
1883d09a7e4SHugo Santosether_cmp(void *one, void *two)
1893d09a7e4SHugo Santos{
1903d09a7e4SHugo Santos	uint16_t *a = (u_short *)one;
1913d09a7e4SHugo Santos	uint16_t *b = (u_short *)two;
1923d09a7e4SHugo Santos	uint16_t diff;
1933d09a7e4SHugo Santos
1943d09a7e4SHugo Santos#ifdef	m68k
1953d09a7e4SHugo Santos	/*
1963d09a7e4SHugo Santos	 * The post-increment-pointer form produces the best
1973d09a7e4SHugo Santos	 * machine code for m68k.  This was carefully tuned
1983d09a7e4SHugo Santos	 * so it compiles to just 8 short (2-byte) op-codes!
1993d09a7e4SHugo Santos	 */
2003d09a7e4SHugo Santos	diff  = *a++ - *b++;
2013d09a7e4SHugo Santos	diff |= *a++ - *b++;
2023d09a7e4SHugo Santos	diff |= *a++ - *b++;
2033d09a7e4SHugo Santos#else
2043d09a7e4SHugo Santos	/*
2053d09a7e4SHugo Santos	 * Most modern CPUs do better with a single expresion.
2063d09a7e4SHugo Santos	 * Note that short-cut evaluation is NOT helpful here,
2073d09a7e4SHugo Santos	 * because it just makes the code longer, not faster!
2083d09a7e4SHugo Santos	 */
2093d09a7e4SHugo Santos	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
2103d09a7e4SHugo Santos#endif
2113d09a7e4SHugo Santos
2123d09a7e4SHugo Santos	return (diff);
2133d09a7e4SHugo Santos}
2143d09a7e4SHugo Santos
2153d09a7e4SHugo Santos#endif /* _DEV_LE_LANCEVAR_H_ */
216