1a7ee7a7dSMarius Strobl /* $NetBSD: lancevar.h,v 1.10 2005/12/11 12:21:27 christos Exp $ */ 2a7ee7a7dSMarius Strobl 3a7ee7a7dSMarius Strobl /*- 4a7ee7a7dSMarius Strobl * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5a7ee7a7dSMarius Strobl * All rights reserved. 6a7ee7a7dSMarius Strobl * 7a7ee7a7dSMarius Strobl * This code is derived from software contributed to The NetBSD Foundation 8a7ee7a7dSMarius Strobl * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 9a7ee7a7dSMarius Strobl * Simulation Facility, NASA Ames Research Center. 10a7ee7a7dSMarius Strobl * 11a7ee7a7dSMarius Strobl * Redistribution and use in source and binary forms, with or without 12a7ee7a7dSMarius Strobl * modification, are permitted provided that the following conditions 13a7ee7a7dSMarius Strobl * are met: 14a7ee7a7dSMarius Strobl * 1. Redistributions of source code must retain the above copyright 15a7ee7a7dSMarius Strobl * notice, this list of conditions and the following disclaimer. 16a7ee7a7dSMarius Strobl * 2. Redistributions in binary form must reproduce the above copyright 17a7ee7a7dSMarius Strobl * notice, this list of conditions and the following disclaimer in the 18a7ee7a7dSMarius Strobl * documentation and/or other materials provided with the distribution. 19a7ee7a7dSMarius Strobl * 3. All advertising materials mentioning features or use of this software 20a7ee7a7dSMarius Strobl * must display the following acknowledgement: 21a7ee7a7dSMarius Strobl * This product includes software developed by the NetBSD 22a7ee7a7dSMarius Strobl * Foundation, Inc. and its contributors. 23a7ee7a7dSMarius Strobl * 4. Neither the name of The NetBSD Foundation nor the names of its 24a7ee7a7dSMarius Strobl * contributors may be used to endorse or promote products derived 25a7ee7a7dSMarius Strobl * from this software without specific prior written permission. 26a7ee7a7dSMarius Strobl * 27a7ee7a7dSMarius Strobl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28a7ee7a7dSMarius Strobl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29a7ee7a7dSMarius Strobl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30a7ee7a7dSMarius Strobl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31a7ee7a7dSMarius Strobl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32a7ee7a7dSMarius Strobl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33a7ee7a7dSMarius Strobl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34a7ee7a7dSMarius Strobl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35a7ee7a7dSMarius Strobl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36a7ee7a7dSMarius Strobl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37a7ee7a7dSMarius Strobl * POSSIBILITY OF SUCH DAMAGE. 38a7ee7a7dSMarius Strobl */ 39a7ee7a7dSMarius Strobl 40a7ee7a7dSMarius Strobl /* $FreeBSD$ */ 41a7ee7a7dSMarius Strobl 42a7ee7a7dSMarius Strobl #ifndef _DEV_LE_LANCEVAR_H_ 43a7ee7a7dSMarius Strobl #define _DEV_LE_LANCEVAR_H_ 44a7ee7a7dSMarius Strobl 45a7ee7a7dSMarius Strobl extern devclass_t le_devclass; 46a7ee7a7dSMarius Strobl 47a7ee7a7dSMarius Strobl struct lance_softc { 48a7ee7a7dSMarius Strobl struct ifnet *sc_ifp; 49a7ee7a7dSMarius Strobl struct ifmedia sc_media; 50a7ee7a7dSMarius Strobl struct mtx sc_mtx; 51a7ee7a7dSMarius Strobl 52a7ee7a7dSMarius Strobl /* 53a7ee7a7dSMarius Strobl * Memory functions: 54a7ee7a7dSMarius Strobl * 55a7ee7a7dSMarius Strobl * copy to/from descriptor 56a7ee7a7dSMarius Strobl * copy to/from buffer 57a7ee7a7dSMarius Strobl * zero bytes in buffer 58a7ee7a7dSMarius Strobl */ 59a7ee7a7dSMarius Strobl void (*sc_copytodesc)(struct lance_softc *, void *, int, int); 60a7ee7a7dSMarius Strobl void (*sc_copyfromdesc)(struct lance_softc *, void *, int, int); 61a7ee7a7dSMarius Strobl void (*sc_copytobuf)(struct lance_softc *, void *, int, int); 62a7ee7a7dSMarius Strobl void (*sc_copyfrombuf)(struct lance_softc *, void *, int, int); 63a7ee7a7dSMarius Strobl void (*sc_zerobuf)(struct lance_softc *, int, int); 64a7ee7a7dSMarius Strobl 65a7ee7a7dSMarius Strobl /* 66a7ee7a7dSMarius Strobl * Machine-dependent functions: 67a7ee7a7dSMarius Strobl * 68a7ee7a7dSMarius Strobl * read/write CSR 69a7ee7a7dSMarius Strobl * hardware reset hook - may be NULL 70a7ee7a7dSMarius Strobl * hardware init hook - may be NULL 71a7ee7a7dSMarius Strobl * no carrier hook - may be NULL 72a7ee7a7dSMarius Strobl * media change hook - may be NULL 73a7ee7a7dSMarius Strobl */ 74a7ee7a7dSMarius Strobl uint16_t (*sc_rdcsr)(struct lance_softc *, uint16_t); 75a7ee7a7dSMarius Strobl void (*sc_wrcsr)(struct lance_softc *, uint16_t, uint16_t); 76a7ee7a7dSMarius Strobl void (*sc_hwreset)(struct lance_softc *); 77a7ee7a7dSMarius Strobl void (*sc_hwinit)(struct lance_softc *); 78a7ee7a7dSMarius Strobl int (*sc_hwintr)(struct lance_softc *); 79a7ee7a7dSMarius Strobl void (*sc_nocarrier)(struct lance_softc *); 80a7ee7a7dSMarius Strobl int (*sc_mediachange)(struct lance_softc *); 81a7ee7a7dSMarius Strobl void (*sc_mediastatus)(struct lance_softc *, struct ifmediareq *); 82a7ee7a7dSMarius Strobl 83a7ee7a7dSMarius Strobl /* 84a7ee7a7dSMarius Strobl * Media-supported by this interface. If this is NULL, 85a7ee7a7dSMarius Strobl * the only supported media is assumed to be "manual". 86a7ee7a7dSMarius Strobl */ 87a7ee7a7dSMarius Strobl const int *sc_supmedia; 88a7ee7a7dSMarius Strobl int sc_nsupmedia; 89a7ee7a7dSMarius Strobl int sc_defaultmedia; 90a7ee7a7dSMarius Strobl 91a7ee7a7dSMarius Strobl uint16_t sc_conf3; /* CSR3 value */ 92a7ee7a7dSMarius Strobl 9360c430f5SMarius Strobl void *sc_mem; /* base address of RAM - CPU's view */ 9460c430f5SMarius Strobl bus_addr_t sc_addr; /* base address of RAM - LANCE's view */ 95a7ee7a7dSMarius Strobl 9660c430f5SMarius Strobl bus_size_t sc_memsize; /* size of RAM */ 97a7ee7a7dSMarius Strobl 98a7ee7a7dSMarius Strobl int sc_nrbuf; /* number of receive buffers */ 99a7ee7a7dSMarius Strobl int sc_ntbuf; /* number of transmit buffers */ 100a7ee7a7dSMarius Strobl int sc_last_rd; 101a7ee7a7dSMarius Strobl int sc_first_td; 102a7ee7a7dSMarius Strobl int sc_last_td; 103a7ee7a7dSMarius Strobl int sc_no_td; 104a7ee7a7dSMarius Strobl 105a7ee7a7dSMarius Strobl int sc_initaddr; 106a7ee7a7dSMarius Strobl int sc_rmdaddr; 107a7ee7a7dSMarius Strobl int sc_tmdaddr; 108a7ee7a7dSMarius Strobl int sc_rbufaddr; 109a7ee7a7dSMarius Strobl int sc_tbufaddr; 110a7ee7a7dSMarius Strobl 111a7ee7a7dSMarius Strobl uint8_t sc_enaddr[ETHER_ADDR_LEN]; 112a7ee7a7dSMarius Strobl 113a7ee7a7dSMarius Strobl void (*sc_meminit)(struct lance_softc *); 114a7ee7a7dSMarius Strobl void (*sc_start_locked)(struct lance_softc *); 115a7ee7a7dSMarius Strobl 116a7ee7a7dSMarius Strobl int sc_flags; 117a7ee7a7dSMarius Strobl #define LE_ALLMULTI (1 << 0) 118a7ee7a7dSMarius Strobl #define LE_BSWAP (1 << 1) 119a7ee7a7dSMarius Strobl #define LE_CARRIER (1 << 2) 120a7ee7a7dSMarius Strobl #define LE_DEBUG (1 << 3) 121a7ee7a7dSMarius Strobl #define LE_PROMISC (1 << 4) 122a7ee7a7dSMarius Strobl }; 123a7ee7a7dSMarius Strobl 124a7ee7a7dSMarius Strobl #define LE_LOCK_INIT(_sc, _name) \ 125a7ee7a7dSMarius Strobl mtx_init(&(_sc)->sc_mtx, _name, MTX_NETWORK_LOCK, MTX_DEF) 126a7ee7a7dSMarius Strobl #define LE_LOCK_INITIALIZED(_sc) mtx_initialized(&(_sc)->sc_mtx) 127a7ee7a7dSMarius Strobl #define LE_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 128a7ee7a7dSMarius Strobl #define LE_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 129a7ee7a7dSMarius Strobl #define LE_LOCK_ASSERT(_sc, _what) mtx_assert(&(_sc)->sc_mtx, (_what)) 130a7ee7a7dSMarius Strobl #define LE_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 131a7ee7a7dSMarius Strobl 13260c430f5SMarius Strobl /* 13360c430f5SMarius Strobl * Unfortunately, manual byte swapping is only necessary for the PCnet-PCI 13460c430f5SMarius Strobl * variants but not for the original LANCE or ILACC so we cannot do this 13560c430f5SMarius Strobl * with #ifdefs resolved at compile time. 13660c430f5SMarius Strobl */ 137a7ee7a7dSMarius Strobl #define LE_HTOLE16(v) (((sc)->sc_flags & LE_BSWAP) ? htole16(v) : (v)) 138a7ee7a7dSMarius Strobl #define LE_HTOLE32(v) (((sc)->sc_flags & LE_BSWAP) ? htole32(v) : (v)) 139a7ee7a7dSMarius Strobl #define LE_LE16TOH(v) (((sc)->sc_flags & LE_BSWAP) ? le16toh(v) : (v)) 140a7ee7a7dSMarius Strobl #define LE_LE32TOH(v) (((sc)->sc_flags & LE_BSWAP) ? le32toh(v) : (v)) 141a7ee7a7dSMarius Strobl 142a7ee7a7dSMarius Strobl int lance_config(struct lance_softc *, const char*, int); 143a7ee7a7dSMarius Strobl void lance_attach(struct lance_softc *); 144a7ee7a7dSMarius Strobl void lance_detach(struct lance_softc *); 145a7ee7a7dSMarius Strobl void lance_suspend(struct lance_softc *); 146a7ee7a7dSMarius Strobl void lance_resume(struct lance_softc *); 147a7ee7a7dSMarius Strobl void lance_init_locked(struct lance_softc *); 148a7ee7a7dSMarius Strobl int lance_put(struct lance_softc *, int, struct mbuf *); 14960c430f5SMarius Strobl struct mbuf *lance_get(struct lance_softc *, int, int); 150a7ee7a7dSMarius Strobl void lance_setladrf(struct lance_softc *, u_int16_t *); 151a7ee7a7dSMarius Strobl 152a7ee7a7dSMarius Strobl /* 153a7ee7a7dSMarius Strobl * The following functions are only useful on certain CPU/bus 154a7ee7a7dSMarius Strobl * combinations. They should be written in assembly language for 155a7ee7a7dSMarius Strobl * maximum efficiency, but machine-independent versions are provided 156a7ee7a7dSMarius Strobl * for drivers that have not yet been optimized. 157a7ee7a7dSMarius Strobl */ 158a7ee7a7dSMarius Strobl void lance_copytobuf_contig(struct lance_softc *, void *, int, int); 159a7ee7a7dSMarius Strobl void lance_copyfrombuf_contig(struct lance_softc *, void *, int, int); 160a7ee7a7dSMarius Strobl void lance_zerobuf_contig(struct lance_softc *, int, int); 161a7ee7a7dSMarius Strobl 162a7ee7a7dSMarius Strobl #if 0 /* Example only - see lance.c */ 163a7ee7a7dSMarius Strobl void lance_copytobuf_gap2(struct lance_softc *, void *, int, int); 164a7ee7a7dSMarius Strobl void lance_copyfrombuf_gap2(struct lance_softc *, void *, int, int); 165a7ee7a7dSMarius Strobl void lance_zerobuf_gap2(struct lance_softc *, int, int); 166a7ee7a7dSMarius Strobl 167a7ee7a7dSMarius Strobl void lance_copytobuf_gap16(struct lance_softc *, void *, int, int); 168a7ee7a7dSMarius Strobl void lance_copyfrombuf_gap16(struct lance_softc *, void *, int, int); 169a7ee7a7dSMarius Strobl void lance_zerobuf_gap16(struct lance_softc *, int, int); 170a7ee7a7dSMarius Strobl #endif /* Example only */ 171a7ee7a7dSMarius Strobl 17260c430f5SMarius Strobl /* 17360c430f5SMarius Strobl * Compare two Ether/802 addresses for equality, inlined and 17460c430f5SMarius Strobl * unrolled for speed. Use this like memcmp(). 17560c430f5SMarius Strobl * 17660c430f5SMarius Strobl * XXX: Add <machine/inlines.h> for stuff like this? 17760c430f5SMarius Strobl * XXX: or maybe add it to libkern.h instead? 17860c430f5SMarius Strobl * 17960c430f5SMarius Strobl * "I'd love to have an inline assembler version of this." 18060c430f5SMarius Strobl * XXX: Who wanted that? mycroft? I wrote one, but this 18160c430f5SMarius Strobl * version in C is as good as hand-coded assembly. -gwr 18260c430f5SMarius Strobl * 18360c430f5SMarius Strobl * Please do NOT tweak this without looking at the actual 18460c430f5SMarius Strobl * assembly code generated before and after your tweaks! 18560c430f5SMarius Strobl */ 18660c430f5SMarius Strobl static inline uint16_t 18760c430f5SMarius Strobl ether_cmp(void *one, void *two) 18860c430f5SMarius Strobl { 18960c430f5SMarius Strobl uint16_t *a = (u_short *)one; 19060c430f5SMarius Strobl uint16_t *b = (u_short *)two; 19160c430f5SMarius Strobl uint16_t diff; 19260c430f5SMarius Strobl 19360c430f5SMarius Strobl #ifdef m68k 19460c430f5SMarius Strobl /* 19560c430f5SMarius Strobl * The post-increment-pointer form produces the best 19660c430f5SMarius Strobl * machine code for m68k. This was carefully tuned 19760c430f5SMarius Strobl * so it compiles to just 8 short (2-byte) op-codes! 19860c430f5SMarius Strobl */ 19960c430f5SMarius Strobl diff = *a++ - *b++; 20060c430f5SMarius Strobl diff |= *a++ - *b++; 20160c430f5SMarius Strobl diff |= *a++ - *b++; 20260c430f5SMarius Strobl #else 20360c430f5SMarius Strobl /* 20460c430f5SMarius Strobl * Most modern CPUs do better with a single expresion. 20560c430f5SMarius Strobl * Note that short-cut evaluation is NOT helpful here, 20660c430f5SMarius Strobl * because it just makes the code longer, not faster! 20760c430f5SMarius Strobl */ 20860c430f5SMarius Strobl diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]); 20960c430f5SMarius Strobl #endif 21060c430f5SMarius Strobl 21160c430f5SMarius Strobl return (diff); 21260c430f5SMarius Strobl } 21360c430f5SMarius Strobl 214a7ee7a7dSMarius Strobl #endif /* _DEV_LE_LANCEVAR_H_ */ 215