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