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