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