Home | History | Annotate | Line # | Download | only in qbus
if_uba.c revision 1.23
      1  1.23  thorpej /*	$NetBSD: if_uba.c,v 1.23 2003/04/01 02:06:06 thorpej Exp $	*/
      2   1.3      cgd 
      3   1.1    ragge /*
      4   1.1    ragge  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      5   1.1    ragge  * All rights reserved.
      6   1.1    ragge  *
      7   1.1    ragge  * Redistribution and use in source and binary forms, with or without
      8   1.1    ragge  * modification, are permitted provided that the following conditions
      9   1.1    ragge  * are met:
     10   1.1    ragge  * 1. Redistributions of source code must retain the above copyright
     11   1.1    ragge  *    notice, this list of conditions and the following disclaimer.
     12   1.1    ragge  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    ragge  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    ragge  *    documentation and/or other materials provided with the distribution.
     15   1.1    ragge  * 3. All advertising materials mentioning features or use of this software
     16   1.1    ragge  *    must display the following acknowledgement:
     17   1.1    ragge  *	This product includes software developed by the University of
     18   1.1    ragge  *	California, Berkeley and its contributors.
     19   1.1    ragge  * 4. Neither the name of the University nor the names of its contributors
     20   1.1    ragge  *    may be used to endorse or promote products derived from this software
     21   1.1    ragge  *    without specific prior written permission.
     22   1.1    ragge  *
     23   1.1    ragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1    ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1    ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1    ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1    ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1    ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1    ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1    ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1    ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1    ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1    ragge  * SUCH DAMAGE.
     34   1.1    ragge  *
     35   1.1    ragge  *	@(#)if_uba.c	7.16 (Berkeley) 12/16/90
     36   1.1    ragge  */
     37   1.1    ragge 
     38  1.22    lukem #include <sys/cdefs.h>
     39  1.23  thorpej __KERNEL_RCSID(0, "$NetBSD: if_uba.c,v 1.23 2003/04/01 02:06:06 thorpej Exp $");
     40  1.10    ragge 
     41   1.8  mycroft #include <sys/param.h>
     42   1.8  mycroft #include <sys/systm.h>
     43   1.8  mycroft #include <sys/malloc.h>
     44   1.8  mycroft #include <sys/mbuf.h>
     45   1.8  mycroft #include <sys/socket.h>
     46  1.19    ragge #include <sys/device.h>
     47   1.1    ragge 
     48  1.23  thorpej #include <uvm/uvm_extern.h>
     49  1.23  thorpej 
     50   1.8  mycroft #include <net/if.h>
     51   1.1    ragge 
     52  1.19    ragge #include <machine/bus.h>
     53  1.19    ragge 
     54  1.19    ragge #include <dev/qbus/if_uba.h>
     55  1.19    ragge #include <dev/qbus/ubareg.h>
     56  1.19    ragge #include <dev/qbus/ubavar.h>
     57  1.19    ragge 
     58  1.19    ragge static	struct mbuf *getmcl(void);
     59   1.1    ragge 
     60   1.1    ragge /*
     61   1.1    ragge  * Routines supporting UNIBUS network interfaces.
     62   1.1    ragge  *
     63   1.1    ragge  * TODO:
     64   1.1    ragge  *	Support interfaces using only one BDP statically.
     65   1.1    ragge  */
     66   1.1    ragge 
     67   1.1    ragge /*
     68  1.12    ragge  * Init UNIBUS for interface whose headers of size hlen are to
     69   1.1    ragge  * end on a page boundary.  We allocate a UNIBUS map register for the page
     70   1.1    ragge  * with the header, and nmr more UNIBUS map registers for i/o on the adapter,
     71   1.1    ragge  * doing this once for each read and once for each write buffer.  We also
     72   1.1    ragge  * allocate page frames in the mbuffer pool for these pages.
     73  1.19    ragge  *
     74  1.19    ragge  * Recent changes:
     75  1.19    ragge  *	No special "header pages" anymore.
     76  1.19    ragge  *	Recv packets are always put in clusters.
     77  1.19    ragge  *	"size" is the maximum buffer size, may not be bigger than MCLBYTES.
     78   1.1    ragge  */
     79  1.10    ragge int
     80  1.19    ragge if_ubaminit(struct ifubinfo *ifu, struct uba_softc *uh, int size,
     81  1.19    ragge     struct ifrw *ifr, int nr, struct ifxmt *ifw, int nw)
     82   1.1    ragge {
     83  1.19    ragge 	struct mbuf *m;
     84  1.19    ragge 	int totsz, i, error, rseg, nm = nr;
     85  1.19    ragge 	bus_dma_segment_t seg;
     86  1.19    ragge 	caddr_t vaddr;
     87  1.19    ragge 
     88  1.19    ragge #ifdef DIAGNOSTIC
     89  1.19    ragge 	if (size > MCLBYTES)
     90  1.19    ragge 		panic("if_ubaminit: size > MCLBYTES");
     91  1.19    ragge #endif
     92  1.19    ragge 	ifu->iff_softc = uh;
     93  1.19    ragge 	/*
     94  1.19    ragge 	 * Get DMA memory for transmit buffers.
     95  1.19    ragge 	 * Buffer size are rounded up to a multiple of the uba page size,
     96  1.19    ragge 	 * then allocated contiguous.
     97  1.19    ragge 	 */
     98  1.19    ragge 	size = (size + UBA_PGOFSET) & ~UBA_PGOFSET;
     99  1.19    ragge 	totsz = size * nw;
    100  1.23  thorpej 	if ((error = bus_dmamem_alloc(uh->uh_dmat, totsz, PAGE_SIZE, 0,
    101  1.19    ragge 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)))
    102  1.19    ragge 		return error;
    103  1.19    ragge 	if ((error = bus_dmamem_map(uh->uh_dmat, &seg, rseg, totsz, &vaddr,
    104  1.19    ragge 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT))) {
    105  1.19    ragge 		bus_dmamem_free(uh->uh_dmat, &seg, rseg);
    106  1.19    ragge 		return error;
    107   1.1    ragge 	}
    108  1.19    ragge 
    109  1.19    ragge 	/*
    110  1.19    ragge 	 * Create receive and transmit maps.
    111  1.19    ragge 	 * Alloc all resources now so we won't fail in the future.
    112  1.19    ragge 	 */
    113  1.19    ragge 
    114  1.19    ragge 	for (i = 0; i < nr; i++) {
    115  1.19    ragge 		if ((error = bus_dmamap_create(uh->uh_dmat, size, 1,
    116  1.19    ragge 		    size, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    117  1.19    ragge 		    &ifr[i].ifrw_map))) {
    118   1.1    ragge 			nr = i;
    119  1.19    ragge 			nm = nw = 0;
    120   1.1    ragge 			goto bad;
    121   1.1    ragge 		}
    122  1.19    ragge 	}
    123  1.19    ragge 	for (i = 0; i < nw; i++) {
    124  1.19    ragge 		if ((error = bus_dmamap_create(uh->uh_dmat, size, 1,
    125  1.19    ragge 		    size, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    126  1.19    ragge 		    &ifw[i].ifw_map))) {
    127   1.1    ragge 			nw = i;
    128  1.19    ragge 			nm = 0;
    129   1.1    ragge 			goto bad;
    130   1.1    ragge 		}
    131   1.1    ragge 	}
    132  1.19    ragge 	/*
    133  1.19    ragge 	 * Preload the rx maps with mbuf clusters.
    134  1.19    ragge 	 */
    135  1.19    ragge 	for (i = 0; i < nm; i++) {
    136  1.19    ragge 		if ((m = getmcl()) == NULL) {
    137  1.19    ragge 			nm = i;
    138  1.19    ragge 			goto bad;
    139  1.19    ragge 		}
    140  1.19    ragge 		ifr[i].ifrw_mbuf = m;
    141  1.19    ragge 		bus_dmamap_load(uh->uh_dmat, ifr[i].ifrw_map,
    142  1.19    ragge 		    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
    143  1.19    ragge 
    144  1.19    ragge 	}
    145  1.19    ragge 	/*
    146  1.19    ragge 	 * Load the tx maps with DMA memory (common case).
    147  1.19    ragge 	 */
    148  1.19    ragge 	for (i = 0; i < nw; i++) {
    149  1.19    ragge 		ifw[i].ifw_vaddr = vaddr + size * i;
    150  1.19    ragge 		ifw[i].ifw_size = size;
    151  1.19    ragge 		bus_dmamap_load(uh->uh_dmat, ifw[i].ifw_map,
    152  1.19    ragge 		    ifw[i].ifw_vaddr, ifw[i].ifw_size, NULL, BUS_DMA_NOWAIT);
    153  1.19    ragge 	}
    154  1.19    ragge 	return 0;
    155   1.1    ragge bad:
    156  1.19    ragge 	while (--nm >= 0) {
    157  1.19    ragge 		bus_dmamap_unload(uh->uh_dmat, ifr[nw].ifrw_map);
    158  1.19    ragge 		m_freem(ifr[nm].ifrw_mbuf);
    159  1.19    ragge 	}
    160   1.1    ragge 	while (--nw >= 0)
    161  1.19    ragge 		bus_dmamap_destroy(uh->uh_dmat, ifw[nw].ifw_map);
    162   1.1    ragge 	while (--nr >= 0)
    163  1.19    ragge 		bus_dmamap_destroy(uh->uh_dmat, ifr[nw].ifrw_map);
    164   1.1    ragge 	return (0);
    165   1.1    ragge }
    166   1.1    ragge 
    167  1.19    ragge struct mbuf *
    168  1.19    ragge getmcl()
    169   1.1    ragge {
    170  1.19    ragge 	struct mbuf *m;
    171   1.1    ragge 
    172  1.19    ragge 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    173  1.19    ragge 	if (m == NULL)
    174  1.19    ragge 		return 0;
    175  1.19    ragge 	MCLGET(m, M_DONTWAIT);
    176  1.19    ragge 	if ((m->m_flags & M_EXT) == 0) {
    177  1.19    ragge 		m_freem(m);
    178  1.19    ragge 		return 0;
    179  1.19    ragge 	}
    180  1.19    ragge 	return m;
    181   1.1    ragge }
    182   1.1    ragge 
    183   1.1    ragge /*
    184   1.1    ragge  * Pull read data off a interface.
    185   1.1    ragge  * Totlen is length of data, with local net header stripped.
    186   1.6  mycroft  * When full cluster sized units are present
    187   1.1    ragge  * on the interface on cluster boundaries we can get them more
    188   1.1    ragge  * easily by remapping, and take advantage of this here.
    189   1.1    ragge  * Save a pointer to the interface structure and the total length,
    190   1.1    ragge  * so that protocols can determine where incoming packets arrived.
    191   1.1    ragge  * Note: we may be called to receive from a transmit buffer by some
    192   1.1    ragge  * devices.  In that case, we must force normal mapping of the buffer,
    193   1.1    ragge  * so that the correct data will appear (only unibus maps are
    194   1.1    ragge  * changed when remapping the transmit buffers).
    195   1.1    ragge  */
    196   1.1    ragge struct mbuf *
    197  1.19    ragge if_ubaget(struct ifubinfo *ifu, struct ifrw *ifr, struct ifnet *ifp, int len)
    198   1.1    ragge {
    199  1.19    ragge 	struct uba_softc *uh = ifu->iff_softc;
    200  1.19    ragge 	struct mbuf *m, *mn;
    201   1.1    ragge 
    202  1.19    ragge 	if ((mn = getmcl()) == NULL)
    203  1.19    ragge 		return NULL;	/* Leave the old */
    204   1.1    ragge 
    205  1.19    ragge 	bus_dmamap_unload(uh->uh_dmat, ifr->ifrw_map);
    206  1.19    ragge 	m = ifr->ifrw_mbuf;
    207  1.19    ragge 	ifr->ifrw_mbuf = mn;
    208  1.19    ragge 	if ((bus_dmamap_load(uh->uh_dmat, ifr->ifrw_map,
    209  1.19    ragge 	    mn->m_ext.ext_buf, mn->m_ext.ext_size, NULL, BUS_DMA_NOWAIT)))
    210  1.19    ragge 		panic("if_ubaget"); /* Cannot happen */
    211  1.19    ragge 	m->m_pkthdr.rcvif = ifp;
    212  1.19    ragge 	m->m_len = m->m_pkthdr.len = len;
    213  1.19    ragge 	return m;
    214   1.1    ragge }
    215   1.1    ragge 
    216   1.1    ragge /*
    217  1.19    ragge  * Called after a packet is sent. Releases hold resources.
    218   1.1    ragge  */
    219  1.19    ragge void
    220  1.19    ragge if_ubaend(struct ifubinfo *ifu, struct ifxmt *ifw)
    221   1.1    ragge {
    222  1.19    ragge 	struct uba_softc *uh = ifu->iff_softc;
    223   1.1    ragge 
    224  1.19    ragge 	if (ifw->ifw_flags & IFRW_MBUF) {
    225  1.19    ragge 		bus_dmamap_unload(uh->uh_dmat, ifw->ifw_map);
    226  1.19    ragge 		m_freem(ifw->ifw_mbuf);
    227  1.19    ragge 		ifw->ifw_mbuf = NULL;
    228  1.19    ragge 	}
    229   1.1    ragge }
    230   1.1    ragge 
    231   1.1    ragge /*
    232   1.1    ragge  * Map a chain of mbufs onto a network interface
    233   1.1    ragge  * in preparation for an i/o operation.
    234   1.1    ragge  * The argument chain of mbufs includes the local network
    235   1.1    ragge  * header which is copied to be in the mapped, aligned
    236   1.1    ragge  * i/o space.
    237   1.1    ragge  */
    238  1.10    ragge int
    239  1.19    ragge if_ubaput(struct ifubinfo *ifu, struct ifxmt *ifw, struct mbuf *m)
    240   1.1    ragge {
    241  1.19    ragge 	struct uba_softc *uh = ifu->iff_softc;
    242  1.19    ragge 	int len;
    243   1.1    ragge 
    244  1.19    ragge 	if (/* m->m_next ==*/ 0) {
    245  1.19    ragge 		/*
    246  1.19    ragge 		 * Map the outgoing packet directly.
    247  1.19    ragge 		 */
    248  1.19    ragge 		if ((ifw->ifw_flags & IFRW_MBUF) == 0) {
    249  1.19    ragge 			bus_dmamap_unload(uh->uh_dmat, ifw->ifw_map);
    250  1.19    ragge 			ifw->ifw_flags |= IFRW_MBUF;
    251  1.19    ragge 		}
    252  1.19    ragge 		bus_dmamap_load(uh->uh_dmat, ifw->ifw_map, mtod(m, void *),
    253  1.19    ragge 		    m->m_len, NULL, BUS_DMA_NOWAIT);
    254  1.19    ragge 		ifw->ifw_mbuf = m;
    255  1.19    ragge 		len = m->m_len;
    256  1.19    ragge 	} else {
    257  1.19    ragge 		if (ifw->ifw_flags & IFRW_MBUF) {
    258  1.19    ragge 			bus_dmamap_load(uh->uh_dmat, ifw->ifw_map,
    259  1.19    ragge 	                    ifw->ifw_vaddr, ifw->ifw_size,NULL,BUS_DMA_NOWAIT);
    260  1.19    ragge 			ifw->ifw_flags &= ~IFRW_MBUF;
    261  1.19    ragge 		}
    262  1.19    ragge 		len = m->m_pkthdr.len;
    263  1.19    ragge 		m_copydata(m, 0, m->m_pkthdr.len, ifw->ifw_vaddr);
    264  1.19    ragge 		m_freem(m);
    265   1.1    ragge 	}
    266  1.19    ragge 	return len;
    267   1.1    ragge }
    268