if_le_ebus.c revision 1.20 1 /* $NetBSD: if_le_ebus.c,v 1.20 2019/05/29 10:07:28 msaitoh 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.20 2019/05/29 10:07:28 msaitoh 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 struct enic_softc *sc = ifp->if_softc;
595 struct ifreq *ifr = (struct ifreq *)data;
596 int s, error = 0;
597
598 s = splnet();
599
600 switch (cmd) {
601 case SIOCSIFMEDIA:
602 #if 0 /*DEBUG*/
603 {
604 extern int ei_drops[];
605 static int flip = 0;
606 if (flip++ == 2) {
607 int i;
608 flip = 0;
609 printf("enic_ioctl(%x) %qd/%qd %qd/%qd %d/%d %d:%d "
610 "%d+%d %d/%d/%d\n", ifp->if_flags,
611 ifp->if_ierrors, ifp->if_oerrors,
612 ifp->if_ipackets, ifp->if_opackets,
613 sc->sc_no_rd, sc->sc_no_td,
614 sc->xhit, sc->xmiss,
615 sc->tfull, sc->tfull2,
616 sc->brh, sc->rf, sc->bxh);
617 printf(" Ctl %x lt %x tim %d\n",
618 sc->sc_regs->Control, sc->it, ifp->if_timer);
619
620 for (i = 0; i < 64; i++)
621 if (ei_drops[i])
622 printf(" %d.%d", i, ei_drops[i]);
623 printf("\n");
624 }
625 }
626 #endif
627 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
628 break;
629
630 default:
631 error = ether_ioctl(ifp, cmd, data);
632 if (cmd == SIOCSIFADDR) {
633 /*
634 * hackattack: NFS does not turn us back
635 * on after a stop. So.
636 */
637 #if 0
638 printf("enic_ioctl(%lx)\n", cmd);
639 #endif
640 enic_init(ifp);
641 }
642 if (error != ENETRESET)
643 break;
644 error = 0;
645 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
646 break;
647 if (ifp->if_flags & IFF_RUNNING) {
648 enic_reset(ifp);
649 }
650 break;
651 }
652 splx(s);
653
654 return error;
655 }
656
657 int
658 enic_intr(void *cookie, void *f)
659 {
660 struct enic_softc *sc = cookie;
661 uint32_t isr, saf, hi, lo, fl;
662
663 isr = sc->sc_regs->Control;
664
665 /* Make sure there is one and that we should take it */
666 if ((isr & (EC_INTEN | EC_DONE)) != (EC_INTEN | EC_DONE))
667 return 0;
668
669 if (isr & EC_ERROR) {
670 printf("%s: internal error\n", device_xname(sc->sc_dev));
671 enic_reset(&sc->sc_ethercom.ec_if);
672 return 1;
673 }
674
675 /*
676 * pull out all completed buffers
677 */
678 while ((isr & EC_OF_EMPTY) == 0) {
679
680 /* beware, order matters */
681 saf = sc->sc_regs->SizeAndFlags;
682 hi = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */
683 lo = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */
684 __USE(hi);
685
686 fl = saf & (ES_F_MASK &~ ES_F_DONE);
687 if (fl == ES_F_RECV)
688 enic_rint(sc, saf, lo);
689 else if (fl == ES_F_XMIT)
690 enic_tint(sc, saf, lo);
691 else {
692 /*
693 * we do not currently expect or care for
694 * command completions?
695 */
696 if (fl != ES_F_CMD)
697 printf("%s: invalid saf=x%x (lo=%x)\n",
698 device_xname(sc->sc_dev), saf, lo);
699 }
700
701 isr = sc->sc_regs->Control;
702 }
703
704 rnd_add_uint32(&sc->rnd_source, isr);
705
706 return 1;
707 }
708
709 void
710 enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
711 {
712 struct mbuf *m;
713 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
714 int len = saf & ES_S_MASK, i;
715
716 /* Find what buffer it is. Should be the first. */
717 for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
718 if (sc->sc_recv[i].phys == phys)
719 goto found;
720 for (i = 0; i < sc->sc_recv_h; i++)
721 if (sc->sc_recv[i].phys == phys)
722 goto found;
723
724 /* uhu?? */
725 printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev),
726 (long long)phys);
727 ifp->if_ierrors++;
728 return;
729
730 /* got it, pop it */
731 found:
732 sc->sc_no_rd--;
733 m = sc->sc_recv[i].mbuf;
734 sc->sc_recv[i].mbuf = NULL;
735 sc->sc_recv[i].phys = ~0;
736 if (i == sc->sc_recv_h) { /* should be */
737 sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
738 }
739 #if DEBUG
740 else
741 sc->brh++;
742 #endif
743
744 if (len <= sizeof(struct ether_header) ||
745 len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
746 ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
747 ETHERMTU + sizeof(struct ether_header))) {
748 ifp->if_ierrors++;
749
750 /* reuse it */
751 enic_post_recv(sc, m);
752 return;
753 }
754
755 /* Adjust size */
756 m->m_pkthdr.len = len;
757 m->m_len = len; /* recheck */
758
759 /* Pass the packet up. */
760 if_percpuq_enqueue(ifp->if_percpuq, m);
761
762 /* Need to refill now */
763 enic_refill(sc);
764 }
765
766 void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
767 {
768 struct mbuf *m;
769 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
770 int i;
771
772 #if DEBUG
773 sc->it = 1;
774 #endif
775
776 /* BUGBUG should there be a per-buffer error bit in SAF? */
777
778 /* Find what buffer it is. Should be the first. */
779 for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
780 if (sc->sc_xmit[i].phys == phys)
781 goto found;
782 for (i = 0; i < sc->sc_xmit_h; i++)
783 if (sc->sc_xmit[i].phys == phys)
784 goto found;
785
786 /* uhu?? */
787 printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev),
788 (long long)phys);
789 ifp->if_oerrors++;
790 return;
791
792 /* got it, pop it */
793 found:
794 m = sc->sc_xmit[i].mbuf;
795 sc->sc_xmit[i].mbuf = NULL;
796 sc->sc_xmit[i].phys = ~0;
797 if (i == sc->sc_xmit_h) { /* should be */
798 sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
799 }
800 #if DEBUG
801 else
802 sc->bxh++;
803 #endif
804 m_freem(m);
805 ifp->if_opackets++;
806
807 if (--sc->sc_no_td == 0)
808 ifp->if_timer = 0;
809
810 ifp->if_flags &= ~IFF_OACTIVE;
811 if_schedule_deferred_start(ifp);
812 #if DEBUG
813 sc->it = 1;
814 #endif
815 }
816
817 /*
818 * Setup output on interface.
819 * Get another datagram to send off of the interface queue, and map it to the
820 * interface before starting the output.
821 * Called only at splnet or interrupt level.
822 */
823 void
824 enic_start(struct ifnet *ifp)
825 {
826 struct enic_softc *sc = ifp->if_softc;
827 struct mbuf *m;
828 int len, ix, s;
829 paddr_t phys;
830
831 #if DEBUG
832 sc->it = 0;
833 #endif
834
835 #if 0
836 printf("enic_start(%x)\n", ifp->if_flags);
837 #endif
838
839 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
840 return;
841
842 s = splnet(); /* I know, I dont trust people.. */
843
844 ix = sc->sc_xmit_h;
845 for (;;) {
846
847 /* Anything to do? */
848 IFQ_POLL(&ifp->if_snd, m);
849 if (m == NULL)
850 break;
851
852 /* find a spot, if any */
853 for (; ix < sc->sc_n_xmit; ix++)
854 if (sc->sc_xmit[ix].mbuf == NULL)
855 goto found;
856 for (ix = 0; ix < sc->sc_xmit_h; ix++)
857 if (sc->sc_xmit[ix].mbuf == NULL)
858 goto found;
859 /* oh well */
860 ifp->if_flags |= IFF_OACTIVE;
861 #if DEBUG
862 sc->tfull++;
863 #endif
864 break;
865
866 found:
867 IFQ_DEQUEUE(&ifp->if_snd, m);
868 if (m == NULL)
869 break;
870
871 /*
872 * If BPF is listening on this interface, let it see the packet
873 * before we commit it to the wire.
874 */
875 bpf_mtap(ifp, m, BPF_D_OUT);
876
877 /*
878 * Copy the mbuf chain into a contiguous transmit buffer.
879 */
880 len = enic_put(sc, &m);
881 if (len == 0)
882 break; /* sanity */
883 if (len > (ETHERMTU + sizeof(struct ether_header))) {
884 printf("enic? tlen %d > %d\n",
885 len, ETHERMTU + sizeof(struct ether_header));
886 len = ETHERMTU + sizeof(struct ether_header);
887 }
888
889 ifp->if_timer = 5;
890
891 /*
892 * Remember and post the buffer
893 */
894 phys = kvtophys((vaddr_t)m->m_data);
895 sc->sc_xmit[ix].mbuf = m;
896 sc->sc_xmit[ix].phys = phys;
897
898 sc->sc_no_td++;
899
900 tpostone(phys, len);
901
902 if (sc->sc_regs->Control & EC_IF_FULL) {
903 ifp->if_flags |= IFF_OACTIVE;
904 #if DEBUG
905 sc->tfull2++;
906 #endif
907 break;
908 }
909
910 ix++;
911 }
912
913 splx(s);
914 }
915
916 int enic_put(struct enic_softc *sc, struct mbuf **pm)
917 {
918 struct mbuf *n, *m = *pm, *mm;
919 int len, tlen = 0, xlen = m->m_pkthdr.len;
920 uint8_t *cp;
921
922 #if 0
923 /* drop garbage */
924 tlen = xlen;
925 for (; m; m = n) {
926 len = m->m_len;
927 if (len == 0) {
928 n = m_free(m);
929 if (m == *pm)
930 *pm = n;
931 continue;
932 }
933 tlen -= len;
934 KASSERT(m != m->m_next);
935 n = m->m_next;
936 if (tlen <= 0)
937 break;
938 }
939
940 /*
941 * We might be done:
942 * (a) empty chain (b) only one segment (c) bad chain
943 */
944 if (((m = *pm) == NULL) || (tlen > 0))
945 xlen = 0;
946 #endif
947
948 if ((xlen == 0) || (xlen <= m->m_len)) {
949 #if DEBUG
950 sc->xhit++;
951 #endif
952 return xlen;
953 }
954
955 /* Nope, true chain. Copy to contig :-(( */
956 tlen = xlen;
957 MGETHDR(n, M_NOWAIT, MT_DATA);
958 if (n == NULL)
959 goto Bad;
960 m_set_rcvif(n, &sc->sc_ethercom.ec_if);
961 n->m_pkthdr.len = tlen;
962
963 MCLGET(n, M_NOWAIT);
964 if ((n->m_flags & M_EXT) == 0) {
965 m_freem(n);
966 goto Bad;
967 }
968
969 n->m_len = tlen;
970 cp = mtod(n, uint8_t *);
971 for (; m && tlen; m = mm) {
972
973 len = m->m_len;
974 if (len > tlen)
975 len = tlen;
976 if (len)
977 memcpy(cp, mtod(m, void *), len);
978
979 cp += len;
980 tlen -= len;
981 mm = m_free(m);
982
983 }
984
985 *pm = n;
986 #if DEBUG
987 sc->xmiss++;
988 #endif
989 return (xlen);
990
991 Bad:
992 printf("enic_put: no mem?\n");
993 m_freem(m);
994 return 0;
995 }
996