1a7ee7a7dSMarius Strobl /* $NetBSD: lance.c,v 1.34 2005/12/24 20:27:30 perry Exp $ */ 2a7ee7a7dSMarius Strobl 3a7ee7a7dSMarius Strobl /*- 4718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-NetBSD 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 /*- 36a7ee7a7dSMarius Strobl * Copyright (c) 1992, 1993 37a7ee7a7dSMarius Strobl * The Regents of the University of California. All rights reserved. 38a7ee7a7dSMarius Strobl * 39a7ee7a7dSMarius Strobl * This code is derived from software contributed to Berkeley by 40a7ee7a7dSMarius Strobl * Ralph Campbell and Rick Macklem. 41a7ee7a7dSMarius Strobl * 42a7ee7a7dSMarius Strobl * Redistribution and use in source and binary forms, with or without 43a7ee7a7dSMarius Strobl * modification, are permitted provided that the following conditions 44a7ee7a7dSMarius Strobl * are met: 45a7ee7a7dSMarius Strobl * 1. Redistributions of source code must retain the above copyright 46a7ee7a7dSMarius Strobl * notice, this list of conditions and the following disclaimer. 47a7ee7a7dSMarius Strobl * 2. Redistributions in binary form must reproduce the above copyright 48a7ee7a7dSMarius Strobl * notice, this list of conditions and the following disclaimer in the 49a7ee7a7dSMarius Strobl * documentation and/or other materials provided with the distribution. 50a7ee7a7dSMarius Strobl * 3. Neither the name of the University nor the names of its contributors 51a7ee7a7dSMarius Strobl * may be used to endorse or promote products derived from this software 52a7ee7a7dSMarius Strobl * without specific prior written permission. 53a7ee7a7dSMarius Strobl * 54a7ee7a7dSMarius Strobl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55a7ee7a7dSMarius Strobl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56a7ee7a7dSMarius Strobl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57a7ee7a7dSMarius Strobl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58a7ee7a7dSMarius Strobl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59a7ee7a7dSMarius Strobl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60a7ee7a7dSMarius Strobl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61a7ee7a7dSMarius Strobl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62a7ee7a7dSMarius Strobl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63a7ee7a7dSMarius Strobl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64a7ee7a7dSMarius Strobl * SUCH DAMAGE. 65a7ee7a7dSMarius Strobl * 66a7ee7a7dSMarius Strobl * @(#)if_le.c 8.2 (Berkeley) 11/16/93 67a7ee7a7dSMarius Strobl */ 68a7ee7a7dSMarius Strobl 69a7ee7a7dSMarius Strobl #include <sys/cdefs.h> 70a7ee7a7dSMarius Strobl __FBSDID("$FreeBSD$"); 71a7ee7a7dSMarius Strobl 72a7ee7a7dSMarius Strobl #include <sys/param.h> 73a7ee7a7dSMarius Strobl #include <sys/bus.h> 74a7ee7a7dSMarius Strobl #include <sys/endian.h> 75a7ee7a7dSMarius Strobl #include <sys/lock.h> 76fc64bae4SMarius Strobl #include <sys/kernel.h> 778ec07310SGleb Smirnoff #include <sys/malloc.h> 78a7ee7a7dSMarius Strobl #include <sys/mbuf.h> 79a7ee7a7dSMarius Strobl #include <sys/mutex.h> 80a7ee7a7dSMarius Strobl #include <sys/socket.h> 81a7ee7a7dSMarius Strobl #include <sys/sockio.h> 82a7ee7a7dSMarius Strobl 83a7ee7a7dSMarius Strobl #include <net/ethernet.h> 84a7ee7a7dSMarius Strobl #include <net/if.h> 8576039bc8SGleb Smirnoff #include <net/if_var.h> 86a7ee7a7dSMarius Strobl #include <net/if_arp.h> 87a7ee7a7dSMarius Strobl #include <net/if_dl.h> 88a7ee7a7dSMarius Strobl #include <net/if_media.h> 89a7ee7a7dSMarius Strobl #include <net/if_types.h> 90a7ee7a7dSMarius Strobl #include <net/if_vlan_var.h> 91a7ee7a7dSMarius Strobl 9260c430f5SMarius Strobl #include <machine/bus.h> 9360c430f5SMarius Strobl 94a7ee7a7dSMarius Strobl #include <dev/le/lancereg.h> 95a7ee7a7dSMarius Strobl #include <dev/le/lancevar.h> 96a7ee7a7dSMarius Strobl 97a7ee7a7dSMarius Strobl devclass_t le_devclass; 98a7ee7a7dSMarius Strobl 99a7ee7a7dSMarius Strobl static void lance_start(struct ifnet *); 100a7ee7a7dSMarius Strobl static void lance_stop(struct lance_softc *); 101a7ee7a7dSMarius Strobl static void lance_init(void *); 102fc64bae4SMarius Strobl static void lance_watchdog(void *s); 103a7ee7a7dSMarius Strobl static int lance_mediachange(struct ifnet *); 104a7ee7a7dSMarius Strobl static void lance_mediastatus(struct ifnet *, struct ifmediareq *); 105a7ee7a7dSMarius Strobl static int lance_ioctl(struct ifnet *, u_long, caddr_t); 106a7ee7a7dSMarius Strobl 107a7ee7a7dSMarius Strobl int 108a7ee7a7dSMarius Strobl lance_config(struct lance_softc *sc, const char* name, int unit) 109a7ee7a7dSMarius Strobl { 110a7ee7a7dSMarius Strobl struct ifnet *ifp; 111a7ee7a7dSMarius Strobl int i, nbuf; 112a7ee7a7dSMarius Strobl 113a7ee7a7dSMarius Strobl if (LE_LOCK_INITIALIZED(sc) == 0) 114a7ee7a7dSMarius Strobl return (ENXIO); 115a7ee7a7dSMarius Strobl 116a7ee7a7dSMarius Strobl ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 117a7ee7a7dSMarius Strobl if (ifp == NULL) 118a7ee7a7dSMarius Strobl return (ENOSPC); 119a7ee7a7dSMarius Strobl 120fc64bae4SMarius Strobl callout_init_mtx(&sc->sc_wdog_ch, &sc->sc_mtx, 0); 121fc64bae4SMarius Strobl 122a7ee7a7dSMarius Strobl /* Initialize ifnet structure. */ 123a7ee7a7dSMarius Strobl ifp->if_softc = sc; 124a7ee7a7dSMarius Strobl if_initname(ifp, name, unit); 125a7ee7a7dSMarius Strobl ifp->if_start = lance_start; 126a7ee7a7dSMarius Strobl ifp->if_ioctl = lance_ioctl; 127a7ee7a7dSMarius Strobl ifp->if_init = lance_init; 128a7ee7a7dSMarius Strobl ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 129a7ee7a7dSMarius Strobl #ifdef LANCE_REVC_BUG 130a7ee7a7dSMarius Strobl ifp->if_flags &= ~IFF_MULTICAST; 131a7ee7a7dSMarius Strobl #endif 132a7ee7a7dSMarius Strobl ifp->if_baudrate = IF_Mbps(10); 133e50d35e6SMaxim Sobolev IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 134e50d35e6SMaxim Sobolev ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 135a7ee7a7dSMarius Strobl IFQ_SET_READY(&ifp->if_snd); 136a7ee7a7dSMarius Strobl 137a7ee7a7dSMarius Strobl /* Initialize ifmedia structures. */ 138a7ee7a7dSMarius Strobl ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus); 139a7ee7a7dSMarius Strobl if (sc->sc_supmedia != NULL) { 140a7ee7a7dSMarius Strobl for (i = 0; i < sc->sc_nsupmedia; i++) 141a7ee7a7dSMarius Strobl ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL); 142a7ee7a7dSMarius Strobl ifmedia_set(&sc->sc_media, sc->sc_defaultmedia); 143a7ee7a7dSMarius Strobl } else { 144a7ee7a7dSMarius Strobl ifmedia_add(&sc->sc_media, 145a7ee7a7dSMarius Strobl IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL); 146a7ee7a7dSMarius Strobl ifmedia_set(&sc->sc_media, 147a7ee7a7dSMarius Strobl IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0)); 148a7ee7a7dSMarius Strobl } 149a7ee7a7dSMarius Strobl 150a7ee7a7dSMarius Strobl switch (sc->sc_memsize) { 151a7ee7a7dSMarius Strobl case 8192: 152a7ee7a7dSMarius Strobl sc->sc_nrbuf = 4; 153a7ee7a7dSMarius Strobl sc->sc_ntbuf = 1; 154a7ee7a7dSMarius Strobl break; 155a7ee7a7dSMarius Strobl case 16384: 156a7ee7a7dSMarius Strobl sc->sc_nrbuf = 8; 157a7ee7a7dSMarius Strobl sc->sc_ntbuf = 2; 158a7ee7a7dSMarius Strobl break; 159a7ee7a7dSMarius Strobl case 32768: 160a7ee7a7dSMarius Strobl sc->sc_nrbuf = 16; 161a7ee7a7dSMarius Strobl sc->sc_ntbuf = 4; 162a7ee7a7dSMarius Strobl break; 163a7ee7a7dSMarius Strobl case 65536: 164a7ee7a7dSMarius Strobl sc->sc_nrbuf = 32; 165a7ee7a7dSMarius Strobl sc->sc_ntbuf = 8; 166a7ee7a7dSMarius Strobl break; 167a7ee7a7dSMarius Strobl case 131072: 168a7ee7a7dSMarius Strobl sc->sc_nrbuf = 64; 169a7ee7a7dSMarius Strobl sc->sc_ntbuf = 16; 170a7ee7a7dSMarius Strobl break; 171a7ee7a7dSMarius Strobl case 262144: 172a7ee7a7dSMarius Strobl sc->sc_nrbuf = 128; 173a7ee7a7dSMarius Strobl sc->sc_ntbuf = 32; 174a7ee7a7dSMarius Strobl break; 175a7ee7a7dSMarius Strobl default: 176a7ee7a7dSMarius Strobl /* weird memory size; cope with it */ 177a7ee7a7dSMarius Strobl nbuf = sc->sc_memsize / LEBLEN; 178a7ee7a7dSMarius Strobl sc->sc_ntbuf = nbuf / 5; 179a7ee7a7dSMarius Strobl sc->sc_nrbuf = nbuf - sc->sc_ntbuf; 180a7ee7a7dSMarius Strobl } 181a7ee7a7dSMarius Strobl 182a7ee7a7dSMarius Strobl if_printf(ifp, "%d receive buffers, %d transmit buffers\n", 183a7ee7a7dSMarius Strobl sc->sc_nrbuf, sc->sc_ntbuf); 184a7ee7a7dSMarius Strobl 185a7ee7a7dSMarius Strobl /* Make sure the chip is stopped. */ 186a7ee7a7dSMarius Strobl LE_LOCK(sc); 187a7ee7a7dSMarius Strobl lance_stop(sc); 188a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 189a7ee7a7dSMarius Strobl 190a7ee7a7dSMarius Strobl return (0); 191a7ee7a7dSMarius Strobl } 192a7ee7a7dSMarius Strobl 193a7ee7a7dSMarius Strobl void 194a7ee7a7dSMarius Strobl lance_attach(struct lance_softc *sc) 195a7ee7a7dSMarius Strobl { 196a7ee7a7dSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 197a7ee7a7dSMarius Strobl 198a7ee7a7dSMarius Strobl /* Attach the interface. */ 199a7ee7a7dSMarius Strobl ether_ifattach(ifp, sc->sc_enaddr); 200a7ee7a7dSMarius Strobl 201a7ee7a7dSMarius Strobl /* Claim 802.1q capability. */ 2021bffa951SGleb Smirnoff ifp->if_hdrlen = sizeof(struct ether_vlan_header); 203a7ee7a7dSMarius Strobl ifp->if_capabilities |= IFCAP_VLAN_MTU; 204a7ee7a7dSMarius Strobl ifp->if_capenable |= IFCAP_VLAN_MTU; 205a7ee7a7dSMarius Strobl } 206a7ee7a7dSMarius Strobl 207a7ee7a7dSMarius Strobl void 208a7ee7a7dSMarius Strobl lance_detach(struct lance_softc *sc) 209a7ee7a7dSMarius Strobl { 210a7ee7a7dSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 211a7ee7a7dSMarius Strobl 212a7ee7a7dSMarius Strobl LE_LOCK(sc); 213a7ee7a7dSMarius Strobl lance_stop(sc); 214a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 215fc64bae4SMarius Strobl callout_drain(&sc->sc_wdog_ch); 216a7ee7a7dSMarius Strobl ether_ifdetach(ifp); 217a7ee7a7dSMarius Strobl if_free(ifp); 218a7ee7a7dSMarius Strobl } 219a7ee7a7dSMarius Strobl 220a7ee7a7dSMarius Strobl void 221a7ee7a7dSMarius Strobl lance_suspend(struct lance_softc *sc) 222a7ee7a7dSMarius Strobl { 223a7ee7a7dSMarius Strobl 224a7ee7a7dSMarius Strobl LE_LOCK(sc); 225a7ee7a7dSMarius Strobl lance_stop(sc); 226a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 227a7ee7a7dSMarius Strobl } 228a7ee7a7dSMarius Strobl 229a7ee7a7dSMarius Strobl void 230a7ee7a7dSMarius Strobl lance_resume(struct lance_softc *sc) 231a7ee7a7dSMarius Strobl { 232a7ee7a7dSMarius Strobl 233a7ee7a7dSMarius Strobl LE_LOCK(sc); 234a7ee7a7dSMarius Strobl if (sc->sc_ifp->if_flags & IFF_UP) 235a7ee7a7dSMarius Strobl lance_init_locked(sc); 236a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 237a7ee7a7dSMarius Strobl } 238a7ee7a7dSMarius Strobl 239a7ee7a7dSMarius Strobl static void 240a7ee7a7dSMarius Strobl lance_start(struct ifnet *ifp) 241a7ee7a7dSMarius Strobl { 242a7ee7a7dSMarius Strobl struct lance_softc *sc = ifp->if_softc; 243a7ee7a7dSMarius Strobl 244a7ee7a7dSMarius Strobl LE_LOCK(sc); 245a7ee7a7dSMarius Strobl (*sc->sc_start_locked)(sc); 246a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 247a7ee7a7dSMarius Strobl } 248a7ee7a7dSMarius Strobl 249a7ee7a7dSMarius Strobl static void 250a7ee7a7dSMarius Strobl lance_stop(struct lance_softc *sc) 251a7ee7a7dSMarius Strobl { 252a7ee7a7dSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 253a7ee7a7dSMarius Strobl 254a7ee7a7dSMarius Strobl LE_LOCK_ASSERT(sc, MA_OWNED); 255a7ee7a7dSMarius Strobl 256a7ee7a7dSMarius Strobl /* 257a7ee7a7dSMarius Strobl * Mark the interface down and cancel the watchdog timer. 258a7ee7a7dSMarius Strobl */ 259a7ee7a7dSMarius Strobl ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 260fc64bae4SMarius Strobl callout_stop(&sc->sc_wdog_ch); 261fc64bae4SMarius Strobl sc->sc_wdog_timer = 0; 262a7ee7a7dSMarius Strobl 263a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); 264a7ee7a7dSMarius Strobl } 265a7ee7a7dSMarius Strobl 266a7ee7a7dSMarius Strobl static void 267a7ee7a7dSMarius Strobl lance_init(void *xsc) 268a7ee7a7dSMarius Strobl { 269a7ee7a7dSMarius Strobl struct lance_softc *sc = (struct lance_softc *)xsc; 270a7ee7a7dSMarius Strobl 271a7ee7a7dSMarius Strobl LE_LOCK(sc); 272a7ee7a7dSMarius Strobl lance_init_locked(sc); 273a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 274a7ee7a7dSMarius Strobl } 275a7ee7a7dSMarius Strobl 276a7ee7a7dSMarius Strobl /* 277a7ee7a7dSMarius Strobl * Initialization of interface; set up initialization block 278a7ee7a7dSMarius Strobl * and transmit/receive descriptor rings. 279a7ee7a7dSMarius Strobl */ 280a7ee7a7dSMarius Strobl void 281a7ee7a7dSMarius Strobl lance_init_locked(struct lance_softc *sc) 282a7ee7a7dSMarius Strobl { 283a7ee7a7dSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 284a7ee7a7dSMarius Strobl u_long a; 285a7ee7a7dSMarius Strobl int timo; 286a7ee7a7dSMarius Strobl 287a7ee7a7dSMarius Strobl LE_LOCK_ASSERT(sc, MA_OWNED); 288a7ee7a7dSMarius Strobl 289a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); 290a7ee7a7dSMarius Strobl DELAY(100); 291a7ee7a7dSMarius Strobl 292a7ee7a7dSMarius Strobl /* Newer LANCE chips have a reset register. */ 293a7ee7a7dSMarius Strobl if (sc->sc_hwreset) 294a7ee7a7dSMarius Strobl (*sc->sc_hwreset)(sc); 295a7ee7a7dSMarius Strobl 296a7ee7a7dSMarius Strobl /* Set the correct byte swapping mode, etc. */ 297a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3); 298a7ee7a7dSMarius Strobl 29917792f45SMarius Strobl /* Set the current media. This may require the chip to be stopped. */ 30017792f45SMarius Strobl if (sc->sc_mediachange) 30117792f45SMarius Strobl (void)(*sc->sc_mediachange)(sc); 30217792f45SMarius Strobl 303a7ee7a7dSMarius Strobl /* 304a7ee7a7dSMarius Strobl * Update our private copy of the Ethernet address. 305a7ee7a7dSMarius Strobl * We NEED the copy so we can ensure its alignment! 306a7ee7a7dSMarius Strobl */ 307a7ee7a7dSMarius Strobl memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN); 308a7ee7a7dSMarius Strobl 309a7ee7a7dSMarius Strobl /* Set up LANCE init block. */ 310a7ee7a7dSMarius Strobl (*sc->sc_meminit)(sc); 311a7ee7a7dSMarius Strobl 312a7ee7a7dSMarius Strobl /* Give LANCE the physical address of its init block. */ 313a7ee7a7dSMarius Strobl a = sc->sc_addr + LE_INITADDR(sc); 314a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff); 315a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16); 316a7ee7a7dSMarius Strobl 317a7ee7a7dSMarius Strobl /* Try to initialize the LANCE. */ 318a7ee7a7dSMarius Strobl DELAY(100); 319a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT); 320a7ee7a7dSMarius Strobl 321a7ee7a7dSMarius Strobl /* Wait for initialization to finish. */ 322a7ee7a7dSMarius Strobl for (timo = 100000; timo; timo--) 323a7ee7a7dSMarius Strobl if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) 324a7ee7a7dSMarius Strobl break; 325a7ee7a7dSMarius Strobl 326a7ee7a7dSMarius Strobl if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) { 327a7ee7a7dSMarius Strobl /* Start the LANCE. */ 328a7ee7a7dSMarius Strobl (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT); 329a7ee7a7dSMarius Strobl ifp->if_drv_flags |= IFF_DRV_RUNNING; 330a7ee7a7dSMarius Strobl ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 331fc64bae4SMarius Strobl sc->sc_wdog_timer = 0; 332fc64bae4SMarius Strobl callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc); 333a7ee7a7dSMarius Strobl (*sc->sc_start_locked)(sc); 334a7ee7a7dSMarius Strobl } else 335a7ee7a7dSMarius Strobl if_printf(ifp, "controller failed to initialize\n"); 336a7ee7a7dSMarius Strobl 337a7ee7a7dSMarius Strobl if (sc->sc_hwinit) 338a7ee7a7dSMarius Strobl (*sc->sc_hwinit)(sc); 339a7ee7a7dSMarius Strobl } 340a7ee7a7dSMarius Strobl 341a7ee7a7dSMarius Strobl /* 342a7ee7a7dSMarius Strobl * Routine to copy from mbuf chain to transmit buffer in 343a7ee7a7dSMarius Strobl * network buffer memory. 344a7ee7a7dSMarius Strobl */ 345a7ee7a7dSMarius Strobl int 346a7ee7a7dSMarius Strobl lance_put(struct lance_softc *sc, int boff, struct mbuf *m) 347a7ee7a7dSMarius Strobl { 348a7ee7a7dSMarius Strobl struct mbuf *n; 349a7ee7a7dSMarius Strobl int len, tlen = 0; 350a7ee7a7dSMarius Strobl 351a7ee7a7dSMarius Strobl LE_LOCK_ASSERT(sc, MA_OWNED); 352a7ee7a7dSMarius Strobl 353a7ee7a7dSMarius Strobl for (; m; m = n) { 354a7ee7a7dSMarius Strobl len = m->m_len; 355a7ee7a7dSMarius Strobl if (len == 0) { 356a7ee7a7dSMarius Strobl n = m_free(m); 357a7ee7a7dSMarius Strobl m = NULL; 358a7ee7a7dSMarius Strobl continue; 359a7ee7a7dSMarius Strobl } 360a7ee7a7dSMarius Strobl (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len); 361a7ee7a7dSMarius Strobl boff += len; 362a7ee7a7dSMarius Strobl tlen += len; 363a7ee7a7dSMarius Strobl n = m_free(m); 364a7ee7a7dSMarius Strobl m = NULL; 365a7ee7a7dSMarius Strobl } 366a7ee7a7dSMarius Strobl if (tlen < LEMINSIZE) { 367a7ee7a7dSMarius Strobl (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen); 368a7ee7a7dSMarius Strobl tlen = LEMINSIZE; 369a7ee7a7dSMarius Strobl } 370a7ee7a7dSMarius Strobl return (tlen); 371a7ee7a7dSMarius Strobl } 372a7ee7a7dSMarius Strobl 373a7ee7a7dSMarius Strobl /* 374a7ee7a7dSMarius Strobl * Pull data off an interface. 375a7ee7a7dSMarius Strobl * Len is length of data, with local net header stripped. 376a7ee7a7dSMarius Strobl * We copy the data into mbufs. When full cluster sized units are present 377a7ee7a7dSMarius Strobl * we copy into clusters. 378a7ee7a7dSMarius Strobl */ 37960c430f5SMarius Strobl struct mbuf * 380a7ee7a7dSMarius Strobl lance_get(struct lance_softc *sc, int boff, int totlen) 381a7ee7a7dSMarius Strobl { 382a7ee7a7dSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 383a7ee7a7dSMarius Strobl struct mbuf *m, *m0, *newm; 384a7ee7a7dSMarius Strobl caddr_t newdata; 385a7ee7a7dSMarius Strobl int len; 386a7ee7a7dSMarius Strobl 38760c430f5SMarius Strobl if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) { 38860c430f5SMarius Strobl #ifdef LEDEBUG 38960c430f5SMarius Strobl if_printf(ifp, "invalid packet size %d; dropping\n", totlen); 39060c430f5SMarius Strobl #endif 39160c430f5SMarius Strobl return (NULL); 39260c430f5SMarius Strobl } 39360c430f5SMarius Strobl 394c6499eccSGleb Smirnoff MGETHDR(m0, M_NOWAIT, MT_DATA); 39560c430f5SMarius Strobl if (m0 == NULL) 39660c430f5SMarius Strobl return (NULL); 397a7ee7a7dSMarius Strobl m0->m_pkthdr.rcvif = ifp; 398a7ee7a7dSMarius Strobl m0->m_pkthdr.len = totlen; 399a7ee7a7dSMarius Strobl len = MHLEN; 400a7ee7a7dSMarius Strobl m = m0; 401a7ee7a7dSMarius Strobl 402a7ee7a7dSMarius Strobl while (totlen > 0) { 403a7ee7a7dSMarius Strobl if (totlen >= MINCLSIZE) { 4042a8c860fSRobert Watson if (!(MCLGET(m, M_NOWAIT))) 405a7ee7a7dSMarius Strobl goto bad; 406a7ee7a7dSMarius Strobl len = MCLBYTES; 407a7ee7a7dSMarius Strobl } 408a7ee7a7dSMarius Strobl 409a7ee7a7dSMarius Strobl if (m == m0) { 410a7ee7a7dSMarius Strobl newdata = (caddr_t) 411a7ee7a7dSMarius Strobl ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN; 412a7ee7a7dSMarius Strobl len -= newdata - m->m_data; 413a7ee7a7dSMarius Strobl m->m_data = newdata; 414a7ee7a7dSMarius Strobl } 415a7ee7a7dSMarius Strobl 416a7ee7a7dSMarius Strobl m->m_len = len = min(totlen, len); 417a7ee7a7dSMarius Strobl (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len); 418a7ee7a7dSMarius Strobl boff += len; 419a7ee7a7dSMarius Strobl 420a7ee7a7dSMarius Strobl totlen -= len; 421a7ee7a7dSMarius Strobl if (totlen > 0) { 422c6499eccSGleb Smirnoff MGET(newm, M_NOWAIT, MT_DATA); 4234d24901aSPedro F. Giffuni if (newm == NULL) 424a7ee7a7dSMarius Strobl goto bad; 425a7ee7a7dSMarius Strobl len = MLEN; 426a7ee7a7dSMarius Strobl m = m->m_next = newm; 427a7ee7a7dSMarius Strobl } 428a7ee7a7dSMarius Strobl } 429a7ee7a7dSMarius Strobl 430a7ee7a7dSMarius Strobl return (m0); 431a7ee7a7dSMarius Strobl 432a7ee7a7dSMarius Strobl bad: 433a7ee7a7dSMarius Strobl m_freem(m0); 43460c430f5SMarius Strobl return (NULL); 435a7ee7a7dSMarius Strobl } 436a7ee7a7dSMarius Strobl 437a7ee7a7dSMarius Strobl static void 438fc64bae4SMarius Strobl lance_watchdog(void *xsc) 439a7ee7a7dSMarius Strobl { 440fc64bae4SMarius Strobl struct lance_softc *sc = (struct lance_softc *)xsc; 441fc64bae4SMarius Strobl struct ifnet *ifp = sc->sc_ifp; 442a7ee7a7dSMarius Strobl 443fc64bae4SMarius Strobl LE_LOCK_ASSERT(sc, MA_OWNED); 444fc64bae4SMarius Strobl 445fc64bae4SMarius Strobl if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) { 446fc64bae4SMarius Strobl callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc); 447fc64bae4SMarius Strobl return; 448fc64bae4SMarius Strobl } 449fc64bae4SMarius Strobl 450a7ee7a7dSMarius Strobl if_printf(ifp, "device timeout\n"); 451c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 452a7ee7a7dSMarius Strobl lance_init_locked(sc); 453a7ee7a7dSMarius Strobl } 454a7ee7a7dSMarius Strobl 455a7ee7a7dSMarius Strobl static int 456a7ee7a7dSMarius Strobl lance_mediachange(struct ifnet *ifp) 457a7ee7a7dSMarius Strobl { 458a7ee7a7dSMarius Strobl struct lance_softc *sc = ifp->if_softc; 459a7ee7a7dSMarius Strobl 460a7ee7a7dSMarius Strobl if (sc->sc_mediachange) { 46117792f45SMarius Strobl /* 46217792f45SMarius Strobl * For setting the port in LE_CSR15 the PCnet chips must 46317792f45SMarius Strobl * be powered down or stopped and unlike documented may 46417792f45SMarius Strobl * not take effect without an initialization. So don't 46517792f45SMarius Strobl * invoke (*sc_mediachange) directly here but go through 46617792f45SMarius Strobl * lance_init_locked(). 46717792f45SMarius Strobl */ 468a7ee7a7dSMarius Strobl LE_LOCK(sc); 46917792f45SMarius Strobl lance_stop(sc); 47017792f45SMarius Strobl lance_init_locked(sc); 47117792f45SMarius Strobl if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 47217792f45SMarius Strobl (*sc->sc_start_locked)(sc); 473a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 474a7ee7a7dSMarius Strobl } 475a7ee7a7dSMarius Strobl return (0); 476a7ee7a7dSMarius Strobl } 477a7ee7a7dSMarius Strobl 478a7ee7a7dSMarius Strobl static void 479a7ee7a7dSMarius Strobl lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 480a7ee7a7dSMarius Strobl { 481a7ee7a7dSMarius Strobl struct lance_softc *sc = ifp->if_softc; 482a7ee7a7dSMarius Strobl 483a7ee7a7dSMarius Strobl LE_LOCK(sc); 484a7ee7a7dSMarius Strobl if (!(ifp->if_flags & IFF_UP)) { 485a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 486a7ee7a7dSMarius Strobl return; 487a7ee7a7dSMarius Strobl } 488a7ee7a7dSMarius Strobl 489a7ee7a7dSMarius Strobl ifmr->ifm_status = IFM_AVALID; 490a7ee7a7dSMarius Strobl if (sc->sc_flags & LE_CARRIER) 491a7ee7a7dSMarius Strobl ifmr->ifm_status |= IFM_ACTIVE; 492a7ee7a7dSMarius Strobl 493a7ee7a7dSMarius Strobl if (sc->sc_mediastatus) 494a7ee7a7dSMarius Strobl (*sc->sc_mediastatus)(sc, ifmr); 495a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 496a7ee7a7dSMarius Strobl } 497a7ee7a7dSMarius Strobl 498a7ee7a7dSMarius Strobl /* 499a7ee7a7dSMarius Strobl * Process an ioctl request. 500a7ee7a7dSMarius Strobl */ 501a7ee7a7dSMarius Strobl static int 502a7ee7a7dSMarius Strobl lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 503a7ee7a7dSMarius Strobl { 504a7ee7a7dSMarius Strobl struct lance_softc *sc = ifp->if_softc; 505a7ee7a7dSMarius Strobl struct ifreq *ifr = (struct ifreq *)data; 506a7ee7a7dSMarius Strobl int error = 0; 507a7ee7a7dSMarius Strobl 508a7ee7a7dSMarius Strobl switch (cmd) { 509a7ee7a7dSMarius Strobl case SIOCSIFFLAGS: 510a7ee7a7dSMarius Strobl LE_LOCK(sc); 511a7ee7a7dSMarius Strobl if (ifp->if_flags & IFF_PROMISC) { 512a7ee7a7dSMarius Strobl if (!(sc->sc_flags & LE_PROMISC)) { 513a7ee7a7dSMarius Strobl sc->sc_flags |= LE_PROMISC; 514a7ee7a7dSMarius Strobl lance_init_locked(sc); 515a7ee7a7dSMarius Strobl } 516a7ee7a7dSMarius Strobl } else if (sc->sc_flags & LE_PROMISC) { 517a7ee7a7dSMarius Strobl sc->sc_flags &= ~LE_PROMISC; 518a7ee7a7dSMarius Strobl lance_init_locked(sc); 519a7ee7a7dSMarius Strobl } 520a7ee7a7dSMarius Strobl 521a7ee7a7dSMarius Strobl if ((ifp->if_flags & IFF_ALLMULTI) && 522a7ee7a7dSMarius Strobl !(sc->sc_flags & LE_ALLMULTI)) { 523a7ee7a7dSMarius Strobl sc->sc_flags |= LE_ALLMULTI; 524a7ee7a7dSMarius Strobl lance_init_locked(sc); 525a7ee7a7dSMarius Strobl } else if (!(ifp->if_flags & IFF_ALLMULTI) && 526a7ee7a7dSMarius Strobl (sc->sc_flags & LE_ALLMULTI)) { 527a7ee7a7dSMarius Strobl sc->sc_flags &= ~LE_ALLMULTI; 528a7ee7a7dSMarius Strobl lance_init_locked(sc); 529a7ee7a7dSMarius Strobl } 530a7ee7a7dSMarius Strobl 531a7ee7a7dSMarius Strobl if (!(ifp->if_flags & IFF_UP) && 532a7ee7a7dSMarius Strobl ifp->if_drv_flags & IFF_DRV_RUNNING) { 533a7ee7a7dSMarius Strobl /* 534a7ee7a7dSMarius Strobl * If interface is marked down and it is running, then 535a7ee7a7dSMarius Strobl * stop it. 536a7ee7a7dSMarius Strobl */ 537a7ee7a7dSMarius Strobl lance_stop(sc); 538a7ee7a7dSMarius Strobl } else if (ifp->if_flags & IFF_UP && 539a7ee7a7dSMarius Strobl !(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 540a7ee7a7dSMarius Strobl /* 541a7ee7a7dSMarius Strobl * If interface is marked up and it is stopped, then 542a7ee7a7dSMarius Strobl * start it. 543a7ee7a7dSMarius Strobl */ 544a7ee7a7dSMarius Strobl lance_init_locked(sc); 545a7ee7a7dSMarius Strobl } 546a7ee7a7dSMarius Strobl #ifdef LEDEBUG 547a7ee7a7dSMarius Strobl if (ifp->if_flags & IFF_DEBUG) 548a7ee7a7dSMarius Strobl sc->sc_flags |= LE_DEBUG; 549a7ee7a7dSMarius Strobl else 550a7ee7a7dSMarius Strobl sc->sc_flags &= ~LE_DEBUG; 551a7ee7a7dSMarius Strobl #endif 552a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 553a7ee7a7dSMarius Strobl break; 554a7ee7a7dSMarius Strobl 555a7ee7a7dSMarius Strobl case SIOCADDMULTI: 556a7ee7a7dSMarius Strobl case SIOCDELMULTI: 557a7ee7a7dSMarius Strobl /* 558a7ee7a7dSMarius Strobl * Multicast list has changed; set the hardware filter 559a7ee7a7dSMarius Strobl * accordingly. 560a7ee7a7dSMarius Strobl */ 561a7ee7a7dSMarius Strobl LE_LOCK(sc); 562a7ee7a7dSMarius Strobl if (ifp->if_drv_flags & IFF_DRV_RUNNING) 563a7ee7a7dSMarius Strobl lance_init_locked(sc); 564a7ee7a7dSMarius Strobl LE_UNLOCK(sc); 565a7ee7a7dSMarius Strobl break; 566a7ee7a7dSMarius Strobl 567a7ee7a7dSMarius Strobl case SIOCGIFMEDIA: 568a7ee7a7dSMarius Strobl case SIOCSIFMEDIA: 569a7ee7a7dSMarius Strobl error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 570a7ee7a7dSMarius Strobl break; 571a7ee7a7dSMarius Strobl 572a7ee7a7dSMarius Strobl default: 573a7ee7a7dSMarius Strobl error = ether_ioctl(ifp, cmd, data); 574a7ee7a7dSMarius Strobl break; 575a7ee7a7dSMarius Strobl } 576a7ee7a7dSMarius Strobl 577a7ee7a7dSMarius Strobl return (error); 578a7ee7a7dSMarius Strobl } 579a7ee7a7dSMarius Strobl 580*208af059SGleb Smirnoff struct lance_hash_maddr_ctx { 581*208af059SGleb Smirnoff struct lance_softc *sc; 582*208af059SGleb Smirnoff uint16_t *af; 583*208af059SGleb Smirnoff }; 584*208af059SGleb Smirnoff 585*208af059SGleb Smirnoff static u_int 586*208af059SGleb Smirnoff lance_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt) 587*208af059SGleb Smirnoff { 588*208af059SGleb Smirnoff struct lance_hash_maddr_ctx *ctx = arg; 589*208af059SGleb Smirnoff struct lance_softc *sc = ctx->sc; 590*208af059SGleb Smirnoff uint32_t crc; 591*208af059SGleb Smirnoff 592*208af059SGleb Smirnoff crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN); 593*208af059SGleb Smirnoff /* Just want the 6 most significant bits. */ 594*208af059SGleb Smirnoff crc >>= 26; 595*208af059SGleb Smirnoff /* Set the corresponding bit in the filter. */ 596*208af059SGleb Smirnoff ctx->af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf)); 597*208af059SGleb Smirnoff 598*208af059SGleb Smirnoff return (1); 599*208af059SGleb Smirnoff } 600*208af059SGleb Smirnoff 601a7ee7a7dSMarius Strobl /* 602a7ee7a7dSMarius Strobl * Set up the logical address filter. 603a7ee7a7dSMarius Strobl */ 604a7ee7a7dSMarius Strobl void 605a7ee7a7dSMarius Strobl lance_setladrf(struct lance_softc *sc, uint16_t *af) 606a7ee7a7dSMarius Strobl { 607a7ee7a7dSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 608*208af059SGleb Smirnoff struct lance_hash_maddr_ctx ctx = { sc, af }; 609a7ee7a7dSMarius Strobl 610a7ee7a7dSMarius Strobl /* 611a7ee7a7dSMarius Strobl * Set up multicast address filter by passing all multicast addresses 612a7ee7a7dSMarius Strobl * through a crc generator, and then using the high order 6 bits as an 613a7ee7a7dSMarius Strobl * index into the 64 bit logical address filter. The high order bit 614a7ee7a7dSMarius Strobl * selects the word, while the rest of the bits select the bit within 615a7ee7a7dSMarius Strobl * the word. 616a7ee7a7dSMarius Strobl */ 617a7ee7a7dSMarius Strobl 618a7ee7a7dSMarius Strobl if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) { 619a7ee7a7dSMarius Strobl af[0] = af[1] = af[2] = af[3] = 0xffff; 620a7ee7a7dSMarius Strobl return; 621a7ee7a7dSMarius Strobl } 622a7ee7a7dSMarius Strobl 623a7ee7a7dSMarius Strobl af[0] = af[1] = af[2] = af[3] = 0x0000; 624*208af059SGleb Smirnoff if_foreach_llmaddr(ifp, lance_hash_maddr, &ctx); 625a7ee7a7dSMarius Strobl } 626a7ee7a7dSMarius Strobl 627a7ee7a7dSMarius Strobl /* 628a7ee7a7dSMarius Strobl * Routines for accessing the transmit and receive buffers. 629a7ee7a7dSMarius Strobl * The various CPU and adapter configurations supported by this 630a7ee7a7dSMarius Strobl * driver require three different access methods for buffers 631a7ee7a7dSMarius Strobl * and descriptors: 632a7ee7a7dSMarius Strobl * (1) contig (contiguous data; no padding), 633a7ee7a7dSMarius Strobl * (2) gap2 (two bytes of data followed by two bytes of padding), 634a7ee7a7dSMarius Strobl * (3) gap16 (16 bytes of data followed by 16 bytes of padding). 635a7ee7a7dSMarius Strobl */ 636a7ee7a7dSMarius Strobl 637a7ee7a7dSMarius Strobl /* 638a7ee7a7dSMarius Strobl * contig: contiguous data with no padding. 639a7ee7a7dSMarius Strobl * 640a7ee7a7dSMarius Strobl * Buffers may have any alignment. 641a7ee7a7dSMarius Strobl */ 642a7ee7a7dSMarius Strobl 643a7ee7a7dSMarius Strobl void 644a7ee7a7dSMarius Strobl lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len) 645a7ee7a7dSMarius Strobl { 646a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 647a7ee7a7dSMarius Strobl 648a7ee7a7dSMarius Strobl /* 649a7ee7a7dSMarius Strobl * Just call memcpy() to do the work. 650a7ee7a7dSMarius Strobl */ 651a7ee7a7dSMarius Strobl memcpy(buf + boff, from, len); 652a7ee7a7dSMarius Strobl } 653a7ee7a7dSMarius Strobl 654a7ee7a7dSMarius Strobl void 655a7ee7a7dSMarius Strobl lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len) 656a7ee7a7dSMarius Strobl { 657a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 658a7ee7a7dSMarius Strobl 659a7ee7a7dSMarius Strobl /* 660a7ee7a7dSMarius Strobl * Just call memcpy() to do the work. 661a7ee7a7dSMarius Strobl */ 662a7ee7a7dSMarius Strobl memcpy(to, buf + boff, len); 663a7ee7a7dSMarius Strobl } 664a7ee7a7dSMarius Strobl 665a7ee7a7dSMarius Strobl void 666a7ee7a7dSMarius Strobl lance_zerobuf_contig(struct lance_softc *sc, int boff, int len) 667a7ee7a7dSMarius Strobl { 668a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 669a7ee7a7dSMarius Strobl 670a7ee7a7dSMarius Strobl /* 671a7ee7a7dSMarius Strobl * Just let memset() do the work 672a7ee7a7dSMarius Strobl */ 673a7ee7a7dSMarius Strobl memset(buf + boff, 0, len); 674a7ee7a7dSMarius Strobl } 675a7ee7a7dSMarius Strobl 676a7ee7a7dSMarius Strobl #if 0 677a7ee7a7dSMarius Strobl /* 678a7ee7a7dSMarius Strobl * Examples only; duplicate these and tweak (if necessary) in 679a7ee7a7dSMarius Strobl * machine-specific front-ends. 680a7ee7a7dSMarius Strobl */ 681a7ee7a7dSMarius Strobl 682a7ee7a7dSMarius Strobl /* 683a7ee7a7dSMarius Strobl * gap2: two bytes of data followed by two bytes of pad. 684a7ee7a7dSMarius Strobl * 685a7ee7a7dSMarius Strobl * Buffers must be 4-byte aligned. The code doesn't worry about 686a7ee7a7dSMarius Strobl * doing an extra byte. 687a7ee7a7dSMarius Strobl */ 688a7ee7a7dSMarius Strobl 689a7ee7a7dSMarius Strobl static void 690a7ee7a7dSMarius Strobl lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len) 691a7ee7a7dSMarius Strobl { 692a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 693a7ee7a7dSMarius Strobl caddr_t from = fromv; 694a7ee7a7dSMarius Strobl volatile uint16_t *bptr; 695a7ee7a7dSMarius Strobl 696a7ee7a7dSMarius Strobl if (boff & 0x1) { 697a7ee7a7dSMarius Strobl /* Handle unaligned first byte. */ 698a7ee7a7dSMarius Strobl bptr = ((volatile uint16_t *)buf) + (boff - 1); 699a7ee7a7dSMarius Strobl *bptr = (*from++ << 8) | (*bptr & 0xff); 700a7ee7a7dSMarius Strobl bptr += 2; 701a7ee7a7dSMarius Strobl len--; 702a7ee7a7dSMarius Strobl } else 703a7ee7a7dSMarius Strobl bptr = ((volatile uint16_t *)buf) + boff; 704a7ee7a7dSMarius Strobl while (len > 1) { 705a7ee7a7dSMarius Strobl *bptr = (from[1] << 8) | (from[0] & 0xff); 706a7ee7a7dSMarius Strobl bptr += 2; 707a7ee7a7dSMarius Strobl from += 2; 708a7ee7a7dSMarius Strobl len -= 2; 709a7ee7a7dSMarius Strobl } 710a7ee7a7dSMarius Strobl if (len == 1) 711a7ee7a7dSMarius Strobl *bptr = (uint16_t)*from; 712a7ee7a7dSMarius Strobl } 713a7ee7a7dSMarius Strobl 714a7ee7a7dSMarius Strobl static void 715a7ee7a7dSMarius Strobl lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len) 716a7ee7a7dSMarius Strobl { 717a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 718a7ee7a7dSMarius Strobl caddr_t to = tov; 719a7ee7a7dSMarius Strobl volatile uint16_t *bptr; 720a7ee7a7dSMarius Strobl uint16_t tmp; 721a7ee7a7dSMarius Strobl 722a7ee7a7dSMarius Strobl if (boff & 0x1) { 723a7ee7a7dSMarius Strobl /* Handle unaligned first byte. */ 724a7ee7a7dSMarius Strobl bptr = ((volatile uint16_t *)buf) + (boff - 1); 725a7ee7a7dSMarius Strobl *to++ = (*bptr >> 8) & 0xff; 726a7ee7a7dSMarius Strobl bptr += 2; 727a7ee7a7dSMarius Strobl len--; 728a7ee7a7dSMarius Strobl } else 729a7ee7a7dSMarius Strobl bptr = ((volatile uint16_t *)buf) + boff; 730a7ee7a7dSMarius Strobl while (len > 1) { 731a7ee7a7dSMarius Strobl tmp = *bptr; 732a7ee7a7dSMarius Strobl *to++ = tmp & 0xff; 733a7ee7a7dSMarius Strobl *to++ = (tmp >> 8) & 0xff; 734a7ee7a7dSMarius Strobl bptr += 2; 735a7ee7a7dSMarius Strobl len -= 2; 736a7ee7a7dSMarius Strobl } 737a7ee7a7dSMarius Strobl if (len == 1) 738a7ee7a7dSMarius Strobl *to = *bptr & 0xff; 739a7ee7a7dSMarius Strobl } 740a7ee7a7dSMarius Strobl 741a7ee7a7dSMarius Strobl static void 742a7ee7a7dSMarius Strobl lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len) 743a7ee7a7dSMarius Strobl { 744a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 745a7ee7a7dSMarius Strobl volatile uint16_t *bptr; 746a7ee7a7dSMarius Strobl 747a7ee7a7dSMarius Strobl if ((unsigned)boff & 0x1) { 748a7ee7a7dSMarius Strobl bptr = ((volatile uint16_t *)buf) + (boff - 1); 749a7ee7a7dSMarius Strobl *bptr &= 0xff; 750a7ee7a7dSMarius Strobl bptr += 2; 751a7ee7a7dSMarius Strobl len--; 752a7ee7a7dSMarius Strobl } else 753a7ee7a7dSMarius Strobl bptr = ((volatile uint16_t *)buf) + boff; 754a7ee7a7dSMarius Strobl while (len > 0) { 755a7ee7a7dSMarius Strobl *bptr = 0; 756a7ee7a7dSMarius Strobl bptr += 2; 757a7ee7a7dSMarius Strobl len -= 2; 758a7ee7a7dSMarius Strobl } 759a7ee7a7dSMarius Strobl } 760a7ee7a7dSMarius Strobl 761a7ee7a7dSMarius Strobl /* 762a7ee7a7dSMarius Strobl * gap16: 16 bytes of data followed by 16 bytes of pad. 763a7ee7a7dSMarius Strobl * 764a7ee7a7dSMarius Strobl * Buffers must be 32-byte aligned. 765a7ee7a7dSMarius Strobl */ 766a7ee7a7dSMarius Strobl 767a7ee7a7dSMarius Strobl static void 768a7ee7a7dSMarius Strobl lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len) 769a7ee7a7dSMarius Strobl { 770a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 771a7ee7a7dSMarius Strobl caddr_t bptr, from = fromv; 772a7ee7a7dSMarius Strobl int xfer; 773a7ee7a7dSMarius Strobl 774a7ee7a7dSMarius Strobl bptr = buf + ((boff << 1) & ~0x1f); 775a7ee7a7dSMarius Strobl boff &= 0xf; 776a7ee7a7dSMarius Strobl xfer = min(len, 16 - boff); 777a7ee7a7dSMarius Strobl while (len > 0) { 778a7ee7a7dSMarius Strobl memcpy(bptr + boff, from, xfer); 779a7ee7a7dSMarius Strobl from += xfer; 780a7ee7a7dSMarius Strobl bptr += 32; 781a7ee7a7dSMarius Strobl boff = 0; 782a7ee7a7dSMarius Strobl len -= xfer; 783a7ee7a7dSMarius Strobl xfer = min(len, 16); 784a7ee7a7dSMarius Strobl } 785a7ee7a7dSMarius Strobl } 786a7ee7a7dSMarius Strobl 787a7ee7a7dSMarius Strobl static void 788a7ee7a7dSMarius Strobl lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len) 789a7ee7a7dSMarius Strobl { 790a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 791a7ee7a7dSMarius Strobl caddr_t bptr, to = tov; 792a7ee7a7dSMarius Strobl int xfer; 793a7ee7a7dSMarius Strobl 794a7ee7a7dSMarius Strobl bptr = buf + ((boff << 1) & ~0x1f); 795a7ee7a7dSMarius Strobl boff &= 0xf; 796a7ee7a7dSMarius Strobl xfer = min(len, 16 - boff); 797a7ee7a7dSMarius Strobl while (len > 0) { 798a7ee7a7dSMarius Strobl memcpy(to, bptr + boff, xfer); 799a7ee7a7dSMarius Strobl to += xfer; 800a7ee7a7dSMarius Strobl bptr += 32; 801a7ee7a7dSMarius Strobl boff = 0; 802a7ee7a7dSMarius Strobl len -= xfer; 803a7ee7a7dSMarius Strobl xfer = min(len, 16); 804a7ee7a7dSMarius Strobl } 805a7ee7a7dSMarius Strobl } 806a7ee7a7dSMarius Strobl 807a7ee7a7dSMarius Strobl static void 808a7ee7a7dSMarius Strobl lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len) 809a7ee7a7dSMarius Strobl { 810a7ee7a7dSMarius Strobl volatile caddr_t buf = sc->sc_mem; 811a7ee7a7dSMarius Strobl caddr_t bptr; 812a7ee7a7dSMarius Strobl int xfer; 813a7ee7a7dSMarius Strobl 814a7ee7a7dSMarius Strobl bptr = buf + ((boff << 1) & ~0x1f); 815a7ee7a7dSMarius Strobl boff &= 0xf; 816a7ee7a7dSMarius Strobl xfer = min(len, 16 - boff); 817a7ee7a7dSMarius Strobl while (len > 0) { 818a7ee7a7dSMarius Strobl memset(bptr + boff, 0, xfer); 819a7ee7a7dSMarius Strobl bptr += 32; 820a7ee7a7dSMarius Strobl boff = 0; 821a7ee7a7dSMarius Strobl len -= xfer; 822a7ee7a7dSMarius Strobl xfer = min(len, 16); 823a7ee7a7dSMarius Strobl } 824a7ee7a7dSMarius Strobl } 825a7ee7a7dSMarius Strobl #endif /* Example only */ 826