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