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