]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/lmc/if_lmc.c
This commit was generated by cvs2svn to compensate for changes in r119256,
[FreeBSD/FreeBSD.git] / sys / dev / lmc / if_lmc.c
1 /*-
2  * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
3  * Copyright (c) LAN Media Corporation 1998, 1999.
4  * Copyright (c) 2000 Stephen Kiernan (sk-ports@vegamuse.org)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *      From NetBSD: if_de.c,v 1.56.2.1 1997/10/27 02:13:25 thorpej Exp
28  *      $Id: if_lmc.c,v 1.9 1999/02/19 15:08:42 explorer Exp $
29  */
30
31 #ifdef COMPILING_LINT
32 #warning "The lmc driver is broken and is not compiled with LINT"
33 #else
34
35 char lmc_version[] = "BSD 1.1";
36
37 #include "opt_netgraph.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45
46 #include <net/if.h>
47 #include <sys/syslog.h>
48
49 #include <vm/vm.h>
50
51 #include <netgraph/ng_message.h>
52 #include <netgraph/ng_parse.h>
53 #include <netgraph/netgraph.h>
54
55 #include <vm/pmap.h>
56 #include <pci/pcivar.h>
57 #include <pci/dc21040reg.h>
58 #define INCLUDE_PATH_PREFIX "dev/lmc/"
59
60 /* Intel CPUs should use I/O mapped access.  */
61 #if defined(__i386__)
62 #define LMC_IOMAPPED
63 #endif
64
65 /*
66  * This turns on all sort of debugging stuff and make the
67  * driver much larger.
68  */
69 #ifdef LMC_DEBUG
70 #define DP(x)   printf x
71 #else
72 #define DP(x)
73 #endif
74
75 #define LMC_HZ  10
76
77 #ifndef TULIP_GP_PINSET
78 #define TULIP_GP_PINSET                 0x00000100L
79 #endif
80 #ifndef TULIP_BUSMODE_READMULTIPLE
81 #define TULIP_BUSMODE_READMULTIPLE      0x00200000L
82 #endif
83
84 /*
85  * C sucks
86  */
87 typedef struct lmc___softc lmc_softc_t;
88 typedef struct lmc___media lmc_media_t;
89 typedef struct lmc___ctl lmc_ctl_t;
90
91 #include "dev/lmc/if_lmcioctl.h"
92 #include "dev/lmc/if_lmcvar.h"
93 #include "dev/lmc/if_lmc_common.c"
94 #include "dev/lmc/if_lmc_media.c"
95
96 /*
97  * This module supports
98  *      the DEC 21140A pass 2.2 PCI Fast Ethernet Controller.
99  */
100 static lmc_intrfunc_t lmc_intr_normal(void *);
101 static ifnet_ret_t lmc_ifstart(lmc_softc_t * const sc );
102 static ifnet_ret_t lmc_ifstart_one(lmc_softc_t * const sc);
103 static struct mbuf *lmc_txput(lmc_softc_t * const sc, struct mbuf *m);
104 static void lmc_rx_intr(lmc_softc_t * const sc);
105
106 static void lmc_watchdog(lmc_softc_t * const sc);
107 static void lmc_ifup(lmc_softc_t * const sc);
108 static void lmc_ifdown(lmc_softc_t * const sc);
109
110 #ifdef LMC_DEBUG
111 static void ng_lmc_dump_packet(struct mbuf *m);
112 #endif /* LMC_DEBUG */
113 static void ng_lmc_watchdog_frame(void *arg);
114 static void ng_lmc_init(void *ignored);
115
116 static ng_constructor_t ng_lmc_constructor;
117 static ng_rcvmsg_t      ng_lmc_rcvmsg;
118 static ng_shutdown_t    ng_lmc_rmnode;
119 static ng_newhook_t     ng_lmc_newhook;
120 /*static ng_findhook_t  ng_lmc_findhook; */
121 static ng_connect_t     ng_lmc_connect;
122 static ng_rcvdata_t     ng_lmc_rcvdata;
123 static ng_disconnect_t  ng_lmc_disconnect;
124
125 /* Parse type for struct lmc_ctl */
126 static const struct ng_parse_fixedarray_info ng_lmc_ctl_cardspec_info = {
127         &ng_parse_int32_type,
128         7,
129         NULL
130 };
131
132 static const struct ng_parse_type ng_lmc_ctl_cardspec_type = {
133         &ng_parse_fixedarray_type,
134         &ng_lmc_ctl_cardspec_info
135 };
136
137 static const struct ng_parse_struct_info ng_lmc_ctl_type_info = {
138         {
139                 { "cardtype",           &ng_parse_int32_type            },
140                 { "clock_source",       &ng_parse_int32_type            },
141                 { "clock_rate",         &ng_parse_int32_type            },
142                 { "crc_length",         &ng_parse_int32_type            },
143                 { "cable_length",       &ng_parse_int32_type            },
144                 { "scrambler_onoff",    &ng_parse_int32_type            },
145                 { "cable_type",         &ng_parse_int32_type            },
146                 { "keepalive_onoff",    &ng_parse_int32_type            },
147                 { "ticks",              &ng_parse_int32_type            },
148                 { "cardspec",           &ng_lmc_ctl_cardspec_type       },
149                 { "circuit_type",       &ng_parse_int32_type            },
150                 { NULL },
151         }
152 };
153
154 static const struct ng_parse_type ng_lmc_ctl_type = {
155         &ng_parse_struct_type,
156         &ng_lmc_ctl_type_info
157 };
158
159
160 /* List of commands and how to convert arguments to/from ASCII */
161 static const struct ng_cmdlist ng_lmc_cmdlist[] = {
162         {
163           NG_LMC_COOKIE,
164           NGM_LMC_GET_CTL,
165           "getctl",
166           NULL,
167           &ng_lmc_ctl_type,
168         },
169         {
170           NG_LMC_COOKIE,
171           NGM_LMC_SET_CTL,
172           "setctl",
173           &ng_lmc_ctl_type,
174           NULL
175         },
176         { 0 }
177 };
178
179 static struct ng_type typestruct = {
180         NG_ABI_VERSION,
181         NG_LMC_NODE_TYPE,
182         NULL,
183         ng_lmc_constructor,
184         ng_lmc_rcvmsg,
185         ng_lmc_rmnode,
186         ng_lmc_newhook,
187         NULL,
188         ng_lmc_connect,
189         ng_lmc_rcvdata,
190         ng_lmc_disconnect,
191         ng_lmc_cmdlist
192 };
193
194 static int ng_lmc_done_init = 0;
195
196 \f
197 /*
198  * Code the read the SROM and MII bit streams (I2C)
199  */
200 static void
201 lmc_delay_300ns(lmc_softc_t * const sc)
202 {
203         int idx;
204         for (idx = (300 / 33) + 1; idx > 0; idx--)
205                 (void)LMC_CSR_READ(sc, csr_busmode);
206 }
207 \f
208
209 #define EMIT    \
210 do { \
211         LMC_CSR_WRITE(sc, csr_srom_mii, csr); \
212         lmc_delay_300ns(sc); \
213 } while (0)
214
215 static void
216 lmc_srom_idle(lmc_softc_t * const sc)
217 {
218         unsigned bit, csr;
219     
220         csr  = SROMSEL ; EMIT;
221         csr  = SROMSEL | SROMRD; EMIT;  
222         csr ^= SROMCS; EMIT;
223         csr ^= SROMCLKON; EMIT;
224
225         /*
226          * Write 25 cycles of 0 which will force the SROM to be idle.
227          */
228         for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
229                 csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
230                 csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
231         }
232         csr ^= SROMCLKOFF; EMIT;
233         csr ^= SROMCS; EMIT;
234         csr  = 0; EMIT;
235 }
236
237      
238 static void
239 lmc_srom_read(lmc_softc_t * const sc)
240 {   
241         unsigned idx; 
242         const unsigned bitwidth = SROM_BITWIDTH;
243         const unsigned cmdmask = (SROMCMD_RD << bitwidth);
244         const unsigned msb = 1 << (bitwidth + 3 - 1);
245         unsigned lastidx = (1 << bitwidth) - 1;
246
247         lmc_srom_idle(sc);
248
249         for (idx = 0; idx <= lastidx; idx++) {
250                 unsigned lastbit, data, bits, bit, csr;
251                 csr  = SROMSEL ;                EMIT;
252                 csr  = SROMSEL | SROMRD;        EMIT;
253                 csr ^= SROMCSON;                EMIT;
254                 csr ^=            SROMCLKON;    EMIT;
255     
256                 lastbit = 0;
257                 for (bits = idx|cmdmask, bit = bitwidth + 3
258                              ; bit > 0
259                              ; bit--, bits <<= 1) {
260                         const unsigned thisbit = bits & msb;
261                         csr ^= SROMCLKOFF; EMIT;    /* clock L data invalid */
262                         if (thisbit != lastbit) {
263                                 csr ^= SROMDOUT; EMIT;/* clock L invert data */
264                         } else {
265                                 EMIT;
266                         }
267                         csr ^= SROMCLKON; EMIT;     /* clock H data valid */
268                         lastbit = thisbit;
269                 }
270                 csr ^= SROMCLKOFF; EMIT;
271
272                 for (data = 0, bits = 0; bits < 16; bits++) {
273                         data <<= 1;
274                         csr ^= SROMCLKON; EMIT;     /* clock H data valid */ 
275                         data |= LMC_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
276                         csr ^= SROMCLKOFF; EMIT;    /* clock L data invalid */
277                 }
278                 sc->lmc_rombuf[idx*2] = data & 0xFF;
279                 sc->lmc_rombuf[idx*2+1] = data >> 8;
280                 csr  = SROMSEL | SROMRD; EMIT;
281                 csr  = 0; EMIT;
282         }
283         lmc_srom_idle(sc);
284 }
285 \f
286 #define MII_EMIT    do { LMC_CSR_WRITE(sc, csr_srom_mii, csr); lmc_delay_300ns(sc); } while (0)
287
288 static void
289 lmc_mii_writebits(lmc_softc_t * const sc, unsigned data, unsigned bits)
290 {
291     unsigned msb = 1 << (bits - 1);
292     unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
293     unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
294
295     csr |= MII_WR; MII_EMIT;            /* clock low; assert write */
296
297     for (; bits > 0; bits--, data <<= 1) {
298         const unsigned thisbit = data & msb;
299         if (thisbit != lastbit) {
300             csr ^= MII_DOUT; MII_EMIT;  /* clock low; invert data */
301         }
302         csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
303         lastbit = thisbit;
304         csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
305     }
306 }
307
308 static void
309 lmc_mii_turnaround(lmc_softc_t * const sc, unsigned cmd)
310 {
311     unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
312
313     if (cmd == MII_WRCMD) {
314         csr |= MII_DOUT; MII_EMIT;      /* clock low; change data */
315         csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
316         csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
317         csr ^= MII_DOUT; MII_EMIT;      /* clock low; change data */
318     } else {
319         csr |= MII_RD; MII_EMIT;        /* clock low; switch to read */
320     }
321     csr ^= MII_CLKON; MII_EMIT;         /* clock high; data valid */
322     csr ^= MII_CLKOFF; MII_EMIT;        /* clock low; data not valid */
323 }
324
325 static unsigned
326 lmc_mii_readbits(lmc_softc_t * const sc)
327 {
328     unsigned data;
329     unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
330     int idx;
331
332     for (idx = 0, data = 0; idx < 16; idx++) {
333         data <<= 1;     /* this is NOOP on the first pass through */
334         csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
335         if (LMC_CSR_READ(sc, csr_srom_mii) & MII_DIN)
336             data |= 1;
337         csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
338     }
339     csr ^= MII_RD; MII_EMIT;            /* clock low; turn off read */
340
341     return data;
342 }
343
344 static unsigned
345 lmc_mii_readreg(lmc_softc_t * const sc, unsigned devaddr, unsigned regno)
346 {
347     unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
348     unsigned data;
349
350     csr &= ~(MII_RD|MII_CLK); MII_EMIT;
351     lmc_mii_writebits(sc, MII_PREAMBLE, 32);
352     lmc_mii_writebits(sc, MII_RDCMD, 8);
353     lmc_mii_writebits(sc, devaddr, 5);
354     lmc_mii_writebits(sc, regno, 5);
355     lmc_mii_turnaround(sc, MII_RDCMD);
356
357     data = lmc_mii_readbits(sc);
358     return data;
359 }
360
361 static void
362 lmc_mii_writereg(lmc_softc_t * const sc, unsigned devaddr,
363                    unsigned regno, unsigned data)
364 {
365     unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
366     csr &= ~(MII_RD|MII_CLK); MII_EMIT;
367     lmc_mii_writebits(sc, MII_PREAMBLE, 32);
368     lmc_mii_writebits(sc, MII_WRCMD, 8);
369     lmc_mii_writebits(sc, devaddr, 5);
370     lmc_mii_writebits(sc, regno, 5);
371     lmc_mii_turnaround(sc, MII_WRCMD);
372     lmc_mii_writebits(sc, data, 16);
373 }
374 \f
375 static int
376 lmc_read_macaddr(lmc_softc_t * const sc)
377 {
378         lmc_srom_read(sc);
379
380         bcopy(sc->lmc_rombuf + 20, sc->lmc_enaddr, 6);
381
382         return 0;
383 }
384 \f
385 /*
386  * Check to make certain there is a signal from the modem, and flicker
387  * lights as needed.
388  */
389 static void
390 lmc_watchdog(lmc_softc_t * const sc)
391 {
392         int state;
393         u_int32_t ostatus;
394         u_int32_t link_status;
395         u_int32_t ticks;
396
397         state = 0;
398
399         link_status = sc->lmc_media->get_link_status(sc);
400         ostatus = ((sc->lmc_flags & LMC_MODEMOK) == LMC_MODEMOK);
401
402         /*
403          * hardware level link lost, but the interface is marked as up.
404          * Mark it as down.
405          */
406         if (link_status == 0 && ostatus) {
407                 printf(LMC_PRINTF_FMT ": physical link down\n",
408                        LMC_PRINTF_ARGS);
409                 sc->lmc_flags &= ~LMC_MODEMOK;
410                 lmc_led_off(sc, LMC_MII16_LED1);
411         }
412
413         /*
414          * hardware link is up, but the interface is marked as down.
415          * Bring it back up again.
416          */
417         if (link_status != 0 && !ostatus) {
418                 printf(LMC_PRINTF_FMT ": physical link up\n",
419                        LMC_PRINTF_ARGS);
420                 if (sc->lmc_flags & LMC_IFUP)
421                         lmc_ifup(sc);
422                 return;
423         }
424
425         /*
426          * remember the timer value
427          */
428         ticks = LMC_CSR_READ(sc, csr_gp_timer);
429         LMC_CSR_WRITE(sc, csr_gp_timer, 0xffffffffUL);
430         sc->ictl.ticks = 0x0000ffff - (ticks & 0x0000ffff);
431
432         sc->lmc_out_dog = LMC_DOG_HOLDOFF;
433 }
434
435 /*
436  * Mark the interface as "up" and enable TX/RX and TX/RX interrupts.
437  * This also does a full software reset.
438  */
439 static void
440 lmc_ifup(lmc_softc_t * const sc)
441 {
442         untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
443         sc->lmc_running = 0;
444
445         lmc_dec_reset(sc);
446         lmc_reset(sc);
447
448         sc->lmc_media->set_link_status(sc, 1);
449         sc->lmc_media->set_status(sc, NULL);
450
451         sc->lmc_flags |= LMC_IFUP;
452
453         /*
454          * select what interrupts we want to get
455          */
456         sc->lmc_intrmask |= (TULIP_STS_NORMALINTR
457                                | TULIP_STS_RXINTR
458                                | TULIP_STS_TXINTR
459                                | TULIP_STS_ABNRMLINTR
460                                | TULIP_STS_SYSERROR
461                                | TULIP_STS_TXSTOPPED
462                                | TULIP_STS_TXUNDERFLOW
463                                | TULIP_STS_RXSTOPPED
464                                );
465         LMC_CSR_WRITE(sc, csr_intr, sc->lmc_intrmask);
466
467         sc->lmc_cmdmode |= TULIP_CMD_TXRUN;
468         sc->lmc_cmdmode |= TULIP_CMD_RXRUN;
469         LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
470
471         untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
472         sc->lmc_handle = timeout(ng_lmc_watchdog_frame, sc, hz);
473         sc->lmc_running = 1;
474
475         /*
476          * check if the physical link is up
477          */
478         if (sc->lmc_media->get_link_status(sc)) {
479                 sc->lmc_flags |= LMC_MODEMOK;
480                 lmc_led_on(sc, LMC_MII16_LED1);
481         }
482 }
483
484 /*
485  * Mark the interface as "down" and disable TX/RX and TX/RX interrupts.
486  * This is done by performing a full reset on the interface.
487  */
488 static void
489 lmc_ifdown(lmc_softc_t * const sc)
490 {
491         untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
492         sc->lmc_running = 0;
493         sc->lmc_flags &= ~LMC_IFUP;
494
495         sc->lmc_media->set_link_status(sc, 0);
496         lmc_led_off(sc, LMC_MII16_LED1);
497
498         lmc_dec_reset(sc);
499         lmc_reset(sc);
500         sc->lmc_media->set_status(sc, NULL);
501 }
502
503 static void
504 lmc_rx_intr(lmc_softc_t * const sc)
505 {
506         lmc_ringinfo_t * const ri = &sc->lmc_rxinfo;
507         int fillok = 1;
508
509         sc->lmc_rxtick++;
510
511         for (;;) {
512                 tulip_desc_t *eop = ri->ri_nextin;
513                 int total_len = 0, last_offset = 0;
514                 struct mbuf *ms = NULL, *me = NULL;
515                 int accept = 0;
516
517                 if (fillok && sc->lmc_rxq.ifq_len < LMC_RXQ_TARGET)
518                         goto queue_mbuf;
519
520                 /*
521                  * If the TULIP has no descriptors, there can't be any receive
522                  * descriptors to process.
523                  */
524                 if (eop == ri->ri_nextout)
525                         break;
526             
527                 /*
528                  * 90% of the packets will fit in one descriptor.  So we
529                  * optimize for that case.
530                  */
531                 if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
532                         _IF_DEQUEUE(&sc->lmc_rxq, ms);
533                         me = ms;
534                 } else {
535                         /*
536                          * If still owned by the TULIP, don't touch it.
537                          */
538                         if (((volatile tulip_desc_t *)eop)->d_status & TULIP_DSTS_OWNER)
539                                 break;
540
541                         /*
542                          * It is possible (though improbable unless the
543                          * BIG_PACKET support is enabled or MCLBYTES < 1518)
544                          * for a received packet to cross more than one
545                          * receive descriptor.
546                          */
547                         while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
548                                 if (++eop == ri->ri_last)
549                                         eop = ri->ri_first;
550                                 if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
551                                         return;
552                                 }
553                                 total_len++;
554                         }
555
556                         /*
557                          * Dequeue the first buffer for the start of the
558                          * packet.  Hopefully this will be the only one we
559                          * need to dequeue.  However, if the packet consumed
560                          * multiple descriptors, then we need to dequeue
561                          * those buffers and chain to the starting mbuf.
562                          * All buffers but the last buffer have the same
563                          * length so we can set that now. (we add to
564                          * last_offset instead of multiplying since we
565                          * normally won't go into the loop and thereby
566                          * saving ourselves from doing a multiplication
567                          * by 0 in the normal case).
568                          */
569                         _IF_DEQUEUE(&sc->lmc_rxq, ms);
570                         for (me = ms; total_len > 0; total_len--) {
571                                 me->m_len = LMC_RX_BUFLEN;
572                                 last_offset += LMC_RX_BUFLEN;
573                                 _IF_DEQUEUE(&sc->lmc_rxq, me->m_next);
574                                 me = me->m_next;
575                         }
576                 }
577
578                 /*
579                  *  Now get the size of received packet (minus the CRC).
580                  */
581                 total_len = ((eop->d_status >> 16) & 0x7FFF);
582                 if (sc->ictl.crc_length == 16)
583                         total_len -= 2;
584                 else
585                         total_len -= 4;
586
587                 sc->lmc_inbytes += total_len;
588                 sc->lmc_inlast = 0;
589
590                 if ((sc->lmc_flags & LMC_RXIGNORE) == 0
591                     && ((eop->d_status & LMC_DSTS_ERRSUM) == 0
592                         )) {
593                         me->m_len = total_len - last_offset;
594                         sc->lmc_flags |= LMC_RXACT;
595                         accept = 1;
596                 } else {
597                         sc->lmc_ierrors++;
598                         if (eop->d_status & TULIP_DSTS_RxOVERFLOW) {
599                                 sc->lmc_dot3stats.dot3StatsInternalMacReceiveErrors++;
600                         }
601                 }
602
603                 sc->lmc_ipackets++;
604                 if (++eop == ri->ri_last)
605                         eop = ri->ri_first;
606                 ri->ri_nextin = eop;
607
608         queue_mbuf:
609                 /*
610                  * Either we are priming the TULIP with mbufs (m == NULL)
611                  * or we are about to accept an mbuf for the upper layers
612                  * so we need to allocate an mbuf to replace it.  If we
613                  * can't replace it, send up it anyways.  This may cause
614                  * us to drop packets in the future but that's better than
615                  * being caught in livelock.
616                  *
617                  * Note that if this packet crossed multiple descriptors
618                  * we don't even try to reallocate all the mbufs here.
619                  * Instead we rely on the test of the beginning of
620                  * the loop to refill for the extra consumed mbufs.
621                  */
622                 if (accept || ms == NULL) {
623                         struct mbuf *m0;
624                         MGETHDR(m0, M_DONTWAIT, MT_DATA);
625                         if (m0 != NULL) {
626                                 MCLGET(m0, M_DONTWAIT);
627                                 if ((m0->m_flags & M_EXT) == 0) {
628                                         m_freem(m0);
629                                         m0 = NULL;
630                                 }
631                         }
632                         if (accept) {
633                                 int error;
634
635                                 ms->m_pkthdr.len = total_len;
636                                 ms->m_pkthdr.rcvif = NULL;
637                                 NG_SEND_DATA_ONLY(error, sc->lmc_hook, ms);
638                         }
639                         ms = m0;
640                 }
641                 if (ms == NULL) {
642                         /*
643                          * Couldn't allocate a new buffer.  Don't bother 
644                          * trying to replenish the receive queue.
645                          */
646                         fillok = 0;
647                         sc->lmc_flags |= LMC_RXBUFSLOW;
648                         continue;
649                 }
650                 /*
651                  * Now give the buffer(s) to the TULIP and save in our
652                  * receive queue.
653                  */
654                 do {
655                         ri->ri_nextout->d_length1 = LMC_RX_BUFLEN;
656                         ri->ri_nextout->d_addr1 = LMC_KVATOPHYS(sc, mtod(ms, caddr_t));
657                         ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
658                         if (++ri->ri_nextout == ri->ri_last)
659                                 ri->ri_nextout = ri->ri_first;
660                         me = ms->m_next;
661                         ms->m_next = NULL;
662                         _IF_ENQUEUE(&sc->lmc_rxq, ms);
663                 } while ((ms = me) != NULL);
664
665                 if (sc->lmc_rxq.ifq_len >= LMC_RXQ_TARGET)
666                         sc->lmc_flags &= ~LMC_RXBUFSLOW;
667         }
668 }
669 \f
670 static int
671 lmc_tx_intr(lmc_softc_t * const sc)
672 {
673     lmc_ringinfo_t * const ri = &sc->lmc_txinfo;
674     struct mbuf *m;
675     int xmits = 0;
676     int descs = 0;
677
678     sc->lmc_txtick++;
679
680     while (ri->ri_free < ri->ri_max) {
681         u_int32_t d_flag;
682         if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
683             break;
684
685         d_flag = ri->ri_nextin->d_flag;
686         if (d_flag & TULIP_DFLAG_TxLASTSEG) {
687                 const u_int32_t d_status = ri->ri_nextin->d_status;
688                 _IF_DEQUEUE(&sc->lmc_txq, m);
689                 if (m != NULL) {
690 #if NBPFILTER > 0
691                     if (sc->lmc_bpf != NULL)
692                         LMC_BPF_MTAP(sc, m);
693 #endif
694                     m_freem(m);
695 #if defined(LMC_DEBUG)
696                 } else {
697                     printf(LMC_PRINTF_FMT ": tx_intr: failed to dequeue mbuf?!?\n", LMC_PRINTF_ARGS);
698 #endif
699                 }
700                     xmits++;
701                     if (d_status & LMC_DSTS_ERRSUM) {
702                         sc->lmc_oerrors++;
703                         if (d_status & TULIP_DSTS_TxUNDERFLOW)
704                             sc->lmc_dot3stats.dot3StatsInternalTransmitUnderflows++;
705                     } else {
706                         if (d_status & TULIP_DSTS_TxDEFERRED)
707                             sc->lmc_dot3stats.dot3StatsDeferredTransmissions++;
708                     }
709         }
710
711         if (++ri->ri_nextin == ri->ri_last)
712             ri->ri_nextin = ri->ri_first;
713
714         ri->ri_free++;
715         descs++;
716         /*sc->lmc_if.if_flags &= ~IFF_OACTIVE;*/
717         sc->lmc_out_deficit++;
718     }
719     /*
720      * If nothing left to transmit, disable the timer.
721      * Else if progress, reset the timer back to 2 ticks.
722      */
723     sc->lmc_opackets += xmits;
724
725     return descs;
726 }
727 \f
728 static void
729 lmc_print_abnormal_interrupt (lmc_softc_t * const sc, u_int32_t csr)
730 {
731         printf(LMC_PRINTF_FMT ": Abnormal interrupt\n", LMC_PRINTF_ARGS);
732 }
733
734 static void
735 lmc_intr_handler(lmc_softc_t * const sc, int *progress_p)
736 {
737     u_int32_t csr;
738
739     while ((csr = LMC_CSR_READ(sc, csr_status)) & sc->lmc_intrmask) {
740
741         *progress_p = 1;
742         LMC_CSR_WRITE(sc, csr_status, csr);
743
744         if (csr & TULIP_STS_SYSERROR) {
745             sc->lmc_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
746             if (sc->lmc_flags & LMC_NOMESSAGES) {
747                 sc->lmc_flags |= LMC_SYSTEMERROR;
748             } else {
749                 printf(LMC_PRINTF_FMT ": system error: %s\n",
750                        LMC_PRINTF_ARGS,
751                        lmc_system_errors[sc->lmc_last_system_error]);
752             }
753             sc->lmc_flags |= LMC_NEEDRESET;
754             sc->lmc_system_errors++;
755             break;
756         }
757         if (csr & (TULIP_STS_RXINTR | TULIP_STS_RXNOBUF)) {
758             u_int32_t misses = LMC_CSR_READ(sc, csr_missed_frames);
759             if (csr & TULIP_STS_RXNOBUF)
760                 sc->lmc_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
761             /*
762              * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
763              * on receive overflows.
764              */
765            if ((misses & 0x0FFE0000) && (sc->lmc_features & LMC_HAVE_RXBADOVRFLW)) {
766                 sc->lmc_dot3stats.dot3StatsInternalMacReceiveErrors++;
767                 /*
768                  * Stop the receiver process and spin until it's stopped.
769                  * Tell rx_intr to drop the packets it dequeues.
770                  */
771                 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode & ~TULIP_CMD_RXRUN);
772                 while ((LMC_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
773                     ;
774                 LMC_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
775                 sc->lmc_flags |= LMC_RXIGNORE;
776             }
777             lmc_rx_intr(sc);
778             if (sc->lmc_flags & LMC_RXIGNORE) {
779                 /*
780                  * Restart the receiver.
781                  */
782                 sc->lmc_flags &= ~LMC_RXIGNORE;
783                 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
784             }
785         }
786         if (csr & TULIP_STS_ABNRMLINTR) {
787             u_int32_t tmp = csr & sc->lmc_intrmask
788                 & ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
789             if (csr & TULIP_STS_TXUNDERFLOW) {
790                 if ((sc->lmc_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
791                     sc->lmc_cmdmode += TULIP_CMD_THRSHLD96;
792                     sc->lmc_flags |= LMC_NEWTXTHRESH;
793                 } else if (sc->lmc_features & LMC_HAVE_STOREFWD) {
794                     sc->lmc_cmdmode |= TULIP_CMD_STOREFWD;
795                     sc->lmc_flags |= LMC_NEWTXTHRESH;
796                 }
797             }
798             if (sc->lmc_flags & LMC_NOMESSAGES) {
799                 sc->lmc_statusbits |= tmp;
800             } else {
801                 lmc_print_abnormal_interrupt(sc, tmp);
802                 sc->lmc_flags |= LMC_NOMESSAGES;
803             }
804             LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
805         }
806
807         if (csr & TULIP_STS_TXINTR)
808                 lmc_tx_intr(sc);
809
810         if (sc->lmc_flags & LMC_WANTTXSTART)
811                 lmc_ifstart(sc);
812     }
813 }
814
815 static lmc_intrfunc_t
816 lmc_intr_normal(void *arg)
817 {
818         lmc_softc_t * sc = (lmc_softc_t *) arg;
819         int progress = 0;
820
821         lmc_intr_handler(sc, &progress);
822
823 #if !defined(LMC_VOID_INTRFUNC)
824         return progress;
825 #endif
826 }
827 \f
828 static struct mbuf *
829 lmc_mbuf_compress(struct mbuf *m)
830 {
831         struct mbuf *m0;
832 #if MCLBYTES >= LMC_MTU + PPP_HEADER_LEN && !defined(BIG_PACKET)
833         MGETHDR(m0, M_DONTWAIT, MT_DATA);
834         if (m0 != NULL) {
835                 if (m->m_pkthdr.len > MHLEN) {
836                         MCLGET(m0, M_DONTWAIT);
837                         if ((m0->m_flags & M_EXT) == 0) {
838                                 m_freem(m);
839                                 m_freem(m0);
840                                 return NULL;
841                         }
842                 }
843                 m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
844                 m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
845         }
846 #else
847         int mlen = MHLEN;
848         int len = m->m_pkthdr.len;
849         struct mbuf **mp = &m0;
850
851         while (len > 0) {
852                 if (mlen == MHLEN) {
853                         MGETHDR(*mp, M_DONTWAIT, MT_DATA);
854                 } else {
855                         MGET(*mp, M_DONTWAIT, MT_DATA);
856                 }
857                 if (*mp == NULL) {
858                         m_freem(m0);
859                         m0 = NULL;
860                         break;
861                 }
862                 if (len > MLEN) {
863                         MCLGET(*mp, M_DONTWAIT);
864                         if (((*mp)->m_flags & M_EXT) == 0) {
865                                 m_freem(m0);
866                                 m0 = NULL;
867                                 break;
868                         }
869                         (*mp)->m_len = (len <= MCLBYTES ? len : MCLBYTES);
870                 } else {
871                         (*mp)->m_len = (len <= mlen ? len : mlen);
872                 }
873                 m_copydata(m, m->m_pkthdr.len - len,
874                            (*mp)->m_len, mtod((*mp), caddr_t));
875                 len -= (*mp)->m_len;
876                 mp = &(*mp)->m_next;
877                 mlen = MLEN;
878         }
879 #endif
880         m_freem(m);
881         return m0;
882 }
883 \f
884 /*
885  * queue the mbuf handed to us for the interface.  If we cannot
886  * queue it, return the mbuf.  Return NULL if the mbuf was queued.
887  */
888 static struct mbuf *
889 lmc_txput(lmc_softc_t * const sc, struct mbuf *m)
890 {
891         lmc_ringinfo_t * const ri = &sc->lmc_txinfo;
892         tulip_desc_t *eop, *nextout;
893         int segcnt, free;
894         u_int32_t d_status;
895         struct mbuf *m0;
896
897 #if defined(LMC_DEBUG)
898         if ((sc->lmc_cmdmode & TULIP_CMD_TXRUN) == 0) {
899                 printf(LMC_PRINTF_FMT ": txput: tx not running\n",
900                        LMC_PRINTF_ARGS);
901                 sc->lmc_flags |= LMC_WANTTXSTART;
902                 goto finish;
903         }
904 #endif
905
906         /*
907          * Now we try to fill in our transmit descriptors.  This is
908          * a bit reminiscent of going on the Ark two by two
909          * since each descriptor for the TULIP can describe
910          * two buffers.  So we advance through packet filling
911          * each of the two entries at a time to fill each
912          * descriptor.  Clear the first and last segment bits
913          * in each descriptor (actually just clear everything
914          * but the end-of-ring or chain bits) to make sure
915          * we don't get messed up by previously sent packets.
916          *
917          * We may fail to put the entire packet on the ring if
918          * there is either not enough ring entries free or if the
919          * packet has more than MAX_TXSEG segments.  In the former
920          * case we will just wait for the ring to empty.  In the
921          * latter case we have to recopy.
922          */
923  again:
924         d_status = 0;
925         eop = nextout = ri->ri_nextout;
926         m0 = m;
927         segcnt = 0;
928         free = ri->ri_free;
929         do {
930                 int len = m0->m_len;
931                 caddr_t addr = mtod(m0, caddr_t);
932                 unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
933
934                 while (len > 0) {
935                         unsigned slen = min(len, clsize);
936 #ifdef BIG_PACKET
937                         int partial = 0;
938                         if (slen >= 2048)
939                                 slen = 2040, partial = 1;
940 #endif
941                         segcnt++;
942                         if (segcnt > LMC_MAX_TXSEG) {
943                                 /*
944                                  * The packet exceeds the number of transmit
945                                  * buffer entries that we can use for one
946                                  * packet, so we have recopy it into one mbuf
947                                  * and then try again.
948                                  */
949                                 m = lmc_mbuf_compress(m);
950                                 if (m == NULL)
951                                         goto finish;
952                                 goto again;
953                         }
954                         if (segcnt & 1) {
955                                 if (--free == 0) {
956                                         /*
957                                          * See if there's any unclaimed space
958                                          * in the transmit ring.
959                                          */
960                                         if ((free += lmc_tx_intr(sc)) == 0) {
961                                                 /*
962                                                  * There's no more room but
963                                                  * since nothing has been
964                                                  * committed at this point,
965                                                  * just show output is active,
966                                                  * put back the mbuf and
967                                                  * return.
968                                                  */
969                                                 sc->lmc_flags |= LMC_WANTTXSTART;
970                                                 goto finish;
971                                         }
972                                 }
973                                 eop = nextout;
974                                 if (++nextout == ri->ri_last)
975                                         nextout = ri->ri_first;
976                                 eop->d_flag &= TULIP_DFLAG_ENDRING;
977                                 eop->d_flag |= TULIP_DFLAG_TxNOPADDING;
978                                 if (sc->ictl.crc_length == 16)
979                                         eop->d_flag |= TULIP_DFLAG_TxHASCRC;
980                                 eop->d_status = d_status;
981                                 eop->d_addr1 = LMC_KVATOPHYS(sc, addr);
982                                 eop->d_length1 = slen;
983                         } else {
984                                 /*
985                                  *  Fill in second half of descriptor
986                                  */
987                                 eop->d_addr2 = LMC_KVATOPHYS(sc, addr);
988                                 eop->d_length2 = slen;
989                         }
990                         d_status = TULIP_DSTS_OWNER;
991                         len -= slen;
992                         addr += slen;
993 #ifdef BIG_PACKET
994                         if (partial)
995                                 continue;
996 #endif
997                         clsize = CLBYTES;
998                 }
999         } while ((m0 = m0->m_next) != NULL);
1000
1001
1002         /*
1003          * The descriptors have been filled in.  Now get ready
1004          * to transmit.
1005          */
1006         _IF_ENQUEUE(&sc->lmc_txq, m);
1007         m = NULL;
1008
1009         /*
1010          * Make sure the next descriptor after this packet is owned
1011          * by us since it may have been set up above if we ran out
1012          * of room in the ring.
1013          */
1014         nextout->d_status = 0;
1015
1016         /*
1017          * If we only used the first segment of the last descriptor,
1018          * make sure the second segment will not be used.
1019          */
1020         if (segcnt & 1) {
1021                 eop->d_addr2 = 0;
1022                 eop->d_length2 = 0;
1023         }
1024
1025         /*
1026          * Mark the last and first segments, indicate we want a transmit
1027          * complete interrupt, and tell it to transmit!
1028          */
1029         eop->d_flag |= TULIP_DFLAG_TxLASTSEG | TULIP_DFLAG_TxWANTINTR;
1030
1031         /*
1032          * Note that ri->ri_nextout is still the start of the packet
1033          * and until we set the OWNER bit, we can still back out of
1034          * everything we have done.
1035          */
1036         ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
1037         ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
1038
1039         LMC_CSR_WRITE(sc, csr_txpoll, 1);
1040
1041         /*
1042          * This advances the ring for us.
1043          */
1044         ri->ri_nextout = nextout;
1045         ri->ri_free = free;
1046
1047         /*
1048          * switch back to the single queueing ifstart.
1049          */
1050         sc->lmc_flags &= ~LMC_WANTTXSTART;
1051         sc->lmc_xmit_busy = 0;
1052         sc->lmc_out_dog = 0;
1053
1054         /*
1055          * If we want a txstart, there must be not enough space in the
1056          * transmit ring.  So we want to enable transmit done interrupts
1057          * so we can immediately reclaim some space.  When the transmit
1058          * interrupt is posted, the interrupt handler will call tx_intr
1059          * to reclaim space and then txstart (since WANTTXSTART is set).
1060          * txstart will move the packet into the transmit ring and clear
1061          * WANTTXSTART thereby causing TXINTR to be cleared.
1062          */
1063  finish:
1064
1065         return m;
1066 }
1067 \f
1068
1069 /*
1070  * These routines gets called at device spl
1071  */
1072
1073 static ifnet_ret_t
1074 lmc_ifstart(lmc_softc_t * const sc)
1075 {
1076         struct mbuf *m;
1077
1078         if (sc->lmc_flags & LMC_IFUP) {
1079                 sc->lmc_xmit_busy = 1;
1080                 for(;;) {
1081                         struct ifqueue *q = &sc->lmc_xmitq_hipri;
1082                         IF_DEQUEUE(q, m);
1083                         if (m == NULL) {
1084                                 q = &sc->lmc_xmitq;
1085                                 IF_DEQUEUE(q, m);
1086                         }
1087                         if (m) {
1088                                 sc->lmc_outbytes = m->m_pkthdr.len;
1089                                 sc->lmc_opackets++;
1090                                 if ((m = lmc_txput(sc, m)) != NULL) {
1091                                         IF_PREPEND(q, m);
1092                                         printf(LMC_PRINTF_FMT
1093                                                ": lmc_txput failed\n",
1094                                                LMC_PRINTF_ARGS);
1095                                         break;
1096                                 }
1097                                 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1098                         }
1099                         else
1100                                 break;
1101                 }
1102         }
1103 }
1104
1105 static ifnet_ret_t
1106 lmc_ifstart_one(lmc_softc_t * const sc)
1107 {
1108         struct mbuf *m;
1109
1110         if ((sc->lmc_flags & LMC_IFUP)) {
1111                 struct ifqueue *q = &sc->lmc_xmitq_hipri;
1112                 IF_DEQUEUE(q, m);
1113                 if (m == NULL) {
1114                         q = &sc->lmc_xmitq;
1115                         IF_DEQUEUE(q, m);
1116                 }
1117                 if (m) {
1118                         sc->lmc_outbytes += m->m_pkthdr.len;
1119                         sc->lmc_opackets++;
1120                         if ((m = lmc_txput(sc, m)) != NULL) {
1121                                 IF_PREPEND(q, m);
1122                         }
1123                         LMC_CSR_WRITE(sc, csr_txpoll, 1);
1124                 }
1125         }
1126 }
1127
1128 /*
1129  * Set up the OS interface magic and attach to the operating system
1130  * network services.
1131  */
1132 static int
1133 lmc_attach(lmc_softc_t * const sc)
1134 {
1135         /*
1136          * we have found a node, make sure our 'type' is availabe.
1137          */
1138         if (ng_lmc_done_init == 0) ng_lmc_init(NULL);
1139         if (ng_make_node_common(&typestruct, &sc->lmc_node) != 0)
1140                 return (0);
1141         sprintf(sc->lmc_nodename, "%s%d", NG_LMC_NODE_TYPE, sc->lmc_unit);
1142         if (ng_name_node(sc->lmc_node, sc->lmc_nodename)) {
1143                 NG_NODE_UNREF(sc->lmc_node); /* make it go away again */
1144                 return (0);
1145         }
1146         NG_NODE_SET_PRIVATE(sc->lmc_node, sc);
1147         callout_handle_init(&sc->lmc_handle);
1148         sc->lmc_xmitq.ifq_maxlen = IFQ_MAXLEN;
1149         sc->lmc_xmitq_hipri.ifq_maxlen = IFQ_MAXLEN;
1150         mtx_init(&sc->lmc_xmitq.ifq_mtx, "lmc_xmitq", NULL, MTX_DEF);
1151         mtx_init(&sc->lmc_xmitq_hipri.ifq_mtx, "lmc_xmitq_hipri", NULL, MTX_DEF);
1152         sc->lmc_running = 0;
1153
1154         /*
1155          * turn off those LEDs...
1156          */
1157         sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1158         lmc_led_on(sc, LMC_MII16_LED0);
1159
1160         return 1;
1161 }
1162 \f
1163 static void
1164 lmc_initring(lmc_softc_t * const sc, lmc_ringinfo_t * const ri,
1165                tulip_desc_t *descs, int ndescs)
1166 {
1167         ri->ri_max = ndescs;
1168         ri->ri_first = descs;
1169         ri->ri_last = ri->ri_first + ri->ri_max;
1170         bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
1171         ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
1172 }
1173
1174 \f
1175
1176 #ifdef LMC_DEBUG
1177 static void
1178 ng_lmc_dump_packet(struct mbuf *m)
1179 {
1180         int i;
1181
1182         printf("mbuf: %d bytes, %s packet\n", m->m_len,
1183                (m->m_type == MT_DATA)?"data":"other");
1184
1185         for (i=0; i < m->m_len; i++) {
1186                 if( (i % 8) == 0 ) {
1187                         if( i ) printf("\n");
1188                         printf("\t");
1189                 }
1190                 else
1191                         printf(" ");
1192                 printf( "0x%02x", m->m_dat[i] );
1193         }
1194         printf("\n");
1195 }
1196 #endif /* LMC_DEBUG */
1197
1198 /* Device timeout/watchdog routine */
1199 static void
1200 ng_lmc_watchdog_frame(void *arg)
1201 {
1202         lmc_softc_t * sc = (lmc_softc_t *) arg;
1203         int s;
1204         int     speed;
1205
1206         if(sc->lmc_running == 0)
1207                 return; /* if we are not running let timeouts die */
1208         /* 
1209          * calculate the apparent throughputs
1210          *  XXX a real hack
1211          */
1212         s = splimp();
1213         speed = sc->lmc_inbytes - sc->lmc_lastinbytes;
1214         sc->lmc_lastinbytes = sc->lmc_inbytes;
1215         if ( sc->lmc_inrate < speed )
1216                 sc->lmc_inrate = speed;
1217         speed = sc->lmc_outbytes - sc->lmc_lastoutbytes;
1218         sc->lmc_lastoutbytes = sc->lmc_outbytes;
1219         if ( sc->lmc_outrate < speed )
1220                 sc->lmc_outrate = speed;
1221         sc->lmc_inlast++;
1222         splx(s);
1223
1224         if ((sc->lmc_inlast > LMC_QUITE_A_WHILE)
1225         && (sc->lmc_out_deficit > LMC_LOTS_OF_PACKETS)) {
1226                 log(LOG_ERR, "%s%d: No response from remote end\n",
1227                     sc->lmc_name, sc->lmc_unit);
1228                 s = splimp();
1229                 lmc_ifdown(sc);
1230                 lmc_ifup(sc);
1231                 sc->lmc_inlast = sc->lmc_out_deficit = 0;
1232                 splx(s);
1233         } else if (sc->lmc_xmit_busy) {
1234                 if (sc->lmc_out_dog == 0) {
1235                         log(LOG_ERR, "ar%d: Transmit failure.. no clock?\n",
1236                                         sc->lmc_unit);
1237                         s = splimp();
1238                         lmc_watchdog(sc);
1239 #if 0                 
1240                         lmc_ifdown(sc);
1241                         lmc_ifup(sc);
1242 #endif
1243                         splx(s);
1244                         sc->lmc_inlast = sc->lmc_out_deficit = 0;
1245                 } else {
1246                         sc->lmc_out_dog--;
1247                 }
1248         }
1249         lmc_watchdog(sc);
1250         sc->lmc_handle = timeout(ng_lmc_watchdog_frame, sc, hz);
1251 }
1252
1253 /***********************************************************************
1254  * This section contains the methods for the Netgraph interface
1255  ***********************************************************************/
1256 /*
1257  * It is not possible or allowable to create a node of this type.
1258  * If the hardware exists, it will already have created it.
1259  */
1260 static  int
1261 ng_lmc_constructor(node_p node)
1262 {
1263         return (EINVAL);
1264 }
1265
1266 /*
1267  * give our ok for a hook to be added...
1268  * If we are not running this should kick the device into life.
1269  * We allow hooks called "control", "rawdata", and "debug".
1270  * The hook's private info points to our stash of info about that
1271  * device.
1272  */
1273 static  int
1274 ng_lmc_newhook(node_p node, hook_p hook, const char *name)
1275 {
1276         lmc_softc_t *       sc = NG_NODE_PRIVATE(node);
1277
1278         /*
1279          * check if it's our friend the debug hook
1280          */
1281         if (strcmp(name, NG_LMC_HOOK_DEBUG) == 0) {
1282                 NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */
1283                 sc->lmc_debug_hook = hook;
1284                 return (0);
1285         }
1286
1287         /*
1288          * Check for raw mode hook.
1289          */
1290         if (strcmp(name, NG_LMC_HOOK_RAW) != 0) {
1291                 return (EINVAL);
1292         }
1293         NG_HOOK_SET_PRIVATE(hook, sc);
1294         sc->lmc_hook = hook;
1295         sc->lmc_datahooks++;
1296         lmc_ifup(sc);
1297         return (0);
1298 }
1299
1300 /*
1301  * incoming messages.
1302  * Just respond to the generic TEXT_STATUS message
1303  */
1304 static  int
1305 ng_lmc_rcvmsg(node_p node, item_p item, hook_p lasthook)
1306 {
1307         lmc_softc_t *sc = NG_NODE_PRIVATE(node);
1308         struct ng_mesg *resp = NULL;
1309         int error = 0;
1310         struct ng_mesg *msg;
1311
1312         NGI_GET_MSG(item, msg);
1313         switch (msg->header.typecookie) {
1314         case NG_LMC_COOKIE:
1315                 switch (msg->header.cmd) {
1316                 case NGM_LMC_GET_CTL:
1317                     {
1318                         lmc_ctl_t *ctl;
1319
1320                         NG_MKRESPONSE(resp, msg, sizeof(*ctl), M_NOWAIT);
1321                         if (!resp) {
1322                                 error = ENOMEM;
1323                                 break;
1324                         }
1325                         ctl = (lmc_ctl_t *) resp->data;
1326                         memcpy( ctl, &sc->ictl, sizeof(*ctl) );
1327                         break;
1328                     }
1329                 case NGM_LMC_SET_CTL:
1330                     {
1331                         lmc_ctl_t *ctl;
1332
1333                         if (msg->header.arglen != sizeof(*ctl)) {
1334                                 error = EINVAL;
1335                                 break;
1336                         }
1337
1338                         ctl = (lmc_ctl_t *) msg->data;
1339                         sc->lmc_media->set_status(sc, ctl);
1340                         break;
1341                     }
1342                 default:
1343                         error = EINVAL;         /* unknown command */
1344                         break;
1345                 }
1346                 break;
1347         case NGM_GENERIC_COOKIE:
1348                 switch(msg->header.cmd) {
1349                 case NGM_TEXT_STATUS: {
1350                         char        *arg;
1351                         int pos = 0;
1352
1353                         int resplen = sizeof(struct ng_mesg) + 512;
1354                         NG_MKRESPONSE(resp, msg, resplen, M_NOWAIT);
1355                         if (resp == NULL) {
1356                                 error = ENOMEM;
1357                                 break;
1358                         }
1359                         arg = (resp)->data;
1360
1361                         /*
1362                          * Put in the throughput information.
1363                          */
1364                         pos = sprintf(arg, "%ld bytes in, %ld bytes out\n"
1365                                       "highest rate seen: %ld B/S in, "
1366                                       "%ld B/S out\n",
1367                                       sc->lmc_inbytes, sc->lmc_outbytes,
1368                                       sc->lmc_inrate, sc->lmc_outrate);
1369                         pos += sprintf(arg + pos, "%ld output errors\n",
1370                                        sc->lmc_oerrors);
1371                         pos += sprintf(arg + pos, "%ld input errors\n",
1372                                        sc->lmc_ierrors);
1373
1374                         resp->header.arglen = pos + 1;
1375                         break;
1376                       }
1377                 default:
1378                         error = EINVAL;
1379                         break;
1380                 }
1381                 break;
1382         default:
1383                 error = EINVAL;
1384                 break;
1385         }
1386
1387         /* Take care of synchronous response, if any */
1388         NG_RESPOND_MSG(error, node, item, resp);
1389         NG_FREE_MSG(msg);
1390         return (error);
1391 }
1392
1393 /*
1394  * get data from another node and transmit it to the line
1395  */
1396 static  int
1397 ng_lmc_rcvdata(hook_p hook, item_p item)
1398 {
1399         int s;
1400         int error = 0;
1401         lmc_softc_t * sc = (lmc_softc_t *) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1402         struct ifqueue  *xmitq_p;
1403         struct mbuf *m;
1404         meta_p meta;
1405
1406         /* Unpack all the data components */
1407         NGI_GET_M(item, m);
1408         NGI_GET_META(item, meta);
1409         NG_FREE_ITEM(item);
1410
1411         /*
1412          * data doesn't come in from just anywhere (e.g control hook)
1413          */
1414         if ( NG_HOOK_PRIVATE(hook) == NULL) {
1415                 error = ENETDOWN;
1416                 goto bad;
1417         }
1418
1419         /*
1420          * Now queue the data for when it can be sent
1421          */
1422         if (meta && meta->priority > 0) {
1423                 xmitq_p = (&sc->lmc_xmitq_hipri);
1424         } else {
1425                 xmitq_p = (&sc->lmc_xmitq);
1426         }
1427         s = splimp();
1428         IF_LOCK(xmitq_p);
1429         if (_IF_QFULL(xmitq_p)) {
1430                 _IF_DROP(xmitq_p);
1431                 IF_UNLOCK(xmitq_p);
1432                 splx(s);
1433                 error = ENOBUFS;
1434                 goto bad;
1435         }
1436         _IF_ENQUEUE(xmitq_p, m);
1437         IF_UNLOCK(xmitq_p);
1438         lmc_ifstart_one(sc);
1439         splx(s);
1440         return (0);
1441
1442 bad:
1443         /*
1444          * It was an error case.
1445          * check if we need to free the mbuf, and then return the error
1446          */
1447         NG_FREE_M(m);
1448         NG_FREE_META(meta);
1449         return (error);
1450 }
1451
1452 /*
1453  * do local shutdown processing..
1454  * this node will refuse to go away, unless the hardware says to..
1455  * don't unref the node, or remove our name. just clear our links up.
1456  */
1457 static  int
1458 ng_lmc_rmnode(node_p node)
1459 {
1460         lmc_softc_t * sc = NG_NODE_PRIVATE(node);
1461
1462         lmc_ifdown(sc);
1463         
1464         /*
1465          * Get rid of the old node.
1466          */
1467         NG_NODE_SET_PRIVATE(node, NULL);
1468         NG_NODE_UNREF(node);
1469         
1470         /*
1471          * Create a new node. This is basically what a device
1472          * driver would do in the attach routine. So let's just do that..
1473          * The node is dead, long live the node!
1474          */
1475         if (ng_make_node_common(&typestruct, &sc->lmc_node) != 0)
1476                 return (0);
1477         sprintf(sc->lmc_nodename, "%s%d", NG_LMC_NODE_TYPE, sc->lmc_unit);
1478         if (ng_name_node(sc->lmc_node, sc->lmc_nodename)) {
1479                 sc->lmc_node = NULL; /* to be sure */
1480                 NG_NODE_UNREF(sc->lmc_node); /* make it go away */
1481                 return (0);
1482         }
1483         NG_NODE_SET_PRIVATE(sc->lmc_node, sc);
1484         callout_handle_init(&sc->lmc_handle);
1485         sc->lmc_running = 0;
1486         /*
1487          * turn off those LEDs...
1488          */
1489         sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1490         lmc_led_on(sc, LMC_MII16_LED0);
1491         return (0);
1492 }
1493 /* already linked */
1494 static  int
1495 ng_lmc_connect(hook_p hook)
1496 {
1497         /* We are probably not at splnet.. force outward queueing */
1498         NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
1499         /* be really amiable and just say "YUP that's OK by me! " */
1500         return (0);
1501 }
1502
1503 /*
1504  * notify on hook disconnection (destruction)
1505  *
1506  * For this type, removal of the last link resets tries to destroy the node.
1507  * As the device still exists, the shutdown method will not actually
1508  * destroy the node, but reset the device and leave it 'fresh' :)
1509  *
1510  * The node removal code will remove all references except that owned by the
1511  * driver.
1512  */
1513 static  int
1514 ng_lmc_disconnect(hook_p hook)
1515 {
1516         lmc_softc_t * sc = (lmc_softc_t *) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1517         int     s;
1518         /*
1519          * If it's the data hook, then free resources etc.
1520          */
1521         if (NG_HOOK_PRIVATE(hook)) {
1522                 s = splimp();
1523                 sc->lmc_datahooks--;
1524                 if (sc->lmc_datahooks == 0)
1525                         lmc_ifdown(sc);
1526                 splx(s);
1527         } else {
1528                 sc->lmc_debug_hook = NULL;
1529         }
1530         return (0);
1531 }
1532
1533 /*
1534  * called during bootup
1535  * or LKM loading to put this type into the list of known modules
1536  */
1537 static void
1538 ng_lmc_init(void *ignored)
1539 {
1540         if (ng_newtype(&typestruct))
1541                 printf("ng_lmc install failed\n");
1542         ng_lmc_done_init = 1;
1543 }
1544 \f
1545 /*
1546  * This is the PCI configuration support.
1547  */
1548 #define PCI_CFID        0x00    /* Configuration ID */
1549 #define PCI_CFCS        0x04    /* Configurtion Command/Status */
1550 #define PCI_CFRV        0x08    /* Configuration Revision */
1551 #define PCI_CFLT        0x0c    /* Configuration Latency Timer */
1552 #define PCI_CBIO        0x10    /* Configuration Base IO Address */
1553 #define PCI_CBMA        0x14    /* Configuration Base Memory Address */
1554 #define PCI_SSID        0x2c    /* subsystem config register */
1555 #define PCI_CFIT        0x3c    /* Configuration Interrupt */
1556 #define PCI_CFDA        0x40    /* Configuration Driver Area */
1557
1558 \f
1559
1560 #include "dev/lmc/if_lmc_fbsd3.c"
1561
1562 #endif