if_le_ebus.c revision 1.18 1 /* $NetBSD: if_le_ebus.c,v 1.18 2019/05/29 05:06:39 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.18 2019/05/29 05:06:39 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 ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
198 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
199 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
200
201 /* Make sure the chip is stopped. */
202 enic_stop(ifp, 0);
203 sc->inited = 0;
204
205 /* Get the mac address and print it */
206 printf(": eNIC [%d %d], address %s\n",
207 sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));
208
209 /* claim 802.1q capability */
210 #if 0
211 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
212 #endif
213
214 /* Attach the interface. */
215 if_attach(ifp);
216 if_deferred_start_init(ifp, NULL);
217 ether_ifattach(ifp, sc->sc_enaddr);
218
219 sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
220 if (sc->sc_sh == NULL)
221 panic("enic_attach: cannot establish shutdown hook");
222
223 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
224 RND_TYPE_NET, RND_FLAG_DEFAULT);
225
226 ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET,
227 enic_intr, sc);
228 }
229
230 /*
231 * Beware: does not work while the nic is running
232 */
233 static int enic_gethwinfo(struct enic_softc *sc)
234 {
235 uint8_t buffer[8]; /* 64bits max */
236 PENIC_INFO hw = (PENIC_INFO)buffer;
237 paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
238 int i;
239
240 /*
241 * First thing first, get the MAC address
242 */
243 memset(buffer, 0, sizeof buffer);
244 buffer[0] = ENIC_CMD_GET_ADDRESS;
245 buffer[3] = ENIC_CMD_GET_ADDRESS; /* bswap bug */
246 sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
247 sc->sc_regs->BufferAddressHi32 = 0;
248 sc->sc_regs->BufferAddressLo32 = phys; /* go! */
249
250 for (i = 0; i < 100; i++) {
251 DELAY(100);
252 if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
253 break;
254 }
255 if (i == 100)
256 return 0;
257
258 phys2 = sc->sc_regs->BufferAddressLo32;
259 if (phys2 != phys) {
260 printf("enic uhu? %llx != %llx?\n",
261 (long long)phys, (long long)phys2);
262 return 0;
263 }
264 memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN);
265
266 /*
267 * Next get the HW parameters
268 */
269 memset(buffer, 0, sizeof buffer);
270 buffer[0] = ENIC_CMD_GET_INFO;
271 buffer[3] = ENIC_CMD_GET_INFO; /* bswap bug */
272 sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
273 sc->sc_regs->BufferAddressHi32 = 0;
274 sc->sc_regs->BufferAddressLo32 = phys; /* go! */
275
276 for (i = 0; i < 100; i++) {
277 DELAY(100);
278 if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
279 break;
280 }
281 if (i == 100)
282 return 0;
283
284 phys2 = sc->sc_regs->BufferAddressLo32;
285 if (phys2 != phys) {
286 printf("enic uhu2? %llx != %llx?\n",
287 (long long)phys, (long long)phys2);
288 return 0;
289 }
290 #if 0
291 printf("enic: hwinfo: %x %x %x %x %x %x \n",
292 hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
293 hw->ErrorCount, hw->FramesDropped, hw->Reserved);
294 #endif
295
296 /*
297 * Get FIFO depths and cap them
298 */
299 sc->sc_n_recv = hw->InputFifoSize;
300 if (sc->sc_n_recv > SC_MAX_N_RECV)
301 sc->sc_n_recv = SC_MAX_N_RECV;
302 if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */
303 sc->sc_n_recv = 8;
304 sc->sc_n_xmit = 4;
305 } else {
306 sc->sc_n_xmit = hw->OutputFifoSize;
307 if (sc->sc_n_xmit > SC_MAX_N_XMIT)
308 sc->sc_n_xmit = SC_MAX_N_XMIT;
309 }
310
311 return 1;
312 }
313
314 void
315 enic_reset(struct ifnet *ifp)
316 {
317 int s;
318
319 s = splnet();
320 enic_stop(ifp,0);
321 enic_init(ifp);
322 splx(s);
323 }
324
325 void
326 enic_stop(struct ifnet *ifp, int suspend)
327 {
328 struct enic_softc *sc = ifp->if_softc;
329
330 #if 0
331 printf("enic_stop %x\n", sc->sc_regs->Control);
332 #endif
333
334 /*
335 * NB: only "ifconfig down" says suspend=1 (then "up" calls init)
336 * Could simply set RXDIS in this case
337 */
338 sc->inited = 2;
339 sc->sc_regs->Control = EC_RESET;
340 sc->sc_no_rd = 0; /* they are gone */
341 sc->sc_no_td = 0; /* they are gone */
342 }
343
344 void
345 enic_shutdown(void *arg)
346 {
347 struct ifnet *ifp = arg;
348
349 enic_stop(ifp, 0);
350 }
351
352 void
353 enic_kill_xmit(struct enic_softc *sc)
354 {
355 int i;
356 struct mbuf *m;
357
358 for (i = 0; i < sc->sc_n_xmit; i++) {
359 if ((m = sc->sc_xmit[i].mbuf) != NULL) {
360 sc->sc_xmit[i].mbuf = NULL;
361 sc->sc_xmit[i].phys = ~0;
362 m_freem(m);
363 }
364 }
365 sc->sc_no_td = 0;
366 sc->sc_xmit_h = 0;
367 }
368
369 void
370 enic_post_recv(struct enic_softc *sc, struct mbuf *m)
371 {
372 int i, waitmode = M_DONTWAIT;
373 paddr_t phys;
374
375 #define rpostone(_p_) \
376 sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
377 sc->sc_regs->BufferAddressHi32 = 0; \
378 sc->sc_regs->BufferAddressLo32 = _p_;
379 #define tpostone(_p_,_s_) \
380 sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
381 sc->sc_regs->BufferAddressHi32 = 0; \
382 sc->sc_regs->BufferAddressLo32 = _p_;
383
384 /* Operational reload? */
385 if (m != NULL) {
386 /* But is the hw ready for it */
387 if (sc->sc_regs->Control & EC_IF_FULL)
388 goto no_room;
389 /* Yes, find a spot. Include empty q case. */
390 for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
391 if (sc->sc_recv[i].mbuf == NULL)
392 goto found;
393 for (i = 0; i < sc->sc_recv_h; i++)
394 if (sc->sc_recv[i].mbuf == NULL)
395 goto found;
396 /* no spot, drop it (sigh) */
397 no_room:
398 #if DEBUG
399 sc->rf++;
400 #endif
401 m_freem(m);
402 return;
403 found:
404 phys = kvtophys((vaddr_t)m->m_data);
405 sc->sc_recv[i].mbuf = m;
406 sc->sc_recv[i].phys = phys;
407 rpostone(phys);
408 sc->sc_no_rd++;
409 return;
410 }
411
412 /* Repost after reset? */
413 if (sc->inited) {
414 /* order doesnt matter, might as well keep it clean */
415 int j = 0;
416 sc->sc_recv_h = 0;
417 for (i = 0; i < sc->sc_n_recv; i++)
418 if ((m = sc->sc_recv[i].mbuf) != NULL) {
419 phys = sc->sc_recv[i].phys;
420 sc->sc_recv[i].mbuf = NULL;
421 sc->sc_recv[i].phys = ~0;
422 sc->sc_recv[j].mbuf = m;
423 sc->sc_recv[j].phys = phys;
424 #if DEBUG
425 if (sc->sc_regs->Control & EC_IF_FULL)
426 printf("?uhu? postrecv full? %d\n",
427 sc->sc_no_rd);
428 #endif
429 sc->sc_no_rd++;
430 rpostone(phys);
431 j++;
432 }
433 /* Any holes left? */
434 sc->inited = 1;
435 if (j >= sc->sc_n_recv)
436 return; /* no, we are done */
437 /* continue on with the loop below */
438 i = j; m = NULL;
439 goto fillem;
440 }
441
442 /* Initial refill, we can wait */
443 waitmode = M_WAIT;
444 sc->sc_recv_h = 0;
445 memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
446 i = 0;
447 fillem:
448 for (; i < sc->sc_n_recv; i++) {
449 MGETHDR(m, waitmode, MT_DATA);
450 if (m == 0)
451 break;
452 m_set_rcvif(m, &sc->sc_ethercom.ec_if);
453 m->m_pkthdr.len = 0;
454
455 MCLGET(m, waitmode);
456 if ((m->m_flags & M_EXT) == 0)
457 break;
458
459 /*
460 * This offset aligns IP/TCP headers and helps performance
461 */
462 #if 1
463 #define ADJUST_MBUF_OFFSET(_m_) { \
464 (_m_)->m_data += 2; \
465 (_m_)->m_len -= 2; \
466 }
467 #else
468 #define ADJUST_MBUF_OFFSET(_m_)
469 #endif
470
471 m->m_len = MCLBYTES;
472
473 ADJUST_MBUF_OFFSET(m);
474 phys = kvtophys((vaddr_t)m->m_data);
475 sc->sc_recv[i].mbuf = m;
476 sc->sc_recv[i].phys = phys;
477 #if DEBUG
478 if (sc->sc_regs->Control & EC_IF_FULL)
479 printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
480 #endif
481 sc->sc_no_rd++;
482 rpostone(phys);
483 m = NULL;
484 }
485
486 if (m)
487 m_freem(m);
488 sc->inited = 1;
489 }
490
491 void enic_refill(struct enic_softc *sc)
492 {
493 struct mbuf *m;
494 int waitmode = M_DONTWAIT;
495
496 MGETHDR(m, waitmode, MT_DATA);
497 if (m == NULL)
498 return;
499 m_set_rcvif(m, &sc->sc_ethercom.ec_if);
500 m->m_pkthdr.len = 0;
501
502 MCLGET(m, waitmode);
503 if ((m->m_flags & M_EXT) == 0) {
504 m_freem(m);
505 return;
506 }
507
508 m->m_len = MCLBYTES;
509 ADJUST_MBUF_OFFSET(m);
510
511 enic_post_recv(sc,m);
512 }
513
514 int
515 enic_init(struct ifnet *ifp)
516 {
517 struct enic_softc *sc = ifp->if_softc;
518 uint32_t ctl;
519
520 /* no need to init many times unless we are in reset */
521 if (sc->inited != 1) {
522
523 /* Cancel all xmit buffers */
524 enic_kill_xmit(sc);
525
526 /* Re-post all recv buffers */
527 enic_post_recv(sc, NULL);
528 }
529
530 /* Start the eNIC */
531 ifp->if_flags |= IFF_RUNNING;
532 ifp->if_flags &= ~IFF_OACTIVE;
533 ifp->if_timer = 0;
534 ctl = sc->sc_regs->Control | EC_INTEN;
535 ctl &= ~EC_RXDIS;
536 sc->sc_regs->Control = ctl;
537 #if 0
538 printf("enic_init <- %x\n", ctl);
539 #endif
540
541 if_schedule_deferred_start(ifp);
542
543 return 0;
544 }
545
546
547 void
548 enic_watchdog(struct ifnet *ifp)
549 {
550 struct enic_softc *sc = ifp->if_softc;
551
552 #if 0
553 printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
554 #endif
555 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
556 ++ifp->if_oerrors;
557
558 enic_reset(ifp);
559 }
560
561 int
562 enic_mediachange(struct ifnet *ifp)
563 {
564 #if 0
565 struct enic_softc *sc = ifp->if_softc;
566 #endif
567 /* more code here.. */
568
569 return 0;
570 }
571
572 void
573 enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
574 {
575 struct enic_softc *sc = ifp->if_softc;
576
577 if ((ifp->if_flags & IFF_UP) == 0)
578 return;
579
580 ifmr->ifm_status = IFM_AVALID;
581 if (sc->sc_havecarrier)
582 ifmr->ifm_status |= IFM_ACTIVE;
583
584 /* more code here someday.. */
585 }
586
587 /*
588 * Process an ioctl request.
589 */
590 int
591 enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
592 {
593 struct enic_softc *sc = ifp->if_softc;
594 struct ifreq *ifr = (struct ifreq *)data;
595 int s, error = 0;
596
597 s = splnet();
598
599 switch (cmd) {
600 case SIOCGIFMEDIA:
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