if_le_ebus.c revision 1.1 1 1.1 pooka /* $NetBSD: if_le_ebus.c,v 1.1 2011/01/26 01:18:50 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code was written by Alessandro Forin and Neil Pittman
8 1.1 pooka * at Microsoft Research and contributed to The NetBSD Foundation
9 1.1 pooka * by Microsoft Corporation.
10 1.1 pooka *
11 1.1 pooka * Redistribution and use in source and binary forms, with or without
12 1.1 pooka * modification, are permitted provided that the following conditions
13 1.1 pooka * are met:
14 1.1 pooka * 1. Redistributions of source code must retain the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer.
16 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pooka * notice, this list of conditions and the following disclaimer in the
18 1.1 pooka * documentation and/or other materials provided with the distribution.
19 1.1 pooka *
20 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
31 1.1 pooka */
32 1.1 pooka
33 1.1 pooka #include <sys/cdefs.h>
34 1.1 pooka __KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.1 2011/01/26 01:18:50 pooka Exp $");
35 1.1 pooka
36 1.1 pooka #include "opt_inet.h"
37 1.1 pooka
38 1.1 pooka #include "rnd.h"
39 1.1 pooka
40 1.1 pooka #include <sys/param.h>
41 1.1 pooka #include <sys/systm.h>
42 1.1 pooka #include <sys/mbuf.h>
43 1.1 pooka #include <sys/syslog.h>
44 1.1 pooka #include <sys/socket.h>
45 1.1 pooka #include <sys/device.h>
46 1.1 pooka #include <sys/malloc.h>
47 1.1 pooka #include <sys/ioctl.h>
48 1.1 pooka #include <sys/errno.h>
49 1.1 pooka
50 1.1 pooka #include <net/if.h>
51 1.1 pooka #include <net/if_dl.h>
52 1.1 pooka #include <net/if_ether.h>
53 1.1 pooka #include <net/if_media.h>
54 1.1 pooka
55 1.1 pooka #ifdef INET
56 1.1 pooka #include <netinet/in.h>
57 1.1 pooka #include <netinet/if_inarp.h>
58 1.1 pooka #endif
59 1.1 pooka
60 1.1 pooka #include <net/bpf.h>
61 1.1 pooka #include <net/bpfdesc.h>
62 1.1 pooka
63 1.1 pooka #if NRND > 0
64 1.1 pooka #include <sys/rnd.h>
65 1.1 pooka #endif
66 1.1 pooka
67 1.1 pooka #include <emips/ebus/ebusvar.h>
68 1.1 pooka #include <emips/emips/machdep.h>
69 1.1 pooka #include <machine/emipsreg.h>
70 1.1 pooka
71 1.1 pooka extern paddr_t kvtophys(vaddr_t);
72 1.1 pooka
73 1.1 pooka struct bufmap {
74 1.1 pooka struct mbuf *mbuf;
75 1.1 pooka paddr_t phys;
76 1.1 pooka };
77 1.1 pooka
78 1.1 pooka struct enic_softc {
79 1.1 pooka device_t sc_dev; /* base device glue */
80 1.1 pooka struct ethercom sc_ethercom; /* Ethernet common part */
81 1.1 pooka struct ifmedia sc_media; /* our supported media */
82 1.1 pooka
83 1.1 pooka struct _Enic *sc_regs; /* hw registers */
84 1.1 pooka
85 1.1 pooka int sc_havecarrier; /* carrier status */
86 1.1 pooka void *sc_sh; /* shutdownhook cookie */
87 1.1 pooka int inited;
88 1.1 pooka
89 1.1 pooka int sc_no_rd;
90 1.1 pooka int sc_n_recv;
91 1.1 pooka int sc_recv_h;
92 1.1 pooka /* BUGBUG really should be malloc-ed */
93 1.1 pooka #define SC_MAX_N_RECV 64
94 1.1 pooka struct bufmap sc_recv[SC_MAX_N_RECV];
95 1.1 pooka
96 1.1 pooka int sc_no_td;
97 1.1 pooka int sc_n_xmit;
98 1.1 pooka int sc_xmit_h;
99 1.1 pooka /* BUGBUG really should be malloc-ed */
100 1.1 pooka #define SC_MAX_N_XMIT 16
101 1.1 pooka struct bufmap sc_xmit[SC_MAX_N_XMIT];
102 1.1 pooka
103 1.1 pooka #if DEBUG
104 1.1 pooka int xhit;
105 1.1 pooka int xmiss;
106 1.1 pooka int tfull;
107 1.1 pooka int tfull2;
108 1.1 pooka int brh;
109 1.1 pooka int rf;
110 1.1 pooka int bxh;
111 1.1 pooka
112 1.1 pooka int it;
113 1.1 pooka #endif
114 1.1 pooka
115 1.1 pooka u_int8_t sc_enaddr[ETHER_ADDR_LEN];
116 1.1 pooka u_int8_t sc_pad[2];
117 1.1 pooka #if NRND > 0
118 1.1 pooka rndsource_element_t rnd_source;
119 1.1 pooka #endif
120 1.1 pooka };
121 1.1 pooka
122 1.1 pooka void enic_reset(struct ifnet *);
123 1.1 pooka int enic_init(struct ifnet *);
124 1.1 pooka void enic_stop(struct ifnet *ifp, int suspend);
125 1.1 pooka void enic_start(struct ifnet *ifp);
126 1.1 pooka void enic_shutdown(void *);
127 1.1 pooka void enic_watchdog(struct ifnet *ifp);
128 1.1 pooka int enic_mediachange(struct ifnet *ifp);
129 1.1 pooka void enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr);
130 1.1 pooka int enic_ioctl(struct ifnet *ifp, u_long cmd, void *data);
131 1.1 pooka int enic_intr(void *cookie, void *f);
132 1.1 pooka void enic_rint(struct enic_softc *, uint32_t, paddr_t);
133 1.1 pooka void enic_tint(struct enic_softc *, uint32_t, paddr_t);
134 1.1 pooka void enic_kill_xmit(struct enic_softc *sc);
135 1.1 pooka void enic_post_recv(struct enic_softc *sc, struct mbuf *m);
136 1.1 pooka void enic_refill(struct enic_softc *sc);
137 1.1 pooka static int enic_gethwinfo(struct enic_softc *sc);
138 1.1 pooka int enic_put(struct enic_softc *sc, struct mbuf **pm);
139 1.1 pooka
140 1.1 pooka static int enic_match(struct device *, struct cfdata *, void *);
141 1.1 pooka static void enic_attach(struct device *, struct device *, void *);
142 1.1 pooka
143 1.1 pooka CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc),
144 1.1 pooka enic_match, enic_attach, NULL, NULL);
145 1.1 pooka
146 1.1 pooka int
147 1.1 pooka enic_match(struct device *parent, struct cfdata *match, void *aux)
148 1.1 pooka {
149 1.1 pooka struct ebus_attach_args *d = aux;
150 1.1 pooka /* donno yet */
151 1.1 pooka struct _Enic *et = (struct _Enic *)d->ia_vaddr;
152 1.1 pooka
153 1.1 pooka if (strcmp("enic", d->ia_name) != 0)
154 1.1 pooka return (0);
155 1.1 pooka if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET))
156 1.1 pooka return 0;
157 1.1 pooka return (1);
158 1.1 pooka }
159 1.1 pooka
160 1.1 pooka void
161 1.1 pooka enic_attach(struct device *parent, struct device *self, void *aux)
162 1.1 pooka {
163 1.1 pooka struct enic_softc *sc = device_private(self);
164 1.1 pooka struct ebus_attach_args *ia = aux;
165 1.1 pooka struct ifnet *ifp = &sc->sc_ethercom.ec_if;
166 1.1 pooka
167 1.1 pooka sc->sc_regs = (struct _Enic *)(ia->ia_vaddr);
168 1.1 pooka #if DEBUG
169 1.1 pooka printf(" virt=%p ", (void*)sc->sc_regs);
170 1.1 pooka #endif
171 1.1 pooka
172 1.1 pooka /* Get the MAC and the depth of the FIFOs */
173 1.1 pooka if (!enic_gethwinfo(sc)) {
174 1.1 pooka printf(" <cannot get hw info> DISABLED.\n");
175 1.1 pooka /*
176 1.1 pooka * NB: caveat maintainer: make sure what we
177 1.1 pooka * did NOT do below does not hurt the system
178 1.1 pooka */
179 1.1 pooka return;
180 1.1 pooka }
181 1.1 pooka
182 1.1 pooka sc->sc_dev = self;
183 1.1 pooka sc->sc_no_td = 0;
184 1.1 pooka sc->sc_havecarrier = 1; /* BUGBUG */
185 1.1 pooka sc->sc_recv_h = 0;
186 1.1 pooka sc->sc_xmit_h = 0;
187 1.1 pooka /* uhmm do I need to do this? */
188 1.1 pooka memset(sc->sc_recv,0, sizeof sc->sc_recv );
189 1.1 pooka memset(sc->sc_xmit,0, sizeof sc->sc_xmit );
190 1.1 pooka
191 1.1 pooka /* Initialize ifnet structure. */
192 1.1 pooka strcpy(ifp->if_xname, device_xname(sc->sc_dev));
193 1.1 pooka ifp->if_softc = sc;
194 1.1 pooka ifp->if_start = enic_start;
195 1.1 pooka ifp->if_ioctl = enic_ioctl;
196 1.1 pooka ifp->if_watchdog = enic_watchdog;
197 1.1 pooka ifp->if_init = enic_init;
198 1.1 pooka ifp->if_stop = enic_stop;
199 1.1 pooka ifp->if_flags =
200 1.1 pooka IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
201 1.1 pooka IFQ_SET_READY(&ifp->if_snd);
202 1.1 pooka
203 1.1 pooka /* Initialize ifmedia structures. */
204 1.1 pooka ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
205 1.1 pooka ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
206 1.1 pooka ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
207 1.1 pooka
208 1.1 pooka /* Make sure the chip is stopped. */
209 1.1 pooka enic_stop(ifp, 0);
210 1.1 pooka sc->inited = 0;
211 1.1 pooka
212 1.1 pooka /* Get the mac address and print it */
213 1.1 pooka printf(": eNIC [%d %d], address %s\n",
214 1.1 pooka sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));
215 1.1 pooka
216 1.1 pooka /* claim 802.1q capability */
217 1.1 pooka // sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
218 1.1 pooka
219 1.1 pooka /* Attach the interface. */
220 1.1 pooka if_attach(ifp);
221 1.1 pooka ether_ifattach(ifp, sc->sc_enaddr);
222 1.1 pooka
223 1.1 pooka sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
224 1.1 pooka if (sc->sc_sh == NULL)
225 1.1 pooka panic("enic_attach: cannot establish shutdown hook");
226 1.1 pooka
227 1.1 pooka #if NRND > 0
228 1.1 pooka rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
229 1.1 pooka RND_TYPE_NET, 0);
230 1.1 pooka #endif
231 1.1 pooka
232 1.1 pooka ebus_intr_establish(parent, (void*)ia->ia_cookie, IPL_NET,
233 1.1 pooka enic_intr, sc);
234 1.1 pooka }
235 1.1 pooka
236 1.1 pooka /* Beware: does not work while the nic is running
237 1.1 pooka */
238 1.1 pooka static int enic_gethwinfo(struct enic_softc *sc)
239 1.1 pooka {
240 1.1 pooka uint8_t buffer[8];/* 64bits max */
241 1.1 pooka PENIC_INFO hw = (PENIC_INFO)buffer;
242 1.1 pooka paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
243 1.1 pooka int i;
244 1.1 pooka
245 1.1 pooka /* First thing first, get the MAC address
246 1.1 pooka */
247 1.1 pooka memset(buffer,0,sizeof buffer);
248 1.1 pooka buffer[0] = ENIC_CMD_GET_ADDRESS;
249 1.1 pooka buffer[3] = ENIC_CMD_GET_ADDRESS;/* bswap bug */
250 1.1 pooka sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
251 1.1 pooka sc->sc_regs->BufferAddressHi32 = 0;
252 1.1 pooka sc->sc_regs->BufferAddressLo32 = phys; /* go! */
253 1.1 pooka
254 1.1 pooka for (i = 0; i < 100; i++) {
255 1.1 pooka DELAY(100);
256 1.1 pooka if (0 == (sc->sc_regs->Control & EC_OF_EMPTY))
257 1.1 pooka break;
258 1.1 pooka }
259 1.1 pooka if (i == 100)
260 1.1 pooka return 0;
261 1.1 pooka
262 1.1 pooka phys2 = sc->sc_regs->BufferAddressLo32;
263 1.1 pooka if (phys2 != phys) {
264 1.1 pooka printf("enic uhu? %llx != %llx?\n",(long long)phys,(long long)phys2);
265 1.1 pooka return 0;
266 1.1 pooka }
267 1.1 pooka memcpy(sc->sc_enaddr,buffer,ETHER_ADDR_LEN);
268 1.1 pooka
269 1.1 pooka /* Next get the HW parameters
270 1.1 pooka */
271 1.1 pooka memset(buffer,0,sizeof buffer);
272 1.1 pooka buffer[0] = ENIC_CMD_GET_INFO;
273 1.1 pooka buffer[3] = ENIC_CMD_GET_INFO;/* bswap bug */
274 1.1 pooka sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
275 1.1 pooka sc->sc_regs->BufferAddressHi32 = 0;
276 1.1 pooka sc->sc_regs->BufferAddressLo32 = phys; /* go! */
277 1.1 pooka
278 1.1 pooka for (i = 0; i < 100; i++) {
279 1.1 pooka DELAY(100);
280 1.1 pooka if (0 == (sc->sc_regs->Control & EC_OF_EMPTY))
281 1.1 pooka break;
282 1.1 pooka }
283 1.1 pooka if (i == 100)
284 1.1 pooka return 0;
285 1.1 pooka
286 1.1 pooka phys2 = sc->sc_regs->BufferAddressLo32;
287 1.1 pooka if (phys2 != phys) {
288 1.1 pooka printf("enic uhu2? %llx != %llx?\n",(long long)phys,(long long)phys2);
289 1.1 pooka return 0;
290 1.1 pooka }
291 1.1 pooka //printf("enic: hwinfo: %x %x %x %x %x %x \n", hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
292 1.1 pooka // hw->ErrorCount, hw->FramesDropped, hw->Reserved);
293 1.1 pooka
294 1.1 pooka /* Get FIFO depths and cap them
295 1.1 pooka */
296 1.1 pooka sc->sc_n_recv = hw->InputFifoSize;
297 1.1 pooka if (sc->sc_n_recv > SC_MAX_N_RECV)
298 1.1 pooka sc->sc_n_recv = SC_MAX_N_RECV;
299 1.1 pooka if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */
300 1.1 pooka sc->sc_n_recv = 8;
301 1.1 pooka sc->sc_n_xmit = 4;
302 1.1 pooka } else {
303 1.1 pooka sc->sc_n_xmit = hw->OutputFifoSize;
304 1.1 pooka if (sc->sc_n_xmit > SC_MAX_N_XMIT)
305 1.1 pooka sc->sc_n_xmit = SC_MAX_N_XMIT;
306 1.1 pooka }
307 1.1 pooka
308 1.1 pooka return 1;
309 1.1 pooka }
310 1.1 pooka
311 1.1 pooka void
312 1.1 pooka enic_reset(struct ifnet *ifp)
313 1.1 pooka {
314 1.1 pooka int s;
315 1.1 pooka
316 1.1 pooka s = splnet();
317 1.1 pooka enic_stop(ifp,0);
318 1.1 pooka enic_init(ifp);
319 1.1 pooka splx(s);
320 1.1 pooka }
321 1.1 pooka
322 1.1 pooka void
323 1.1 pooka enic_stop(struct ifnet *ifp, int suspend)
324 1.1 pooka {
325 1.1 pooka struct enic_softc *sc = ifp->if_softc;
326 1.1 pooka
327 1.1 pooka //printf("enic_stop %x\n", sc->sc_regs->Control);
328 1.1 pooka
329 1.1 pooka /* NB: only "ifconfig down" says suspend=1 (then "up" calls init)
330 1.1 pooka * Could simply set RXDIS in this case
331 1.1 pooka */
332 1.1 pooka sc->inited = 2;
333 1.1 pooka sc->sc_regs->Control = EC_RESET;
334 1.1 pooka sc->sc_no_rd = 0; /* they are gone */
335 1.1 pooka sc->sc_no_td = 0; /* they are gone */
336 1.1 pooka }
337 1.1 pooka
338 1.1 pooka void
339 1.1 pooka enic_shutdown(void *arg)
340 1.1 pooka {
341 1.1 pooka struct ifnet *ifp = arg;
342 1.1 pooka
343 1.1 pooka enic_stop(ifp, 0);
344 1.1 pooka }
345 1.1 pooka
346 1.1 pooka void
347 1.1 pooka enic_kill_xmit(struct enic_softc *sc)
348 1.1 pooka {
349 1.1 pooka int i;
350 1.1 pooka struct mbuf *m;
351 1.1 pooka
352 1.1 pooka for (i = 0; i < sc->sc_n_xmit; i++)
353 1.1 pooka if ((m = sc->sc_xmit[i].mbuf) != NULL) {
354 1.1 pooka sc->sc_xmit[i].mbuf = NULL;
355 1.1 pooka sc->sc_xmit[i].phys = ~0;
356 1.1 pooka m_freem(m);
357 1.1 pooka }
358 1.1 pooka sc->sc_no_td = 0;
359 1.1 pooka sc->sc_xmit_h = 0;
360 1.1 pooka }
361 1.1 pooka
362 1.1 pooka void
363 1.1 pooka enic_post_recv(struct enic_softc *sc, struct mbuf *m)
364 1.1 pooka {
365 1.1 pooka int i, waitmode = M_DONTWAIT;
366 1.1 pooka paddr_t phys;
367 1.1 pooka
368 1.1 pooka #define rpostone(_p_) \
369 1.1 pooka sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
370 1.1 pooka sc->sc_regs->BufferAddressHi32 = 0; \
371 1.1 pooka sc->sc_regs->BufferAddressLo32 = _p_;
372 1.1 pooka #define tpostone(_p_,_s_) \
373 1.1 pooka sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
374 1.1 pooka sc->sc_regs->BufferAddressHi32 = 0; \
375 1.1 pooka sc->sc_regs->BufferAddressLo32 = _p_;
376 1.1 pooka
377 1.1 pooka /* Operational reload? */
378 1.1 pooka if (m != NULL) {
379 1.1 pooka /* But is the hw ready for it */
380 1.1 pooka if (sc->sc_regs->Control & EC_IF_FULL)
381 1.1 pooka goto no_room;
382 1.1 pooka /* Yes, find a spot. Include empty q case. */
383 1.1 pooka for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
384 1.1 pooka if (sc->sc_recv[i].mbuf == NULL)
385 1.1 pooka goto found;
386 1.1 pooka for (i = 0; i < sc->sc_recv_h; i++)
387 1.1 pooka if (sc->sc_recv[i].mbuf == NULL)
388 1.1 pooka goto found;
389 1.1 pooka /* no spot, drop it (sigh) */
390 1.1 pooka no_room:
391 1.1 pooka #if DEBUG
392 1.1 pooka sc->rf++;
393 1.1 pooka #endif
394 1.1 pooka m_freem(m);
395 1.1 pooka return;
396 1.1 pooka found:
397 1.1 pooka phys = kvtophys((vaddr_t)m->m_data);
398 1.1 pooka sc->sc_recv[i].mbuf = m;
399 1.1 pooka sc->sc_recv[i].phys = phys;
400 1.1 pooka rpostone(phys);
401 1.1 pooka sc->sc_no_rd++;
402 1.1 pooka return;
403 1.1 pooka }
404 1.1 pooka
405 1.1 pooka /* Repost after reset? */
406 1.1 pooka if (sc->inited) {
407 1.1 pooka /* order doesnt matter, might as well keep it clean */
408 1.1 pooka int j = 0;
409 1.1 pooka sc->sc_recv_h = 0;
410 1.1 pooka for (i = 0; i < sc->sc_n_recv; i++)
411 1.1 pooka if ((m = sc->sc_recv[i].mbuf) != NULL) {
412 1.1 pooka phys = sc->sc_recv[i].phys;
413 1.1 pooka sc->sc_recv[i].mbuf = NULL;
414 1.1 pooka sc->sc_recv[i].phys = ~0;
415 1.1 pooka sc->sc_recv[j].mbuf = m;
416 1.1 pooka sc->sc_recv[j].phys = phys;
417 1.1 pooka #if DEBUG
418 1.1 pooka if (sc->sc_regs->Control & EC_IF_FULL)
419 1.1 pooka printf("?uhu? postrecv full? %d\n", sc->sc_no_rd);
420 1.1 pooka #endif
421 1.1 pooka sc->sc_no_rd++;
422 1.1 pooka rpostone(phys);
423 1.1 pooka j++;
424 1.1 pooka }
425 1.1 pooka /* Any holes left? */
426 1.1 pooka sc->inited = 1;
427 1.1 pooka if (j >= sc->sc_n_recv)
428 1.1 pooka return;/* no, we are done */
429 1.1 pooka /* continue on with the loop below */
430 1.1 pooka i = j; m = NULL;
431 1.1 pooka goto fillem;
432 1.1 pooka }
433 1.1 pooka
434 1.1 pooka /* Initial refill, we can wait */
435 1.1 pooka waitmode = M_WAIT;
436 1.1 pooka sc->sc_recv_h = 0;
437 1.1 pooka memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
438 1.1 pooka i = 0;
439 1.1 pooka fillem:
440 1.1 pooka for (; i < sc->sc_n_recv; i++) {
441 1.1 pooka MGETHDR(m, waitmode, MT_DATA);
442 1.1 pooka if (m == 0)
443 1.1 pooka break;
444 1.1 pooka m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
445 1.1 pooka m->m_pkthdr.len = 0;
446 1.1 pooka
447 1.1 pooka MCLGET(m, waitmode);
448 1.1 pooka if ((m->m_flags & M_EXT) == 0)
449 1.1 pooka break;
450 1.1 pooka
451 1.1 pooka /* This offset aligns IP/TCP headers and helps performance
452 1.1 pooka */
453 1.1 pooka #if 1
454 1.1 pooka #define ADJUST_MBUF_OFFSET(_m_) { \
455 1.1 pooka (_m_)->m_data += 2; \
456 1.1 pooka (_m_)->m_len -= 2; \
457 1.1 pooka }
458 1.1 pooka #else
459 1.1 pooka #define ADJUST_MBUF_OFFSET(_m_)
460 1.1 pooka #endif
461 1.1 pooka
462 1.1 pooka m->m_len = MCLBYTES;
463 1.1 pooka
464 1.1 pooka ADJUST_MBUF_OFFSET(m);
465 1.1 pooka phys = kvtophys((vaddr_t)m->m_data);
466 1.1 pooka sc->sc_recv[i].mbuf = m;
467 1.1 pooka sc->sc_recv[i].phys = phys;
468 1.1 pooka #if DEBUG
469 1.1 pooka if (sc->sc_regs->Control & EC_IF_FULL)
470 1.1 pooka printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
471 1.1 pooka #endif
472 1.1 pooka sc->sc_no_rd++;
473 1.1 pooka rpostone(phys);
474 1.1 pooka m = NULL;
475 1.1 pooka }
476 1.1 pooka
477 1.1 pooka if (m) m_freem(m);
478 1.1 pooka sc->inited = 1;
479 1.1 pooka }
480 1.1 pooka
481 1.1 pooka void enic_refill(struct enic_softc *sc)
482 1.1 pooka {
483 1.1 pooka struct mbuf *m;
484 1.1 pooka int waitmode = M_DONTWAIT;
485 1.1 pooka
486 1.1 pooka MGETHDR(m, waitmode, MT_DATA);
487 1.1 pooka if (m == 0)
488 1.1 pooka return;
489 1.1 pooka m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
490 1.1 pooka m->m_pkthdr.len = 0;
491 1.1 pooka
492 1.1 pooka MCLGET(m, waitmode);
493 1.1 pooka if ((m->m_flags & M_EXT) == 0) {
494 1.1 pooka m_freem(m);
495 1.1 pooka return;
496 1.1 pooka }
497 1.1 pooka
498 1.1 pooka m->m_len = MCLBYTES;
499 1.1 pooka ADJUST_MBUF_OFFSET(m);
500 1.1 pooka
501 1.1 pooka enic_post_recv(sc,m);
502 1.1 pooka }
503 1.1 pooka
504 1.1 pooka int
505 1.1 pooka enic_init(struct ifnet *ifp)
506 1.1 pooka {
507 1.1 pooka struct enic_softc *sc = ifp->if_softc;
508 1.1 pooka uint32_t ctl;
509 1.1 pooka
510 1.1 pooka /* no need to init many times unless we are in reset */
511 1.1 pooka if (sc->inited != 1) {
512 1.1 pooka
513 1.1 pooka /* Cancel all xmit buffers */
514 1.1 pooka enic_kill_xmit(sc);
515 1.1 pooka
516 1.1 pooka /* Re-post all recv buffers */
517 1.1 pooka enic_post_recv(sc,NULL);
518 1.1 pooka }
519 1.1 pooka
520 1.1 pooka /* Start the eNIC */
521 1.1 pooka ifp->if_flags |= IFF_RUNNING;
522 1.1 pooka ifp->if_flags &= ~IFF_OACTIVE;
523 1.1 pooka ifp->if_timer = 0;
524 1.1 pooka ctl = sc->sc_regs->Control | EC_INTEN;
525 1.1 pooka ctl &= ~EC_RXDIS;
526 1.1 pooka sc->sc_regs->Control = ctl;
527 1.1 pooka //printf("enic_init <- %x\n",ctl);
528 1.1 pooka
529 1.1 pooka enic_start(ifp);
530 1.1 pooka
531 1.1 pooka return (0);
532 1.1 pooka }
533 1.1 pooka
534 1.1 pooka
535 1.1 pooka void
536 1.1 pooka enic_watchdog(struct ifnet *ifp)
537 1.1 pooka {
538 1.1 pooka struct enic_softc *sc = ifp->if_softc;
539 1.1 pooka
540 1.1 pooka //printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
541 1.1 pooka log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
542 1.1 pooka ++ifp->if_oerrors;
543 1.1 pooka
544 1.1 pooka enic_reset(ifp);
545 1.1 pooka }
546 1.1 pooka
547 1.1 pooka int
548 1.1 pooka enic_mediachange(struct ifnet *ifp)
549 1.1 pooka {
550 1.1 pooka // struct enic_softc *sc = ifp->if_softc;
551 1.1 pooka /* more code here.. */
552 1.1 pooka
553 1.1 pooka return (0);
554 1.1 pooka }
555 1.1 pooka
556 1.1 pooka void
557 1.1 pooka enic_mediastatus(
558 1.1 pooka struct ifnet *ifp,
559 1.1 pooka struct ifmediareq *ifmr)
560 1.1 pooka {
561 1.1 pooka struct enic_softc *sc = ifp->if_softc;
562 1.1 pooka
563 1.1 pooka if ((ifp->if_flags & IFF_UP) == 0)
564 1.1 pooka return;
565 1.1 pooka
566 1.1 pooka ifmr->ifm_status = IFM_AVALID;
567 1.1 pooka if (sc->sc_havecarrier)
568 1.1 pooka ifmr->ifm_status |= IFM_ACTIVE;
569 1.1 pooka
570 1.1 pooka /* more code here someday.. */
571 1.1 pooka }
572 1.1 pooka
573 1.1 pooka /*
574 1.1 pooka * Process an ioctl request.
575 1.1 pooka */
576 1.1 pooka int
577 1.1 pooka enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
578 1.1 pooka {
579 1.1 pooka struct enic_softc *sc = ifp->if_softc;
580 1.1 pooka struct ifreq *ifr = (struct ifreq *)data;
581 1.1 pooka int s, error = 0;
582 1.1 pooka
583 1.1 pooka s = splnet();
584 1.1 pooka
585 1.1 pooka switch (cmd) {
586 1.1 pooka case SIOCGIFMEDIA:
587 1.1 pooka case SIOCSIFMEDIA:
588 1.1 pooka #if 0 /*DEBUG*/
589 1.1 pooka {
590 1.1 pooka extern int ei_drops[];
591 1.1 pooka static int flip = 0;
592 1.1 pooka if (flip++ == 2) {
593 1.1 pooka int i;
594 1.1 pooka flip = 0;
595 1.1 pooka printf("enic_ioctl(%x) %qd/%qd %qd/%qd %d/%d %d:%d "
596 1.1 pooka "%d+%d %d/%d/%d\n", ifp->if_flags,
597 1.1 pooka ifp->if_ierrors, ifp->if_oerrors,
598 1.1 pooka ifp->if_ipackets, ifp->if_opackets,
599 1.1 pooka sc->sc_no_rd, sc->sc_no_td,
600 1.1 pooka sc->xhit, sc->xmiss,
601 1.1 pooka sc->tfull, sc->tfull2,
602 1.1 pooka sc->brh, sc->rf, sc->bxh);
603 1.1 pooka printf(" Ctl %x lt %x tim %d\n",
604 1.1 pooka sc->sc_regs->Control, sc->it, ifp->if_timer);
605 1.1 pooka
606 1.1 pooka for (i = 0; i < 64; i++)
607 1.1 pooka if (ei_drops[i])
608 1.1 pooka printf(" %d.%d",i,ei_drops[i]);
609 1.1 pooka printf("\n");
610 1.1 pooka }
611 1.1 pooka }
612 1.1 pooka #endif
613 1.1 pooka error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
614 1.1 pooka break;
615 1.1 pooka
616 1.1 pooka default:
617 1.1 pooka error = ether_ioctl(ifp, cmd, data);
618 1.1 pooka if (cmd == SIOCSIFADDR) {
619 1.1 pooka /*
620 1.1 pooka * hackattack: NFS does not turn us back
621 1.1 pooka * on after a stop. So.
622 1.1 pooka */
623 1.1 pooka //printf("enic_ioctl(%lx)\n",cmd);
624 1.1 pooka enic_init(ifp);
625 1.1 pooka }
626 1.1 pooka if (error != ENETRESET)
627 1.1 pooka break;
628 1.1 pooka error = 0;
629 1.1 pooka if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
630 1.1 pooka break;
631 1.1 pooka if (ifp->if_flags & IFF_RUNNING) {
632 1.1 pooka enic_reset(ifp);
633 1.1 pooka }
634 1.1 pooka break;
635 1.1 pooka }
636 1.1 pooka splx(s);
637 1.1 pooka
638 1.1 pooka return (error);
639 1.1 pooka }
640 1.1 pooka
641 1.1 pooka int
642 1.1 pooka enic_intr(void *cookie, void *f)
643 1.1 pooka {
644 1.1 pooka struct enic_softc *sc = cookie;
645 1.1 pooka u_int32_t isr, saf, hi, lo, fl;
646 1.1 pooka
647 1.1 pooka isr = sc->sc_regs->Control;
648 1.1 pooka
649 1.1 pooka /* Make sure there is one and that we should take it */
650 1.1 pooka if ((isr & (EC_INTEN|EC_DONE)) != (EC_INTEN|EC_DONE))
651 1.1 pooka return (0);
652 1.1 pooka
653 1.1 pooka if (isr & EC_ERROR) {
654 1.1 pooka printf("%s: internal error\n", device_xname(sc->sc_dev));
655 1.1 pooka enic_reset(&sc->sc_ethercom.ec_if);
656 1.1 pooka return (1);
657 1.1 pooka }
658 1.1 pooka
659 1.1 pooka /* pull out all completed buffers
660 1.1 pooka */
661 1.1 pooka while ((isr & EC_OF_EMPTY) == 0) {
662 1.1 pooka
663 1.1 pooka /* beware, order matters */
664 1.1 pooka saf = sc->sc_regs->SizeAndFlags;
665 1.1 pooka hi = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */
666 1.1 pooka lo = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */
667 1.1 pooka
668 1.1 pooka fl = saf & (ES_F_MASK &~ ES_F_DONE);
669 1.1 pooka if (fl == ES_F_RECV)
670 1.1 pooka enic_rint(sc,saf,lo);
671 1.1 pooka else
672 1.1 pooka if (fl == ES_F_XMIT)
673 1.1 pooka enic_tint(sc,saf,lo);
674 1.1 pooka else
675 1.1 pooka /* we do not currently expect or care for command completions? */
676 1.1 pooka if (fl != ES_F_CMD)
677 1.1 pooka printf("%s: invalid saf=x%x (lo=%x)\n", device_xname(sc->sc_dev), saf, lo);
678 1.1 pooka
679 1.1 pooka isr = sc->sc_regs->Control;
680 1.1 pooka }
681 1.1 pooka
682 1.1 pooka #if NRND > 0
683 1.1 pooka rnd_add_uint32(&sc->rnd_source, isr);
684 1.1 pooka #endif
685 1.1 pooka
686 1.1 pooka return (1);
687 1.1 pooka }
688 1.1 pooka
689 1.1 pooka void
690 1.1 pooka enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
691 1.1 pooka {
692 1.1 pooka struct mbuf *m;
693 1.1 pooka struct ifnet *ifp = &sc->sc_ethercom.ec_if;
694 1.1 pooka int len = saf & ES_S_MASK, i;
695 1.1 pooka
696 1.1 pooka /* Find what buffer it is. Should be the first. */
697 1.1 pooka for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
698 1.1 pooka if (sc->sc_recv[i].phys == phys)
699 1.1 pooka goto found;
700 1.1 pooka for (i = 0; i < sc->sc_recv_h; i++)
701 1.1 pooka if (sc->sc_recv[i].phys == phys)
702 1.1 pooka goto found;
703 1.1 pooka
704 1.1 pooka /* uhu?? */
705 1.1 pooka printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev), (long long)phys);
706 1.1 pooka ifp->if_ierrors++;
707 1.1 pooka return;
708 1.1 pooka
709 1.1 pooka /* got it, pop it */
710 1.1 pooka found:
711 1.1 pooka sc->sc_no_rd--;
712 1.1 pooka m = sc->sc_recv[i].mbuf;
713 1.1 pooka sc->sc_recv[i].mbuf = NULL;
714 1.1 pooka sc->sc_recv[i].phys = ~0;
715 1.1 pooka if (i == sc->sc_recv_h) { /* should be */
716 1.1 pooka sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
717 1.1 pooka }
718 1.1 pooka #if DEBUG
719 1.1 pooka else sc->brh++;
720 1.1 pooka #endif
721 1.1 pooka
722 1.1 pooka if (len <= sizeof(struct ether_header) ||
723 1.1 pooka len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
724 1.1 pooka ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
725 1.1 pooka ETHERMTU + sizeof(struct ether_header))) {
726 1.1 pooka ifp->if_ierrors++;
727 1.1 pooka
728 1.1 pooka /* reuse it */
729 1.1 pooka enic_post_recv(sc,m);
730 1.1 pooka return;
731 1.1 pooka }
732 1.1 pooka
733 1.1 pooka /* Adjust size */
734 1.1 pooka m->m_pkthdr.len = len;
735 1.1 pooka m->m_len = len; /* recheck */
736 1.1 pooka
737 1.1 pooka ifp->if_ipackets++;
738 1.1 pooka
739 1.1 pooka /*
740 1.1 pooka * Check if there's a BPF listener on this interface.
741 1.1 pooka * If so, hand off the raw packet to BPF.
742 1.1 pooka */
743 1.1 pooka if (ifp->if_bpf)
744 1.1 pooka bpf_mtap(ifp, m);
745 1.1 pooka
746 1.1 pooka /* Pass the packet up. */
747 1.1 pooka (*ifp->if_input)(ifp, m);
748 1.1 pooka
749 1.1 pooka /* Need to refill now */
750 1.1 pooka enic_refill(sc);
751 1.1 pooka }
752 1.1 pooka
753 1.1 pooka void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
754 1.1 pooka {
755 1.1 pooka struct mbuf *m;
756 1.1 pooka struct ifnet *ifp = &sc->sc_ethercom.ec_if;
757 1.1 pooka int i;
758 1.1 pooka
759 1.1 pooka #if DEBUG
760 1.1 pooka sc->it = 1;
761 1.1 pooka #endif
762 1.1 pooka
763 1.1 pooka /* BUGBUG should there be a per-buffer error bit in SAF? */
764 1.1 pooka
765 1.1 pooka /* Find what buffer it is. Should be the first. */
766 1.1 pooka for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
767 1.1 pooka if (sc->sc_xmit[i].phys == phys)
768 1.1 pooka goto found;
769 1.1 pooka for (i = 0; i < sc->sc_xmit_h; i++)
770 1.1 pooka if (sc->sc_xmit[i].phys == phys)
771 1.1 pooka goto found;
772 1.1 pooka
773 1.1 pooka /* uhu?? */
774 1.1 pooka printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev), (long long)phys);
775 1.1 pooka ifp->if_oerrors++;
776 1.1 pooka return;
777 1.1 pooka
778 1.1 pooka /* got it, pop it */
779 1.1 pooka found:
780 1.1 pooka m = sc->sc_xmit[i].mbuf;
781 1.1 pooka sc->sc_xmit[i].mbuf = NULL;
782 1.1 pooka sc->sc_xmit[i].phys = ~0;
783 1.1 pooka if (i == sc->sc_xmit_h) { /* should be */
784 1.1 pooka sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
785 1.1 pooka }
786 1.1 pooka #if DEBUG
787 1.1 pooka else sc->bxh++;
788 1.1 pooka #endif
789 1.1 pooka m_freem(m);
790 1.1 pooka ifp->if_opackets++;
791 1.1 pooka
792 1.1 pooka if (--sc->sc_no_td == 0)
793 1.1 pooka ifp->if_timer = 0;
794 1.1 pooka
795 1.1 pooka ifp->if_flags &= ~IFF_OACTIVE;
796 1.1 pooka enic_start(ifp);
797 1.1 pooka #if DEBUG
798 1.1 pooka sc->it = 1;
799 1.1 pooka #endif
800 1.1 pooka }
801 1.1 pooka
802 1.1 pooka /*
803 1.1 pooka * Setup output on interface.
804 1.1 pooka * Get another datagram to send off of the interface queue, and map it to the
805 1.1 pooka * interface before starting the output.
806 1.1 pooka * Called only at splnet or interrupt level.
807 1.1 pooka */
808 1.1 pooka void
809 1.1 pooka enic_start(struct ifnet *ifp)
810 1.1 pooka {
811 1.1 pooka struct enic_softc *sc = ifp->if_softc;
812 1.1 pooka struct mbuf *m;
813 1.1 pooka int len, ix, s;
814 1.1 pooka paddr_t phys;
815 1.1 pooka
816 1.1 pooka #if DEBUG
817 1.1 pooka sc->it = 0;
818 1.1 pooka #endif
819 1.1 pooka
820 1.1 pooka //printf("enic_start(%x)\n", ifp->if_flags);
821 1.1 pooka
822 1.1 pooka if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
823 1.1 pooka return;
824 1.1 pooka
825 1.1 pooka s = splnet(); /* I know, I dont trust people.. */
826 1.1 pooka
827 1.1 pooka ix = sc->sc_xmit_h;
828 1.1 pooka for (;;) {
829 1.1 pooka
830 1.1 pooka /* Anything to do? */
831 1.1 pooka IFQ_POLL(&ifp->if_snd, m);
832 1.1 pooka if (m == 0)
833 1.1 pooka break;
834 1.1 pooka
835 1.1 pooka /* find a spot, if any */
836 1.1 pooka for (;ix < sc->sc_n_xmit; ix++)
837 1.1 pooka if (sc->sc_xmit[ix].mbuf == NULL)
838 1.1 pooka goto found;
839 1.1 pooka for (ix = 0; ix < sc->sc_xmit_h; ix++)
840 1.1 pooka if (sc->sc_xmit[ix].mbuf == NULL)
841 1.1 pooka goto found;
842 1.1 pooka /* oh well */
843 1.1 pooka ifp->if_flags |= IFF_OACTIVE;
844 1.1 pooka #if DEBUG
845 1.1 pooka sc->tfull++;
846 1.1 pooka #endif
847 1.1 pooka break;
848 1.1 pooka
849 1.1 pooka found:
850 1.1 pooka IFQ_DEQUEUE(&ifp->if_snd, m);
851 1.1 pooka if (m == 0)
852 1.1 pooka break;
853 1.1 pooka
854 1.1 pooka /*
855 1.1 pooka * If BPF is listening on this interface, let it see the packet
856 1.1 pooka * before we commit it to the wire.
857 1.1 pooka */
858 1.1 pooka if (ifp->if_bpf)
859 1.1 pooka bpf_mtap(ifp, m);
860 1.1 pooka
861 1.1 pooka /*
862 1.1 pooka * Copy the mbuf chain into a contiguous transmit buffer.
863 1.1 pooka */
864 1.1 pooka len = enic_put(sc, &m);
865 1.1 pooka if (len == 0)
866 1.1 pooka break; /* sanity */
867 1.1 pooka if (len > (ETHERMTU + sizeof(struct ether_header))) {
868 1.1 pooka printf("enic? tlen %d > %d\n", len, ETHERMTU + sizeof(struct ether_header));
869 1.1 pooka len = ETHERMTU + sizeof(struct ether_header);
870 1.1 pooka }
871 1.1 pooka
872 1.1 pooka ifp->if_timer = 5;
873 1.1 pooka
874 1.1 pooka /*
875 1.1 pooka * Remember and post the buffer
876 1.1 pooka */
877 1.1 pooka phys = kvtophys((vaddr_t)m->m_data);
878 1.1 pooka sc->sc_xmit[ix].mbuf = m;
879 1.1 pooka sc->sc_xmit[ix].phys = phys;
880 1.1 pooka
881 1.1 pooka sc->sc_no_td++;
882 1.1 pooka
883 1.1 pooka tpostone(phys,len);
884 1.1 pooka
885 1.1 pooka if (sc->sc_regs->Control & EC_IF_FULL) {
886 1.1 pooka ifp->if_flags |= IFF_OACTIVE;
887 1.1 pooka #if DEBUG
888 1.1 pooka sc->tfull2++;
889 1.1 pooka #endif
890 1.1 pooka break;
891 1.1 pooka }
892 1.1 pooka
893 1.1 pooka ix++;
894 1.1 pooka }
895 1.1 pooka
896 1.1 pooka splx(s);
897 1.1 pooka }
898 1.1 pooka
899 1.1 pooka int enic_put(struct enic_softc *sc, struct mbuf **pm)
900 1.1 pooka {
901 1.1 pooka struct mbuf *n, *m = *pm, *mm;
902 1.1 pooka int len, tlen = 0, xlen = m->m_pkthdr.len;
903 1.1 pooka uint8_t *cp;
904 1.1 pooka
905 1.1 pooka #if 0
906 1.1 pooka /* drop garbage */
907 1.1 pooka tlen = xlen;
908 1.1 pooka for (; m; m = n) {
909 1.1 pooka len = m->m_len;
910 1.1 pooka if (len == 0) {
911 1.1 pooka MFREE(m, n);
912 1.1 pooka if (m == *pm) *pm = n;
913 1.1 pooka continue;
914 1.1 pooka }
915 1.1 pooka tlen -= len;
916 1.1 pooka KASSERT(m != m->m_next);
917 1.1 pooka n = m->m_next;
918 1.1 pooka if (tlen <= 0) break;
919 1.1 pooka }
920 1.1 pooka
921 1.1 pooka /* We might be done: (a) empty chain (b) only one segment (c) bad chain */
922 1.1 pooka if (((m = *pm) == NULL) || (tlen > 0))
923 1.1 pooka xlen = 0;
924 1.1 pooka #endif
925 1.1 pooka
926 1.1 pooka if ((xlen == 0) || (xlen <= m->m_len)) {
927 1.1 pooka #if DEBUG
928 1.1 pooka sc->xhit++;
929 1.1 pooka #endif
930 1.1 pooka return xlen;
931 1.1 pooka }
932 1.1 pooka
933 1.1 pooka /* Nope, true chain. Copy to contig :-(( */
934 1.1 pooka tlen = xlen;
935 1.1 pooka MGETHDR(n, M_NOWAIT, MT_DATA);
936 1.1 pooka if (n == 0)
937 1.1 pooka goto Bad;
938 1.1 pooka n->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
939 1.1 pooka n->m_pkthdr.len = tlen;
940 1.1 pooka
941 1.1 pooka MCLGET(n, M_NOWAIT);
942 1.1 pooka if ((n->m_flags & M_EXT) == 0) {
943 1.1 pooka m_freem(n);
944 1.1 pooka goto Bad;
945 1.1 pooka }
946 1.1 pooka
947 1.1 pooka n->m_len = tlen;
948 1.1 pooka cp = mtod(n, uint8_t *);
949 1.1 pooka for (; m && tlen; m = mm) {
950 1.1 pooka
951 1.1 pooka len = m->m_len;
952 1.1 pooka if (len > tlen) len = tlen;
953 1.1 pooka if (len)
954 1.1 pooka memcpy(cp, mtod(m, void *), len);
955 1.1 pooka
956 1.1 pooka cp += len;
957 1.1 pooka tlen -= len;
958 1.1 pooka MFREE(m, mm);
959 1.1 pooka
960 1.1 pooka }
961 1.1 pooka
962 1.1 pooka *pm = n;
963 1.1 pooka #if DEBUG
964 1.1 pooka sc->xmiss++;
965 1.1 pooka #endif
966 1.1 pooka return (xlen);
967 1.1 pooka
968 1.1 pooka Bad:
969 1.1 pooka printf("enic_put: no mem?\n");
970 1.1 pooka m_freem(m);
971 1.1 pooka return 0;
972 1.1 pooka }
973