1155093Smarius/*	$NetBSD: lancevar.h,v 1.10 2005/12/11 12:21:27 christos Exp $	*/
2155093Smarius
3155093Smarius/*-
4155093Smarius * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5155093Smarius * All rights reserved.
6155093Smarius *
7155093Smarius * This code is derived from software contributed to The NetBSD Foundation
8155093Smarius * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9155093Smarius * Simulation Facility, NASA Ames Research Center.
10155093Smarius *
11155093Smarius * Redistribution and use in source and binary forms, with or without
12155093Smarius * modification, are permitted provided that the following conditions
13155093Smarius * are met:
14155093Smarius * 1. Redistributions of source code must retain the above copyright
15155093Smarius *    notice, this list of conditions and the following disclaimer.
16155093Smarius * 2. Redistributions in binary form must reproduce the above copyright
17155093Smarius *    notice, this list of conditions and the following disclaimer in the
18155093Smarius *    documentation and/or other materials provided with the distribution.
19155093Smarius *
20155093Smarius * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21155093Smarius * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22155093Smarius * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23155093Smarius * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24155093Smarius * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25155093Smarius * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26155093Smarius * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27155093Smarius * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28155093Smarius * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29155093Smarius * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30155093Smarius * POSSIBILITY OF SUCH DAMAGE.
31155093Smarius */
32155093Smarius
33155093Smarius/* $FreeBSD: releng/10.3/sys/dev/le/lancevar.h 204646 2010-03-03 17:55:51Z joel $ */
34155093Smarius
35155093Smarius#ifndef _DEV_LE_LANCEVAR_H_
36155093Smarius#define	_DEV_LE_LANCEVAR_H_
37155093Smarius
38155093Smariusextern devclass_t le_devclass;
39155093Smarius
40155093Smariusstruct lance_softc {
41155093Smarius	struct ifnet	*sc_ifp;
42155093Smarius	struct ifmedia	sc_media;
43155093Smarius	struct mtx	sc_mtx;
44164933Smarius	struct callout	sc_wdog_ch;
45164933Smarius	int		sc_wdog_timer;
46155093Smarius
47155093Smarius	/*
48155093Smarius	 * Memory functions:
49155093Smarius	 *
50155093Smarius	 *	copy to/from descriptor
51155093Smarius	 *	copy to/from buffer
52155093Smarius	 *	zero bytes in buffer
53155093Smarius	 */
54155093Smarius	void	(*sc_copytodesc)(struct lance_softc *, void *, int, int);
55155093Smarius	void	(*sc_copyfromdesc)(struct lance_softc *, void *, int, int);
56155093Smarius	void	(*sc_copytobuf)(struct lance_softc *, void *, int, int);
57155093Smarius	void	(*sc_copyfrombuf)(struct lance_softc *, void *, int, int);
58155093Smarius	void	(*sc_zerobuf)(struct lance_softc *, int, int);
59155093Smarius
60155093Smarius	/*
61155093Smarius	 * Machine-dependent functions:
62155093Smarius	 *
63155093Smarius	 *	read/write CSR
64155093Smarius	 *	hardware reset hook - may be NULL
65155093Smarius	 *	hardware init hook - may be NULL
66155093Smarius	 *	no carrier hook - may be NULL
67155093Smarius	 *	media change hook - may be NULL
68155093Smarius	 */
69155093Smarius	uint16_t	(*sc_rdcsr)(struct lance_softc *, uint16_t);
70155093Smarius	void	(*sc_wrcsr)(struct lance_softc *, uint16_t, uint16_t);
71155093Smarius	void	(*sc_hwreset)(struct lance_softc *);
72155093Smarius	void	(*sc_hwinit)(struct lance_softc *);
73155093Smarius	int	(*sc_hwintr)(struct lance_softc *);
74155093Smarius	void	(*sc_nocarrier)(struct lance_softc *);
75155093Smarius	int	(*sc_mediachange)(struct lance_softc *);
76155093Smarius	void	(*sc_mediastatus)(struct lance_softc *, struct ifmediareq *);
77155093Smarius
78155093Smarius	/*
79155093Smarius	 * Media-supported by this interface.  If this is NULL,
80155093Smarius	 * the only supported media is assumed to be "manual".
81155093Smarius	 */
82155093Smarius	const int	*sc_supmedia;
83155093Smarius	int	sc_nsupmedia;
84155093Smarius	int	sc_defaultmedia;
85155093Smarius
86155093Smarius	uint16_t	sc_conf3;	/* CSR3 value */
87155093Smarius
88158663Smarius	void	*sc_mem;		/* base address of RAM - CPU's view */
89158663Smarius	bus_addr_t	sc_addr;	/* base address of RAM - LANCE's view */
90155093Smarius
91158663Smarius	bus_size_t	sc_memsize;	/* size of RAM */
92155093Smarius
93155093Smarius	int	sc_nrbuf;	/* number of receive buffers */
94155093Smarius	int	sc_ntbuf;	/* number of transmit buffers */
95155093Smarius	int	sc_last_rd;
96155093Smarius	int	sc_first_td;
97155093Smarius	int	sc_last_td;
98155093Smarius	int	sc_no_td;
99155093Smarius
100155093Smarius	int	sc_initaddr;
101155093Smarius	int	sc_rmdaddr;
102155093Smarius	int	sc_tmdaddr;
103155093Smarius	int	sc_rbufaddr;
104155093Smarius	int	sc_tbufaddr;
105155093Smarius
106155093Smarius	uint8_t	sc_enaddr[ETHER_ADDR_LEN];
107155093Smarius
108155093Smarius	void	(*sc_meminit)(struct lance_softc *);
109155093Smarius	void	(*sc_start_locked)(struct lance_softc *);
110155093Smarius
111155093Smarius	int	sc_flags;
112155093Smarius#define	LE_ALLMULTI	(1 << 0)
113155093Smarius#define	LE_BSWAP	(1 << 1)
114155093Smarius#define	LE_CARRIER	(1 << 2)
115155093Smarius#define	LE_DEBUG	(1 << 3)
116155093Smarius#define	LE_PROMISC	(1 << 4)
117155093Smarius};
118155093Smarius
119155093Smarius#define	LE_LOCK_INIT(_sc, _name)					\
120155093Smarius	mtx_init(&(_sc)->sc_mtx, _name, MTX_NETWORK_LOCK, MTX_DEF)
121155093Smarius#define	LE_LOCK_INITIALIZED(_sc)	mtx_initialized(&(_sc)->sc_mtx)
122155093Smarius#define	LE_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
123155093Smarius#define	LE_UNLOCK(_sc)			mtx_unlock(&(_sc)->sc_mtx)
124155093Smarius#define	LE_LOCK_ASSERT(_sc, _what)	mtx_assert(&(_sc)->sc_mtx, (_what))
125155093Smarius#define	LE_LOCK_DESTROY(_sc)		mtx_destroy(&(_sc)->sc_mtx)
126155093Smarius
127158663Smarius/*
128158663Smarius * Unfortunately, manual byte swapping is only necessary for the PCnet-PCI
129158663Smarius * variants but not for the original LANCE or ILACC so we cannot do this
130158663Smarius * with #ifdefs resolved at compile time.
131158663Smarius */
132155093Smarius#define	LE_HTOLE16(v)	(((sc)->sc_flags & LE_BSWAP) ? htole16(v) : (v))
133155093Smarius#define	LE_HTOLE32(v)	(((sc)->sc_flags & LE_BSWAP) ? htole32(v) : (v))
134155093Smarius#define	LE_LE16TOH(v)	(((sc)->sc_flags & LE_BSWAP) ? le16toh(v) : (v))
135155093Smarius#define	LE_LE32TOH(v)	(((sc)->sc_flags & LE_BSWAP) ? le32toh(v) : (v))
136155093Smarius
137155093Smariusint lance_config(struct lance_softc *, const char*, int);
138155093Smariusvoid lance_attach(struct lance_softc *);
139155093Smariusvoid lance_detach(struct lance_softc *);
140155093Smariusvoid lance_suspend(struct lance_softc *);
141155093Smariusvoid lance_resume(struct lance_softc *);
142155093Smariusvoid lance_init_locked(struct lance_softc *);
143155093Smariusint lance_put(struct lance_softc *, int, struct mbuf *);
144158663Smariusstruct mbuf *lance_get(struct lance_softc *, int, int);
145155093Smariusvoid lance_setladrf(struct lance_softc *, u_int16_t *);
146155093Smarius
147155093Smarius/*
148155093Smarius * The following functions are only useful on certain CPU/bus
149155093Smarius * combinations.  They should be written in assembly language for
150155093Smarius * maximum efficiency, but machine-independent versions are provided
151155093Smarius * for drivers that have not yet been optimized.
152155093Smarius */
153155093Smariusvoid lance_copytobuf_contig(struct lance_softc *, void *, int, int);
154155093Smariusvoid lance_copyfrombuf_contig(struct lance_softc *, void *, int, int);
155155093Smariusvoid lance_zerobuf_contig(struct lance_softc *, int, int);
156155093Smarius
157155093Smarius#if 0	/* Example only - see lance.c */
158155093Smariusvoid lance_copytobuf_gap2(struct lance_softc *, void *, int, int);
159155093Smariusvoid lance_copyfrombuf_gap2(struct lance_softc *, void *, int, int);
160155093Smariusvoid lance_zerobuf_gap2(struct lance_softc *, int, int);
161155093Smarius
162155093Smariusvoid lance_copytobuf_gap16(struct lance_softc *, void *, int, int);
163155093Smariusvoid lance_copyfrombuf_gap16(struct lance_softc *, void *, int, int);
164155093Smariusvoid lance_zerobuf_gap16(struct lance_softc *, int, int);
165155093Smarius#endif /* Example only */
166155093Smarius
167158663Smarius/*
168158663Smarius * Compare two Ether/802 addresses for equality, inlined and
169158663Smarius * unrolled for speed.  Use this like memcmp().
170158663Smarius *
171158663Smarius * XXX: Add <machine/inlines.h> for stuff like this?
172158663Smarius * XXX: or maybe add it to libkern.h instead?
173158663Smarius *
174158663Smarius * "I'd love to have an inline assembler version of this."
175158663Smarius * XXX: Who wanted that? mycroft?  I wrote one, but this
176158663Smarius * version in C is as good as hand-coded assembly. -gwr
177158663Smarius *
178158663Smarius * Please do NOT tweak this without looking at the actual
179158663Smarius * assembly code generated before and after your tweaks!
180158663Smarius */
181158663Smariusstatic inline uint16_t
182158663Smariusether_cmp(void *one, void *two)
183158663Smarius{
184158663Smarius	uint16_t *a = (u_short *)one;
185158663Smarius	uint16_t *b = (u_short *)two;
186158663Smarius	uint16_t diff;
187158663Smarius
188158663Smarius#ifdef	m68k
189158663Smarius	/*
190158663Smarius	 * The post-increment-pointer form produces the best
191158663Smarius	 * machine code for m68k.  This was carefully tuned
192158663Smarius	 * so it compiles to just 8 short (2-byte) op-codes!
193158663Smarius	 */
194158663Smarius	diff  = *a++ - *b++;
195158663Smarius	diff |= *a++ - *b++;
196158663Smarius	diff |= *a++ - *b++;
197158663Smarius#else
198158663Smarius	/*
199158663Smarius	 * Most modern CPUs do better with a single expresion.
200158663Smarius	 * Note that short-cut evaluation is NOT helpful here,
201158663Smarius	 * because it just makes the code longer, not faster!
202158663Smarius	 */
203158663Smarius	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
204158663Smarius#endif
205158663Smarius
206158663Smarius	return (diff);
207158663Smarius}
208158663Smarius
209155093Smarius#endif /* _DEV_LE_LANCEVAR_H_ */
210