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