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)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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 $
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #warning "The lmc driver is broken and is not compiled with LINT"
37 char lmc_version[] = "BSD 1.1";
39 #include "opt_netgraph.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
49 #include <sys/syslog.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/ng_parse.h>
55 #include <netgraph/netgraph.h>
58 #include <dev/pci/pcivar.h>
59 #include <pci/dc21040reg.h>
60 #define INCLUDE_PATH_PREFIX "dev/lmc/"
62 /* Intel CPUs should use I/O mapped access. */
68 * This turns on all sort of debugging stuff and make the
72 #define DP(x) printf x
79 #ifndef TULIP_GP_PINSET
80 #define TULIP_GP_PINSET 0x00000100L
82 #ifndef TULIP_BUSMODE_READMULTIPLE
83 #define TULIP_BUSMODE_READMULTIPLE 0x00200000L
89 typedef struct lmc___softc lmc_softc_t;
90 typedef struct lmc___media lmc_media_t;
91 typedef struct lmc___ctl lmc_ctl_t;
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"
99 * This module supports
100 * the DEC 21140A pass 2.2 PCI Fast Ethernet Controller.
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);
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);
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);
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;
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,
134 static const struct ng_parse_type ng_lmc_ctl_cardspec_type = {
135 &ng_parse_fixedarray_type,
136 &ng_lmc_ctl_cardspec_info
139 static const struct ng_parse_struct_info ng_lmc_ctl_type_info = {
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 },
156 static const struct ng_parse_type ng_lmc_ctl_type = {
157 &ng_parse_struct_type,
158 &ng_lmc_ctl_type_info
162 /* List of commands and how to convert arguments to/from ASCII */
163 static const struct ng_cmdlist ng_lmc_cmdlist[] = {
181 static struct ng_type typestruct = {
196 static int ng_lmc_done_init = 0;
200 * Code the read the SROM and MII bit streams (I2C)
203 lmc_delay_300ns(lmc_softc_t * const sc)
206 for (idx = (300 / 33) + 1; idx > 0; idx--)
207 (void)LMC_CSR_READ(sc, csr_busmode);
213 LMC_CSR_WRITE(sc, csr_srom_mii, csr); \
214 lmc_delay_300ns(sc); \
218 lmc_srom_idle(lmc_softc_t * const sc)
222 csr = SROMSEL ; EMIT;
223 csr = SROMSEL | SROMRD; EMIT;
225 csr ^= SROMCLKON; EMIT;
228 * Write 25 cycles of 0 which will force the SROM to be idle.
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 */
234 csr ^= SROMCLKOFF; EMIT;
241 lmc_srom_read(lmc_softc_t * const sc)
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;
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;
259 for (bits = idx|cmdmask, bit = bitwidth + 3
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 */
269 csr ^= SROMCLKON; EMIT; /* clock H data valid */
272 csr ^= SROMCLKOFF; EMIT;
274 for (data = 0, bits = 0; bits < 16; bits++) {
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 */
280 sc->lmc_rombuf[idx*2] = data & 0xFF;
281 sc->lmc_rombuf[idx*2+1] = data >> 8;
282 csr = SROMSEL | SROMRD; EMIT;
288 #define MII_EMIT do { LMC_CSR_WRITE(sc, csr_srom_mii, csr); lmc_delay_300ns(sc); } while (0)
291 lmc_mii_writebits(lmc_softc_t * const sc, unsigned data, unsigned bits)
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;
297 csr |= MII_WR; MII_EMIT; /* clock low; assert write */
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 */
304 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
306 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
311 lmc_mii_turnaround(lmc_softc_t * const sc, unsigned cmd)
313 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
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 */
321 csr |= MII_RD; MII_EMIT; /* clock low; switch to read */
323 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
324 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
328 lmc_mii_readbits(lmc_softc_t * const sc)
331 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
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)
339 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
341 csr ^= MII_RD; MII_EMIT; /* clock low; turn off read */
347 lmc_mii_readreg(lmc_softc_t * const sc, unsigned devaddr, unsigned regno)
349 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
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);
359 data = lmc_mii_readbits(sc);
364 lmc_mii_writereg(lmc_softc_t * const sc, unsigned devaddr,
365 unsigned regno, unsigned data)
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);
378 lmc_read_macaddr(lmc_softc_t * const sc)
382 bcopy(sc->lmc_rombuf + 20, sc->lmc_enaddr, 6);
388 * Check to make certain there is a signal from the modem, and flicker
392 lmc_watchdog(lmc_softc_t * const sc)
396 u_int32_t link_status;
401 link_status = sc->lmc_media->get_link_status(sc);
402 ostatus = ((sc->lmc_flags & LMC_MODEMOK) == LMC_MODEMOK);
405 * hardware level link lost, but the interface is marked as up.
408 if (link_status == 0 && ostatus) {
409 printf(LMC_PRINTF_FMT ": physical link down\n",
411 sc->lmc_flags &= ~LMC_MODEMOK;
412 lmc_led_off(sc, LMC_MII16_LED1);
416 * hardware link is up, but the interface is marked as down.
417 * Bring it back up again.
419 if (link_status != 0 && !ostatus) {
420 printf(LMC_PRINTF_FMT ": physical link up\n",
422 if (sc->lmc_flags & LMC_IFUP)
428 * remember the timer value
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);
434 sc->lmc_out_dog = LMC_DOG_HOLDOFF;
438 * Mark the interface as "up" and enable TX/RX and TX/RX interrupts.
439 * This also does a full software reset.
442 lmc_ifup(lmc_softc_t * const sc)
444 untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
450 sc->lmc_media->set_link_status(sc, 1);
451 sc->lmc_media->set_status(sc, NULL);
453 sc->lmc_flags |= LMC_IFUP;
456 * select what interrupts we want to get
458 sc->lmc_intrmask |= (TULIP_STS_NORMALINTR
461 | TULIP_STS_ABNRMLINTR
463 | TULIP_STS_TXSTOPPED
464 | TULIP_STS_TXUNDERFLOW
465 | TULIP_STS_RXSTOPPED
467 LMC_CSR_WRITE(sc, csr_intr, sc->lmc_intrmask);
469 sc->lmc_cmdmode |= TULIP_CMD_TXRUN;
470 sc->lmc_cmdmode |= TULIP_CMD_RXRUN;
471 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
473 untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
474 sc->lmc_handle = timeout(ng_lmc_watchdog_frame, sc, hz);
478 * check if the physical link is up
480 if (sc->lmc_media->get_link_status(sc)) {
481 sc->lmc_flags |= LMC_MODEMOK;
482 lmc_led_on(sc, LMC_MII16_LED1);
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.
491 lmc_ifdown(lmc_softc_t * const sc)
493 untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
495 sc->lmc_flags &= ~LMC_IFUP;
497 sc->lmc_media->set_link_status(sc, 0);
498 lmc_led_off(sc, LMC_MII16_LED1);
502 sc->lmc_media->set_status(sc, NULL);
506 lmc_rx_intr(lmc_softc_t * const sc)
508 lmc_ringinfo_t * const ri = &sc->lmc_rxinfo;
514 tulip_desc_t *eop = ri->ri_nextin;
515 int total_len = 0, last_offset = 0;
516 struct mbuf *ms = NULL, *me = NULL;
519 if (fillok && sc->lmc_rxq.ifq_len < LMC_RXQ_TARGET)
523 * If the TULIP has no descriptors, there can't be any receive
524 * descriptors to process.
526 if (eop == ri->ri_nextout)
530 * 90% of the packets will fit in one descriptor. So we
531 * optimize for that case.
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);
538 * If still owned by the TULIP, don't touch it.
540 if (((volatile tulip_desc_t *)eop)->d_status & TULIP_DSTS_OWNER)
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.
549 while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
550 if (++eop == ri->ri_last)
552 if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
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).
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);
581 * Now get the size of received packet (minus the CRC).
583 total_len = ((eop->d_status >> 16) & 0x7FFF);
584 if (sc->ictl.crc_length == 16)
589 sc->lmc_inbytes += total_len;
592 if ((sc->lmc_flags & LMC_RXIGNORE) == 0
593 && ((eop->d_status & LMC_DSTS_ERRSUM) == 0
595 me->m_len = total_len - last_offset;
596 sc->lmc_flags |= LMC_RXACT;
600 if (eop->d_status & TULIP_DSTS_RxOVERFLOW) {
601 sc->lmc_dot3stats.dot3StatsInternalMacReceiveErrors++;
606 if (++eop == ri->ri_last)
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.
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.
624 if (accept || ms == NULL) {
626 MGETHDR(m0, M_DONTWAIT, MT_DATA);
628 MCLGET(m0, M_DONTWAIT);
629 if ((m0->m_flags & M_EXT) == 0) {
637 ms->m_pkthdr.len = total_len;
638 ms->m_pkthdr.rcvif = NULL;
639 NG_SEND_DATA_ONLY(error, sc->lmc_hook, ms);
645 * Couldn't allocate a new buffer. Don't bother
646 * trying to replenish the receive queue.
649 sc->lmc_flags |= LMC_RXBUFSLOW;
653 * Now give the buffer(s) to the TULIP and save in our
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;
664 _IF_ENQUEUE(&sc->lmc_rxq, ms);
665 } while ((ms = me) != NULL);
667 if (sc->lmc_rxq.ifq_len >= LMC_RXQ_TARGET)
668 sc->lmc_flags &= ~LMC_RXBUFSLOW;
673 lmc_tx_intr(lmc_softc_t * const sc)
675 lmc_ringinfo_t * const ri = &sc->lmc_txinfo;
682 while (ri->ri_free < ri->ri_max) {
684 if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
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);
693 if (sc->lmc_bpf != NULL)
697 #if defined(LMC_DEBUG)
699 printf(LMC_PRINTF_FMT ": tx_intr: failed to dequeue mbuf?!?\n", LMC_PRINTF_ARGS);
703 if (d_status & LMC_DSTS_ERRSUM) {
705 if (d_status & TULIP_DSTS_TxUNDERFLOW)
706 sc->lmc_dot3stats.dot3StatsInternalTransmitUnderflows++;
708 if (d_status & TULIP_DSTS_TxDEFERRED)
709 sc->lmc_dot3stats.dot3StatsDeferredTransmissions++;
713 if (++ri->ri_nextin == ri->ri_last)
714 ri->ri_nextin = ri->ri_first;
718 /*sc->lmc_if.if_flags &= ~IFF_OACTIVE;*/
719 sc->lmc_out_deficit++;
722 * If nothing left to transmit, disable the timer.
723 * Else if progress, reset the timer back to 2 ticks.
725 sc->lmc_opackets += xmits;
731 lmc_print_abnormal_interrupt (lmc_softc_t * const sc, u_int32_t csr)
733 printf(LMC_PRINTF_FMT ": Abnormal interrupt\n", LMC_PRINTF_ARGS);
737 lmc_intr_handler(lmc_softc_t * const sc, int *progress_p)
741 while ((csr = LMC_CSR_READ(sc, csr_status)) & sc->lmc_intrmask) {
744 LMC_CSR_WRITE(sc, csr_status, csr);
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;
751 printf(LMC_PRINTF_FMT ": system error: %s\n",
753 lmc_system_errors[sc->lmc_last_system_error]);
755 sc->lmc_flags |= LMC_NEEDRESET;
756 sc->lmc_system_errors++;
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;
764 * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
765 * on receive overflows.
767 if ((misses & 0x0FFE0000) && (sc->lmc_features & LMC_HAVE_RXBADOVRFLW)) {
768 sc->lmc_dot3stats.dot3StatsInternalMacReceiveErrors++;
770 * Stop the receiver process and spin until it's stopped.
771 * Tell rx_intr to drop the packets it dequeues.
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)
776 LMC_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
777 sc->lmc_flags |= LMC_RXIGNORE;
780 if (sc->lmc_flags & LMC_RXIGNORE) {
782 * Restart the receiver.
784 sc->lmc_flags &= ~LMC_RXIGNORE;
785 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
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;
800 if (sc->lmc_flags & LMC_NOMESSAGES) {
801 sc->lmc_statusbits |= tmp;
803 lmc_print_abnormal_interrupt(sc, tmp);
804 sc->lmc_flags |= LMC_NOMESSAGES;
806 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
809 if (csr & TULIP_STS_TXINTR)
812 if (sc->lmc_flags & LMC_WANTTXSTART)
817 static lmc_intrfunc_t
818 lmc_intr_normal(void *arg)
820 lmc_softc_t * sc = (lmc_softc_t *) arg;
823 lmc_intr_handler(sc, &progress);
825 #if !defined(LMC_VOID_INTRFUNC)
831 lmc_mbuf_compress(struct mbuf *m)
834 #if MCLBYTES >= LMC_MTU + PPP_HEADER_LEN && !defined(BIG_PACKET)
835 MGETHDR(m0, M_DONTWAIT, MT_DATA);
837 if (m->m_pkthdr.len > MHLEN) {
838 MCLGET(m0, M_DONTWAIT);
839 if ((m0->m_flags & M_EXT) == 0) {
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;
850 int len = m->m_pkthdr.len;
851 struct mbuf **mp = &m0;
855 MGETHDR(*mp, M_DONTWAIT, MT_DATA);
857 MGET(*mp, M_DONTWAIT, MT_DATA);
865 MCLGET(*mp, M_DONTWAIT);
866 if (((*mp)->m_flags & M_EXT) == 0) {
871 (*mp)->m_len = (len <= MCLBYTES ? len : MCLBYTES);
873 (*mp)->m_len = (len <= mlen ? len : mlen);
875 m_copydata(m, m->m_pkthdr.len - len,
876 (*mp)->m_len, mtod((*mp), caddr_t));
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.
891 lmc_txput(lmc_softc_t * const sc, struct mbuf *m)
893 lmc_ringinfo_t * const ri = &sc->lmc_txinfo;
894 tulip_desc_t *eop, *nextout;
899 #if defined(LMC_DEBUG)
900 if ((sc->lmc_cmdmode & TULIP_CMD_TXRUN) == 0) {
901 printf(LMC_PRINTF_FMT ": txput: tx not running\n",
903 sc->lmc_flags |= LMC_WANTTXSTART;
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.
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.
927 eop = nextout = ri->ri_nextout;
933 caddr_t addr = mtod(m0, caddr_t);
934 unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
937 unsigned slen = min(len, clsize);
941 slen = 2040, partial = 1;
944 if (segcnt > LMC_MAX_TXSEG) {
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.
951 m = lmc_mbuf_compress(m);
959 * See if there's any unclaimed space
960 * in the transmit ring.
962 if ((free += lmc_tx_intr(sc)) == 0) {
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
971 sc->lmc_flags |= LMC_WANTTXSTART;
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;
987 * Fill in second half of descriptor
989 eop->d_addr2 = LMC_KVATOPHYS(sc, addr);
990 eop->d_length2 = slen;
992 d_status = TULIP_DSTS_OWNER;
1001 } while ((m0 = m0->m_next) != NULL);
1005 * The descriptors have been filled in. Now get ready
1008 _IF_ENQUEUE(&sc->lmc_txq, m);
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.
1016 nextout->d_status = 0;
1019 * If we only used the first segment of the last descriptor,
1020 * make sure the second segment will not be used.
1028 * Mark the last and first segments, indicate we want a transmit
1029 * complete interrupt, and tell it to transmit!
1031 eop->d_flag |= TULIP_DFLAG_TxLASTSEG | TULIP_DFLAG_TxWANTINTR;
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.
1038 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
1039 ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
1041 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1044 * This advances the ring for us.
1046 ri->ri_nextout = nextout;
1050 * switch back to the single queueing ifstart.
1052 sc->lmc_flags &= ~LMC_WANTTXSTART;
1053 sc->lmc_xmit_busy = 0;
1054 sc->lmc_out_dog = 0;
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.
1072 * These routines gets called at device spl
1076 lmc_ifstart(lmc_softc_t * const sc)
1080 if (sc->lmc_flags & LMC_IFUP) {
1081 sc->lmc_xmit_busy = 1;
1083 struct ifqueue *q = &sc->lmc_xmitq_hipri;
1090 sc->lmc_outbytes = m->m_pkthdr.len;
1092 if ((m = lmc_txput(sc, m)) != NULL) {
1094 printf(LMC_PRINTF_FMT
1095 ": lmc_txput failed\n",
1099 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1108 lmc_ifstart_one(lmc_softc_t * const sc)
1112 if ((sc->lmc_flags & LMC_IFUP)) {
1113 struct ifqueue *q = &sc->lmc_xmitq_hipri;
1120 sc->lmc_outbytes += m->m_pkthdr.len;
1122 if ((m = lmc_txput(sc, m)) != NULL) {
1125 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1131 * Set up the OS interface magic and attach to the operating system
1135 lmc_attach(lmc_softc_t * const sc)
1138 * we have found a node, make sure our 'type' is availabe.
1140 if (ng_lmc_done_init == 0) ng_lmc_init(NULL);
1141 if (ng_make_node_common(&typestruct, &sc->lmc_node) != 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 */
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;
1157 * turn off those LEDs...
1159 sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1160 lmc_led_on(sc, LMC_MII16_LED0);
1166 lmc_initring(lmc_softc_t * const sc, lmc_ringinfo_t * const ri,
1167 tulip_desc_t *descs, int ndescs)
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;
1180 ng_lmc_dump_packet(struct mbuf *m)
1184 printf("mbuf: %d bytes, %s packet\n", m->m_len,
1185 (m->m_type == MT_DATA)?"data":"other");
1187 for (i=0; i < m->m_len; i++) {
1188 if( (i % 8) == 0 ) {
1189 if( i ) printf("\n");
1194 printf( "0x%02x", m->m_dat[i] );
1198 #endif /* LMC_DEBUG */
1200 /* Device timeout/watchdog routine */
1202 ng_lmc_watchdog_frame(void *arg)
1204 lmc_softc_t * sc = (lmc_softc_t *) arg;
1208 if(sc->lmc_running == 0)
1209 return; /* if we are not running let timeouts die */
1211 * calculate the apparent throughputs
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;
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);
1233 sc->lmc_inlast = sc->lmc_out_deficit = 0;
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",
1246 sc->lmc_inlast = sc->lmc_out_deficit = 0;
1252 sc->lmc_handle = timeout(ng_lmc_watchdog_frame, sc, hz);
1255 /***********************************************************************
1256 * This section contains the methods for the Netgraph interface
1257 ***********************************************************************/
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.
1263 ng_lmc_constructor(node_p node)
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
1276 ng_lmc_newhook(node_p node, hook_p hook, const char *name)
1278 lmc_softc_t * sc = NG_NODE_PRIVATE(node);
1281 * check if it's our friend the debug hook
1283 if (strcmp(name, NG_LMC_HOOK_DEBUG) == 0) {
1284 NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */
1285 sc->lmc_debug_hook = hook;
1290 * Check for raw mode hook.
1292 if (strcmp(name, NG_LMC_HOOK_RAW) != 0) {
1295 NG_HOOK_SET_PRIVATE(hook, sc);
1296 sc->lmc_hook = hook;
1297 sc->lmc_datahooks++;
1303 * incoming messages.
1304 * Just respond to the generic TEXT_STATUS message
1307 ng_lmc_rcvmsg(node_p node, item_p item, hook_p lasthook)
1309 lmc_softc_t *sc = NG_NODE_PRIVATE(node);
1310 struct ng_mesg *resp = NULL;
1312 struct ng_mesg *msg;
1314 NGI_GET_MSG(item, msg);
1315 switch (msg->header.typecookie) {
1317 switch (msg->header.cmd) {
1318 case NGM_LMC_GET_CTL:
1322 NG_MKRESPONSE(resp, msg, sizeof(*ctl), M_NOWAIT);
1327 ctl = (lmc_ctl_t *) resp->data;
1328 memcpy( ctl, &sc->ictl, sizeof(*ctl) );
1331 case NGM_LMC_SET_CTL:
1335 if (msg->header.arglen != sizeof(*ctl)) {
1340 ctl = (lmc_ctl_t *) msg->data;
1341 sc->lmc_media->set_status(sc, ctl);
1345 error = EINVAL; /* unknown command */
1349 case NGM_GENERIC_COOKIE:
1350 switch(msg->header.cmd) {
1351 case NGM_TEXT_STATUS: {
1355 int resplen = sizeof(struct ng_mesg) + 512;
1356 NG_MKRESPONSE(resp, msg, resplen, M_NOWAIT);
1364 * Put in the throughput information.
1366 pos = sprintf(arg, "%ld bytes in, %ld bytes out\n"
1367 "highest rate seen: %ld B/S in, "
1369 sc->lmc_inbytes, sc->lmc_outbytes,
1370 sc->lmc_inrate, sc->lmc_outrate);
1371 pos += sprintf(arg + pos, "%ld output errors\n",
1373 pos += sprintf(arg + pos, "%ld input errors\n",
1376 resp->header.arglen = pos + 1;
1389 /* Take care of synchronous response, if any */
1390 NG_RESPOND_MSG(error, node, item, resp);
1396 * get data from another node and transmit it to the line
1399 ng_lmc_rcvdata(hook_p hook, item_p item)
1403 lmc_softc_t * sc = (lmc_softc_t *) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1404 struct ifqueue *xmitq_p;
1408 /* Unpack all the data components */
1410 NGI_GET_META(item, meta);
1414 * data doesn't come in from just anywhere (e.g control hook)
1416 if ( NG_HOOK_PRIVATE(hook) == NULL) {
1422 * Now queue the data for when it can be sent
1424 if (meta && meta->priority > 0) {
1425 xmitq_p = (&sc->lmc_xmitq_hipri);
1427 xmitq_p = (&sc->lmc_xmitq);
1431 if (_IF_QFULL(xmitq_p)) {
1438 _IF_ENQUEUE(xmitq_p, m);
1440 lmc_ifstart_one(sc);
1446 * It was an error case.
1447 * check if we need to free the mbuf, and then return the error
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.
1460 ng_lmc_rmnode(node_p node)
1462 lmc_softc_t * sc = NG_NODE_PRIVATE(node);
1467 * Get rid of the old node.
1469 NG_NODE_SET_PRIVATE(node, NULL);
1470 NG_NODE_UNREF(node);
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!
1477 if (ng_make_node_common(&typestruct, &sc->lmc_node) != 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 */
1485 NG_NODE_SET_PRIVATE(sc->lmc_node, sc);
1486 callout_handle_init(&sc->lmc_handle);
1487 sc->lmc_running = 0;
1489 * turn off those LEDs...
1491 sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1492 lmc_led_on(sc, LMC_MII16_LED0);
1495 /* already linked */
1497 ng_lmc_connect(hook_p hook)
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! " */
1506 * notify on hook disconnection (destruction)
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' :)
1512 * The node removal code will remove all references except that owned by the
1516 ng_lmc_disconnect(hook_p hook)
1518 lmc_softc_t * sc = (lmc_softc_t *) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1521 * If it's the data hook, then free resources etc.
1523 if (NG_HOOK_PRIVATE(hook)) {
1525 sc->lmc_datahooks--;
1526 if (sc->lmc_datahooks == 0)
1530 sc->lmc_debug_hook = NULL;
1536 * called during bootup
1537 * or LKM loading to put this type into the list of known modules
1540 ng_lmc_init(void *ignored)
1542 if (ng_newtype(&typestruct))
1543 printf("ng_lmc install failed\n");
1544 ng_lmc_done_init = 1;
1548 * This is the PCI configuration support.
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 */
1562 #include "dev/lmc/if_lmc_fbsd3.c"