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