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.
27 * From NetBSD: if_de.c,v 1.56.2.1 1997/10/27 02:13:25 thorpej Exp
28 * $Id: if_lmc.c,v 1.9 1999/02/19 15:08:42 explorer Exp $
32 #warning "The lmc driver is broken and is not compiled with LINT"
35 char lmc_version[] = "BSD 1.1";
37 #include "opt_netgraph.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
47 #include <sys/syslog.h>
51 #include <netgraph/ng_message.h>
52 #include <netgraph/ng_parse.h>
53 #include <netgraph/netgraph.h>
56 #include <pci/pcivar.h>
57 #include <pci/dc21040reg.h>
58 #define INCLUDE_PATH_PREFIX "dev/lmc/"
60 /* Intel CPUs should use I/O mapped access. */
66 * This turns on all sort of debugging stuff and make the
70 #define DP(x) printf x
77 #ifndef TULIP_GP_PINSET
78 #define TULIP_GP_PINSET 0x00000100L
80 #ifndef TULIP_BUSMODE_READMULTIPLE
81 #define TULIP_BUSMODE_READMULTIPLE 0x00200000L
87 typedef struct lmc___softc lmc_softc_t;
88 typedef struct lmc___media lmc_media_t;
89 typedef struct lmc___ctl lmc_ctl_t;
91 #include "dev/lmc/if_lmcioctl.h"
92 #include "dev/lmc/if_lmcvar.h"
93 #include "dev/lmc/if_lmc_common.c"
94 #include "dev/lmc/if_lmc_media.c"
97 * This module supports
98 * the DEC 21140A pass 2.2 PCI Fast Ethernet Controller.
100 static lmc_intrfunc_t lmc_intr_normal(void *);
101 static ifnet_ret_t lmc_ifstart(lmc_softc_t * const sc );
102 static ifnet_ret_t lmc_ifstart_one(lmc_softc_t * const sc);
103 static struct mbuf *lmc_txput(lmc_softc_t * const sc, struct mbuf *m);
104 static void lmc_rx_intr(lmc_softc_t * const sc);
106 static void lmc_watchdog(lmc_softc_t * const sc);
107 static void lmc_ifup(lmc_softc_t * const sc);
108 static void lmc_ifdown(lmc_softc_t * const sc);
111 static void ng_lmc_dump_packet(struct mbuf *m);
112 #endif /* LMC_DEBUG */
113 static void ng_lmc_watchdog_frame(void *arg);
114 static void ng_lmc_init(void *ignored);
116 static ng_constructor_t ng_lmc_constructor;
117 static ng_rcvmsg_t ng_lmc_rcvmsg;
118 static ng_shutdown_t ng_lmc_rmnode;
119 static ng_newhook_t ng_lmc_newhook;
120 /*static ng_findhook_t ng_lmc_findhook; */
121 static ng_connect_t ng_lmc_connect;
122 static ng_rcvdata_t ng_lmc_rcvdata;
123 static ng_disconnect_t ng_lmc_disconnect;
125 /* Parse type for struct lmc_ctl */
126 static const struct ng_parse_fixedarray_info ng_lmc_ctl_cardspec_info = {
127 &ng_parse_int32_type,
132 static const struct ng_parse_type ng_lmc_ctl_cardspec_type = {
133 &ng_parse_fixedarray_type,
134 &ng_lmc_ctl_cardspec_info
137 static const struct ng_parse_struct_info ng_lmc_ctl_type_info = {
139 { "cardtype", &ng_parse_int32_type },
140 { "clock_source", &ng_parse_int32_type },
141 { "clock_rate", &ng_parse_int32_type },
142 { "crc_length", &ng_parse_int32_type },
143 { "cable_length", &ng_parse_int32_type },
144 { "scrambler_onoff", &ng_parse_int32_type },
145 { "cable_type", &ng_parse_int32_type },
146 { "keepalive_onoff", &ng_parse_int32_type },
147 { "ticks", &ng_parse_int32_type },
148 { "cardspec", &ng_lmc_ctl_cardspec_type },
149 { "circuit_type", &ng_parse_int32_type },
154 static const struct ng_parse_type ng_lmc_ctl_type = {
155 &ng_parse_struct_type,
156 &ng_lmc_ctl_type_info
160 /* List of commands and how to convert arguments to/from ASCII */
161 static const struct ng_cmdlist ng_lmc_cmdlist[] = {
179 static struct ng_type typestruct = {
194 static int ng_lmc_done_init = 0;
198 * Code the read the SROM and MII bit streams (I2C)
201 lmc_delay_300ns(lmc_softc_t * const sc)
204 for (idx = (300 / 33) + 1; idx > 0; idx--)
205 (void)LMC_CSR_READ(sc, csr_busmode);
211 LMC_CSR_WRITE(sc, csr_srom_mii, csr); \
212 lmc_delay_300ns(sc); \
216 lmc_srom_idle(lmc_softc_t * const sc)
220 csr = SROMSEL ; EMIT;
221 csr = SROMSEL | SROMRD; EMIT;
223 csr ^= SROMCLKON; EMIT;
226 * Write 25 cycles of 0 which will force the SROM to be idle.
228 for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
229 csr ^= SROMCLKOFF; EMIT; /* clock low; data not valid */
230 csr ^= SROMCLKON; EMIT; /* clock high; data valid */
232 csr ^= SROMCLKOFF; EMIT;
239 lmc_srom_read(lmc_softc_t * const sc)
242 const unsigned bitwidth = SROM_BITWIDTH;
243 const unsigned cmdmask = (SROMCMD_RD << bitwidth);
244 const unsigned msb = 1 << (bitwidth + 3 - 1);
245 unsigned lastidx = (1 << bitwidth) - 1;
249 for (idx = 0; idx <= lastidx; idx++) {
250 unsigned lastbit, data, bits, bit, csr;
251 csr = SROMSEL ; EMIT;
252 csr = SROMSEL | SROMRD; EMIT;
253 csr ^= SROMCSON; EMIT;
254 csr ^= SROMCLKON; EMIT;
257 for (bits = idx|cmdmask, bit = bitwidth + 3
259 ; bit--, bits <<= 1) {
260 const unsigned thisbit = bits & msb;
261 csr ^= SROMCLKOFF; EMIT; /* clock L data invalid */
262 if (thisbit != lastbit) {
263 csr ^= SROMDOUT; EMIT;/* clock L invert data */
267 csr ^= SROMCLKON; EMIT; /* clock H data valid */
270 csr ^= SROMCLKOFF; EMIT;
272 for (data = 0, bits = 0; bits < 16; bits++) {
274 csr ^= SROMCLKON; EMIT; /* clock H data valid */
275 data |= LMC_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
276 csr ^= SROMCLKOFF; EMIT; /* clock L data invalid */
278 sc->lmc_rombuf[idx*2] = data & 0xFF;
279 sc->lmc_rombuf[idx*2+1] = data >> 8;
280 csr = SROMSEL | SROMRD; EMIT;
286 #define MII_EMIT do { LMC_CSR_WRITE(sc, csr_srom_mii, csr); lmc_delay_300ns(sc); } while (0)
289 lmc_mii_writebits(lmc_softc_t * const sc, unsigned data, unsigned bits)
291 unsigned msb = 1 << (bits - 1);
292 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
293 unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
295 csr |= MII_WR; MII_EMIT; /* clock low; assert write */
297 for (; bits > 0; bits--, data <<= 1) {
298 const unsigned thisbit = data & msb;
299 if (thisbit != lastbit) {
300 csr ^= MII_DOUT; MII_EMIT; /* clock low; invert data */
302 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
304 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
309 lmc_mii_turnaround(lmc_softc_t * const sc, unsigned cmd)
311 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
313 if (cmd == MII_WRCMD) {
314 csr |= MII_DOUT; MII_EMIT; /* clock low; change data */
315 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
316 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
317 csr ^= MII_DOUT; MII_EMIT; /* clock low; change data */
319 csr |= MII_RD; MII_EMIT; /* clock low; switch to read */
321 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
322 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
326 lmc_mii_readbits(lmc_softc_t * const sc)
329 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
332 for (idx = 0, data = 0; idx < 16; idx++) {
333 data <<= 1; /* this is NOOP on the first pass through */
334 csr ^= MII_CLKON; MII_EMIT; /* clock high; data valid */
335 if (LMC_CSR_READ(sc, csr_srom_mii) & MII_DIN)
337 csr ^= MII_CLKOFF; MII_EMIT; /* clock low; data not valid */
339 csr ^= MII_RD; MII_EMIT; /* clock low; turn off read */
345 lmc_mii_readreg(lmc_softc_t * const sc, unsigned devaddr, unsigned regno)
347 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
350 csr &= ~(MII_RD|MII_CLK); MII_EMIT;
351 lmc_mii_writebits(sc, MII_PREAMBLE, 32);
352 lmc_mii_writebits(sc, MII_RDCMD, 8);
353 lmc_mii_writebits(sc, devaddr, 5);
354 lmc_mii_writebits(sc, regno, 5);
355 lmc_mii_turnaround(sc, MII_RDCMD);
357 data = lmc_mii_readbits(sc);
362 lmc_mii_writereg(lmc_softc_t * const sc, unsigned devaddr,
363 unsigned regno, unsigned data)
365 unsigned csr = LMC_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
366 csr &= ~(MII_RD|MII_CLK); MII_EMIT;
367 lmc_mii_writebits(sc, MII_PREAMBLE, 32);
368 lmc_mii_writebits(sc, MII_WRCMD, 8);
369 lmc_mii_writebits(sc, devaddr, 5);
370 lmc_mii_writebits(sc, regno, 5);
371 lmc_mii_turnaround(sc, MII_WRCMD);
372 lmc_mii_writebits(sc, data, 16);
376 lmc_read_macaddr(lmc_softc_t * const sc)
380 bcopy(sc->lmc_rombuf + 20, sc->lmc_enaddr, 6);
386 * Check to make certain there is a signal from the modem, and flicker
390 lmc_watchdog(lmc_softc_t * const sc)
394 u_int32_t link_status;
399 link_status = sc->lmc_media->get_link_status(sc);
400 ostatus = ((sc->lmc_flags & LMC_MODEMOK) == LMC_MODEMOK);
403 * hardware level link lost, but the interface is marked as up.
406 if (link_status == 0 && ostatus) {
407 printf(LMC_PRINTF_FMT ": physical link down\n",
409 sc->lmc_flags &= ~LMC_MODEMOK;
410 lmc_led_off(sc, LMC_MII16_LED1);
414 * hardware link is up, but the interface is marked as down.
415 * Bring it back up again.
417 if (link_status != 0 && !ostatus) {
418 printf(LMC_PRINTF_FMT ": physical link up\n",
420 if (sc->lmc_flags & LMC_IFUP)
426 * remember the timer value
428 ticks = LMC_CSR_READ(sc, csr_gp_timer);
429 LMC_CSR_WRITE(sc, csr_gp_timer, 0xffffffffUL);
430 sc->ictl.ticks = 0x0000ffff - (ticks & 0x0000ffff);
432 sc->lmc_out_dog = LMC_DOG_HOLDOFF;
436 * Mark the interface as "up" and enable TX/RX and TX/RX interrupts.
437 * This also does a full software reset.
440 lmc_ifup(lmc_softc_t * const sc)
442 untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
448 sc->lmc_media->set_link_status(sc, 1);
449 sc->lmc_media->set_status(sc, NULL);
451 sc->lmc_flags |= LMC_IFUP;
454 * select what interrupts we want to get
456 sc->lmc_intrmask |= (TULIP_STS_NORMALINTR
459 | TULIP_STS_ABNRMLINTR
461 | TULIP_STS_TXSTOPPED
462 | TULIP_STS_TXUNDERFLOW
463 | TULIP_STS_RXSTOPPED
465 LMC_CSR_WRITE(sc, csr_intr, sc->lmc_intrmask);
467 sc->lmc_cmdmode |= TULIP_CMD_TXRUN;
468 sc->lmc_cmdmode |= TULIP_CMD_RXRUN;
469 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
471 untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
472 sc->lmc_handle = timeout(ng_lmc_watchdog_frame, sc, hz);
476 * check if the physical link is up
478 if (sc->lmc_media->get_link_status(sc)) {
479 sc->lmc_flags |= LMC_MODEMOK;
480 lmc_led_on(sc, LMC_MII16_LED1);
485 * Mark the interface as "down" and disable TX/RX and TX/RX interrupts.
486 * This is done by performing a full reset on the interface.
489 lmc_ifdown(lmc_softc_t * const sc)
491 untimeout(ng_lmc_watchdog_frame, sc, sc->lmc_handle);
493 sc->lmc_flags &= ~LMC_IFUP;
495 sc->lmc_media->set_link_status(sc, 0);
496 lmc_led_off(sc, LMC_MII16_LED1);
500 sc->lmc_media->set_status(sc, NULL);
504 lmc_rx_intr(lmc_softc_t * const sc)
506 lmc_ringinfo_t * const ri = &sc->lmc_rxinfo;
512 tulip_desc_t *eop = ri->ri_nextin;
513 int total_len = 0, last_offset = 0;
514 struct mbuf *ms = NULL, *me = NULL;
517 if (fillok && sc->lmc_rxq.ifq_len < LMC_RXQ_TARGET)
521 * If the TULIP has no descriptors, there can't be any receive
522 * descriptors to process.
524 if (eop == ri->ri_nextout)
528 * 90% of the packets will fit in one descriptor. So we
529 * optimize for that case.
531 if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
532 _IF_DEQUEUE(&sc->lmc_rxq, ms);
536 * If still owned by the TULIP, don't touch it.
538 if (((volatile tulip_desc_t *)eop)->d_status & TULIP_DSTS_OWNER)
542 * It is possible (though improbable unless the
543 * BIG_PACKET support is enabled or MCLBYTES < 1518)
544 * for a received packet to cross more than one
545 * receive descriptor.
547 while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
548 if (++eop == ri->ri_last)
550 if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
557 * Dequeue the first buffer for the start of the
558 * packet. Hopefully this will be the only one we
559 * need to dequeue. However, if the packet consumed
560 * multiple descriptors, then we need to dequeue
561 * those buffers and chain to the starting mbuf.
562 * All buffers but the last buffer have the same
563 * length so we can set that now. (we add to
564 * last_offset instead of multiplying since we
565 * normally won't go into the loop and thereby
566 * saving ourselves from doing a multiplication
567 * by 0 in the normal case).
569 _IF_DEQUEUE(&sc->lmc_rxq, ms);
570 for (me = ms; total_len > 0; total_len--) {
571 me->m_len = LMC_RX_BUFLEN;
572 last_offset += LMC_RX_BUFLEN;
573 _IF_DEQUEUE(&sc->lmc_rxq, me->m_next);
579 * Now get the size of received packet (minus the CRC).
581 total_len = ((eop->d_status >> 16) & 0x7FFF);
582 if (sc->ictl.crc_length == 16)
587 sc->lmc_inbytes += total_len;
590 if ((sc->lmc_flags & LMC_RXIGNORE) == 0
591 && ((eop->d_status & LMC_DSTS_ERRSUM) == 0
593 me->m_len = total_len - last_offset;
594 sc->lmc_flags |= LMC_RXACT;
598 if (eop->d_status & TULIP_DSTS_RxOVERFLOW) {
599 sc->lmc_dot3stats.dot3StatsInternalMacReceiveErrors++;
604 if (++eop == ri->ri_last)
610 * Either we are priming the TULIP with mbufs (m == NULL)
611 * or we are about to accept an mbuf for the upper layers
612 * so we need to allocate an mbuf to replace it. If we
613 * can't replace it, send up it anyways. This may cause
614 * us to drop packets in the future but that's better than
615 * being caught in livelock.
617 * Note that if this packet crossed multiple descriptors
618 * we don't even try to reallocate all the mbufs here.
619 * Instead we rely on the test of the beginning of
620 * the loop to refill for the extra consumed mbufs.
622 if (accept || ms == NULL) {
624 MGETHDR(m0, M_DONTWAIT, MT_DATA);
626 MCLGET(m0, M_DONTWAIT);
627 if ((m0->m_flags & M_EXT) == 0) {
635 ms->m_pkthdr.len = total_len;
636 ms->m_pkthdr.rcvif = NULL;
637 NG_SEND_DATA_ONLY(error, sc->lmc_hook, ms);
643 * Couldn't allocate a new buffer. Don't bother
644 * trying to replenish the receive queue.
647 sc->lmc_flags |= LMC_RXBUFSLOW;
651 * Now give the buffer(s) to the TULIP and save in our
655 ri->ri_nextout->d_length1 = LMC_RX_BUFLEN;
656 ri->ri_nextout->d_addr1 = LMC_KVATOPHYS(sc, mtod(ms, caddr_t));
657 ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
658 if (++ri->ri_nextout == ri->ri_last)
659 ri->ri_nextout = ri->ri_first;
662 _IF_ENQUEUE(&sc->lmc_rxq, ms);
663 } while ((ms = me) != NULL);
665 if (sc->lmc_rxq.ifq_len >= LMC_RXQ_TARGET)
666 sc->lmc_flags &= ~LMC_RXBUFSLOW;
671 lmc_tx_intr(lmc_softc_t * const sc)
673 lmc_ringinfo_t * const ri = &sc->lmc_txinfo;
680 while (ri->ri_free < ri->ri_max) {
682 if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
685 d_flag = ri->ri_nextin->d_flag;
686 if (d_flag & TULIP_DFLAG_TxLASTSEG) {
687 const u_int32_t d_status = ri->ri_nextin->d_status;
688 _IF_DEQUEUE(&sc->lmc_txq, m);
691 if (sc->lmc_bpf != NULL)
695 #if defined(LMC_DEBUG)
697 printf(LMC_PRINTF_FMT ": tx_intr: failed to dequeue mbuf?!?\n", LMC_PRINTF_ARGS);
701 if (d_status & LMC_DSTS_ERRSUM) {
703 if (d_status & TULIP_DSTS_TxUNDERFLOW)
704 sc->lmc_dot3stats.dot3StatsInternalTransmitUnderflows++;
706 if (d_status & TULIP_DSTS_TxDEFERRED)
707 sc->lmc_dot3stats.dot3StatsDeferredTransmissions++;
711 if (++ri->ri_nextin == ri->ri_last)
712 ri->ri_nextin = ri->ri_first;
716 /*sc->lmc_if.if_flags &= ~IFF_OACTIVE;*/
717 sc->lmc_out_deficit++;
720 * If nothing left to transmit, disable the timer.
721 * Else if progress, reset the timer back to 2 ticks.
723 sc->lmc_opackets += xmits;
729 lmc_print_abnormal_interrupt (lmc_softc_t * const sc, u_int32_t csr)
731 printf(LMC_PRINTF_FMT ": Abnormal interrupt\n", LMC_PRINTF_ARGS);
735 lmc_intr_handler(lmc_softc_t * const sc, int *progress_p)
739 while ((csr = LMC_CSR_READ(sc, csr_status)) & sc->lmc_intrmask) {
742 LMC_CSR_WRITE(sc, csr_status, csr);
744 if (csr & TULIP_STS_SYSERROR) {
745 sc->lmc_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
746 if (sc->lmc_flags & LMC_NOMESSAGES) {
747 sc->lmc_flags |= LMC_SYSTEMERROR;
749 printf(LMC_PRINTF_FMT ": system error: %s\n",
751 lmc_system_errors[sc->lmc_last_system_error]);
753 sc->lmc_flags |= LMC_NEEDRESET;
754 sc->lmc_system_errors++;
757 if (csr & (TULIP_STS_RXINTR | TULIP_STS_RXNOBUF)) {
758 u_int32_t misses = LMC_CSR_READ(sc, csr_missed_frames);
759 if (csr & TULIP_STS_RXNOBUF)
760 sc->lmc_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
762 * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
763 * on receive overflows.
765 if ((misses & 0x0FFE0000) && (sc->lmc_features & LMC_HAVE_RXBADOVRFLW)) {
766 sc->lmc_dot3stats.dot3StatsInternalMacReceiveErrors++;
768 * Stop the receiver process and spin until it's stopped.
769 * Tell rx_intr to drop the packets it dequeues.
771 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode & ~TULIP_CMD_RXRUN);
772 while ((LMC_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
774 LMC_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
775 sc->lmc_flags |= LMC_RXIGNORE;
778 if (sc->lmc_flags & LMC_RXIGNORE) {
780 * Restart the receiver.
782 sc->lmc_flags &= ~LMC_RXIGNORE;
783 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
786 if (csr & TULIP_STS_ABNRMLINTR) {
787 u_int32_t tmp = csr & sc->lmc_intrmask
788 & ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
789 if (csr & TULIP_STS_TXUNDERFLOW) {
790 if ((sc->lmc_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
791 sc->lmc_cmdmode += TULIP_CMD_THRSHLD96;
792 sc->lmc_flags |= LMC_NEWTXTHRESH;
793 } else if (sc->lmc_features & LMC_HAVE_STOREFWD) {
794 sc->lmc_cmdmode |= TULIP_CMD_STOREFWD;
795 sc->lmc_flags |= LMC_NEWTXTHRESH;
798 if (sc->lmc_flags & LMC_NOMESSAGES) {
799 sc->lmc_statusbits |= tmp;
801 lmc_print_abnormal_interrupt(sc, tmp);
802 sc->lmc_flags |= LMC_NOMESSAGES;
804 LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
807 if (csr & TULIP_STS_TXINTR)
810 if (sc->lmc_flags & LMC_WANTTXSTART)
815 static lmc_intrfunc_t
816 lmc_intr_normal(void *arg)
818 lmc_softc_t * sc = (lmc_softc_t *) arg;
821 lmc_intr_handler(sc, &progress);
823 #if !defined(LMC_VOID_INTRFUNC)
829 lmc_mbuf_compress(struct mbuf *m)
832 #if MCLBYTES >= LMC_MTU + PPP_HEADER_LEN && !defined(BIG_PACKET)
833 MGETHDR(m0, M_DONTWAIT, MT_DATA);
835 if (m->m_pkthdr.len > MHLEN) {
836 MCLGET(m0, M_DONTWAIT);
837 if ((m0->m_flags & M_EXT) == 0) {
843 m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
844 m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
848 int len = m->m_pkthdr.len;
849 struct mbuf **mp = &m0;
853 MGETHDR(*mp, M_DONTWAIT, MT_DATA);
855 MGET(*mp, M_DONTWAIT, MT_DATA);
863 MCLGET(*mp, M_DONTWAIT);
864 if (((*mp)->m_flags & M_EXT) == 0) {
869 (*mp)->m_len = (len <= MCLBYTES ? len : MCLBYTES);
871 (*mp)->m_len = (len <= mlen ? len : mlen);
873 m_copydata(m, m->m_pkthdr.len - len,
874 (*mp)->m_len, mtod((*mp), caddr_t));
885 * queue the mbuf handed to us for the interface. If we cannot
886 * queue it, return the mbuf. Return NULL if the mbuf was queued.
889 lmc_txput(lmc_softc_t * const sc, struct mbuf *m)
891 lmc_ringinfo_t * const ri = &sc->lmc_txinfo;
892 tulip_desc_t *eop, *nextout;
897 #if defined(LMC_DEBUG)
898 if ((sc->lmc_cmdmode & TULIP_CMD_TXRUN) == 0) {
899 printf(LMC_PRINTF_FMT ": txput: tx not running\n",
901 sc->lmc_flags |= LMC_WANTTXSTART;
907 * Now we try to fill in our transmit descriptors. This is
908 * a bit reminiscent of going on the Ark two by two
909 * since each descriptor for the TULIP can describe
910 * two buffers. So we advance through packet filling
911 * each of the two entries at a time to fill each
912 * descriptor. Clear the first and last segment bits
913 * in each descriptor (actually just clear everything
914 * but the end-of-ring or chain bits) to make sure
915 * we don't get messed up by previously sent packets.
917 * We may fail to put the entire packet on the ring if
918 * there is either not enough ring entries free or if the
919 * packet has more than MAX_TXSEG segments. In the former
920 * case we will just wait for the ring to empty. In the
921 * latter case we have to recopy.
925 eop = nextout = ri->ri_nextout;
931 caddr_t addr = mtod(m0, caddr_t);
932 unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
935 unsigned slen = min(len, clsize);
939 slen = 2040, partial = 1;
942 if (segcnt > LMC_MAX_TXSEG) {
944 * The packet exceeds the number of transmit
945 * buffer entries that we can use for one
946 * packet, so we have recopy it into one mbuf
947 * and then try again.
949 m = lmc_mbuf_compress(m);
957 * See if there's any unclaimed space
958 * in the transmit ring.
960 if ((free += lmc_tx_intr(sc)) == 0) {
962 * There's no more room but
963 * since nothing has been
964 * committed at this point,
965 * just show output is active,
966 * put back the mbuf and
969 sc->lmc_flags |= LMC_WANTTXSTART;
974 if (++nextout == ri->ri_last)
975 nextout = ri->ri_first;
976 eop->d_flag &= TULIP_DFLAG_ENDRING;
977 eop->d_flag |= TULIP_DFLAG_TxNOPADDING;
978 if (sc->ictl.crc_length == 16)
979 eop->d_flag |= TULIP_DFLAG_TxHASCRC;
980 eop->d_status = d_status;
981 eop->d_addr1 = LMC_KVATOPHYS(sc, addr);
982 eop->d_length1 = slen;
985 * Fill in second half of descriptor
987 eop->d_addr2 = LMC_KVATOPHYS(sc, addr);
988 eop->d_length2 = slen;
990 d_status = TULIP_DSTS_OWNER;
999 } while ((m0 = m0->m_next) != NULL);
1003 * The descriptors have been filled in. Now get ready
1006 _IF_ENQUEUE(&sc->lmc_txq, m);
1010 * Make sure the next descriptor after this packet is owned
1011 * by us since it may have been set up above if we ran out
1012 * of room in the ring.
1014 nextout->d_status = 0;
1017 * If we only used the first segment of the last descriptor,
1018 * make sure the second segment will not be used.
1026 * Mark the last and first segments, indicate we want a transmit
1027 * complete interrupt, and tell it to transmit!
1029 eop->d_flag |= TULIP_DFLAG_TxLASTSEG | TULIP_DFLAG_TxWANTINTR;
1032 * Note that ri->ri_nextout is still the start of the packet
1033 * and until we set the OWNER bit, we can still back out of
1034 * everything we have done.
1036 ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
1037 ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
1039 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1042 * This advances the ring for us.
1044 ri->ri_nextout = nextout;
1048 * switch back to the single queueing ifstart.
1050 sc->lmc_flags &= ~LMC_WANTTXSTART;
1051 sc->lmc_xmit_busy = 0;
1052 sc->lmc_out_dog = 0;
1055 * If we want a txstart, there must be not enough space in the
1056 * transmit ring. So we want to enable transmit done interrupts
1057 * so we can immediately reclaim some space. When the transmit
1058 * interrupt is posted, the interrupt handler will call tx_intr
1059 * to reclaim space and then txstart (since WANTTXSTART is set).
1060 * txstart will move the packet into the transmit ring and clear
1061 * WANTTXSTART thereby causing TXINTR to be cleared.
1070 * These routines gets called at device spl
1074 lmc_ifstart(lmc_softc_t * const sc)
1078 if (sc->lmc_flags & LMC_IFUP) {
1079 sc->lmc_xmit_busy = 1;
1081 struct ifqueue *q = &sc->lmc_xmitq_hipri;
1088 sc->lmc_outbytes = m->m_pkthdr.len;
1090 if ((m = lmc_txput(sc, m)) != NULL) {
1092 printf(LMC_PRINTF_FMT
1093 ": lmc_txput failed\n",
1097 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1106 lmc_ifstart_one(lmc_softc_t * const sc)
1110 if ((sc->lmc_flags & LMC_IFUP)) {
1111 struct ifqueue *q = &sc->lmc_xmitq_hipri;
1118 sc->lmc_outbytes += m->m_pkthdr.len;
1120 if ((m = lmc_txput(sc, m)) != NULL) {
1123 LMC_CSR_WRITE(sc, csr_txpoll, 1);
1129 * Set up the OS interface magic and attach to the operating system
1133 lmc_attach(lmc_softc_t * const sc)
1136 * we have found a node, make sure our 'type' is availabe.
1138 if (ng_lmc_done_init == 0) ng_lmc_init(NULL);
1139 if (ng_make_node_common(&typestruct, &sc->lmc_node) != 0)
1141 sprintf(sc->lmc_nodename, "%s%d", NG_LMC_NODE_TYPE, sc->lmc_unit);
1142 if (ng_name_node(sc->lmc_node, sc->lmc_nodename)) {
1143 NG_NODE_UNREF(sc->lmc_node); /* make it go away again */
1146 NG_NODE_SET_PRIVATE(sc->lmc_node, sc);
1147 callout_handle_init(&sc->lmc_handle);
1148 sc->lmc_xmitq.ifq_maxlen = IFQ_MAXLEN;
1149 sc->lmc_xmitq_hipri.ifq_maxlen = IFQ_MAXLEN;
1150 mtx_init(&sc->lmc_xmitq.ifq_mtx, "lmc_xmitq", NULL, MTX_DEF);
1151 mtx_init(&sc->lmc_xmitq_hipri.ifq_mtx, "lmc_xmitq_hipri", NULL, MTX_DEF);
1152 sc->lmc_running = 0;
1155 * turn off those LEDs...
1157 sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1158 lmc_led_on(sc, LMC_MII16_LED0);
1164 lmc_initring(lmc_softc_t * const sc, lmc_ringinfo_t * const ri,
1165 tulip_desc_t *descs, int ndescs)
1167 ri->ri_max = ndescs;
1168 ri->ri_first = descs;
1169 ri->ri_last = ri->ri_first + ri->ri_max;
1170 bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
1171 ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
1178 ng_lmc_dump_packet(struct mbuf *m)
1182 printf("mbuf: %d bytes, %s packet\n", m->m_len,
1183 (m->m_type == MT_DATA)?"data":"other");
1185 for (i=0; i < m->m_len; i++) {
1186 if( (i % 8) == 0 ) {
1187 if( i ) printf("\n");
1192 printf( "0x%02x", m->m_dat[i] );
1196 #endif /* LMC_DEBUG */
1198 /* Device timeout/watchdog routine */
1200 ng_lmc_watchdog_frame(void *arg)
1202 lmc_softc_t * sc = (lmc_softc_t *) arg;
1206 if(sc->lmc_running == 0)
1207 return; /* if we are not running let timeouts die */
1209 * calculate the apparent throughputs
1213 speed = sc->lmc_inbytes - sc->lmc_lastinbytes;
1214 sc->lmc_lastinbytes = sc->lmc_inbytes;
1215 if ( sc->lmc_inrate < speed )
1216 sc->lmc_inrate = speed;
1217 speed = sc->lmc_outbytes - sc->lmc_lastoutbytes;
1218 sc->lmc_lastoutbytes = sc->lmc_outbytes;
1219 if ( sc->lmc_outrate < speed )
1220 sc->lmc_outrate = speed;
1224 if ((sc->lmc_inlast > LMC_QUITE_A_WHILE)
1225 && (sc->lmc_out_deficit > LMC_LOTS_OF_PACKETS)) {
1226 log(LOG_ERR, "%s%d: No response from remote end\n",
1227 sc->lmc_name, sc->lmc_unit);
1231 sc->lmc_inlast = sc->lmc_out_deficit = 0;
1233 } else if (sc->lmc_xmit_busy) {
1234 if (sc->lmc_out_dog == 0) {
1235 log(LOG_ERR, "ar%d: Transmit failure.. no clock?\n",
1244 sc->lmc_inlast = sc->lmc_out_deficit = 0;
1250 sc->lmc_handle = timeout(ng_lmc_watchdog_frame, sc, hz);
1253 /***********************************************************************
1254 * This section contains the methods for the Netgraph interface
1255 ***********************************************************************/
1257 * It is not possible or allowable to create a node of this type.
1258 * If the hardware exists, it will already have created it.
1261 ng_lmc_constructor(node_p node)
1267 * give our ok for a hook to be added...
1268 * If we are not running this should kick the device into life.
1269 * We allow hooks called "control", "rawdata", and "debug".
1270 * The hook's private info points to our stash of info about that
1274 ng_lmc_newhook(node_p node, hook_p hook, const char *name)
1276 lmc_softc_t * sc = NG_NODE_PRIVATE(node);
1279 * check if it's our friend the debug hook
1281 if (strcmp(name, NG_LMC_HOOK_DEBUG) == 0) {
1282 NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */
1283 sc->lmc_debug_hook = hook;
1288 * Check for raw mode hook.
1290 if (strcmp(name, NG_LMC_HOOK_RAW) != 0) {
1293 NG_HOOK_SET_PRIVATE(hook, sc);
1294 sc->lmc_hook = hook;
1295 sc->lmc_datahooks++;
1301 * incoming messages.
1302 * Just respond to the generic TEXT_STATUS message
1305 ng_lmc_rcvmsg(node_p node, item_p item, hook_p lasthook)
1307 lmc_softc_t *sc = NG_NODE_PRIVATE(node);
1308 struct ng_mesg *resp = NULL;
1310 struct ng_mesg *msg;
1312 NGI_GET_MSG(item, msg);
1313 switch (msg->header.typecookie) {
1315 switch (msg->header.cmd) {
1316 case NGM_LMC_GET_CTL:
1320 NG_MKRESPONSE(resp, msg, sizeof(*ctl), M_NOWAIT);
1325 ctl = (lmc_ctl_t *) resp->data;
1326 memcpy( ctl, &sc->ictl, sizeof(*ctl) );
1329 case NGM_LMC_SET_CTL:
1333 if (msg->header.arglen != sizeof(*ctl)) {
1338 ctl = (lmc_ctl_t *) msg->data;
1339 sc->lmc_media->set_status(sc, ctl);
1343 error = EINVAL; /* unknown command */
1347 case NGM_GENERIC_COOKIE:
1348 switch(msg->header.cmd) {
1349 case NGM_TEXT_STATUS: {
1353 int resplen = sizeof(struct ng_mesg) + 512;
1354 NG_MKRESPONSE(resp, msg, resplen, M_NOWAIT);
1362 * Put in the throughput information.
1364 pos = sprintf(arg, "%ld bytes in, %ld bytes out\n"
1365 "highest rate seen: %ld B/S in, "
1367 sc->lmc_inbytes, sc->lmc_outbytes,
1368 sc->lmc_inrate, sc->lmc_outrate);
1369 pos += sprintf(arg + pos, "%ld output errors\n",
1371 pos += sprintf(arg + pos, "%ld input errors\n",
1374 resp->header.arglen = pos + 1;
1387 /* Take care of synchronous response, if any */
1388 NG_RESPOND_MSG(error, node, item, resp);
1394 * get data from another node and transmit it to the line
1397 ng_lmc_rcvdata(hook_p hook, item_p item)
1401 lmc_softc_t * sc = (lmc_softc_t *) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1402 struct ifqueue *xmitq_p;
1406 /* Unpack all the data components */
1408 NGI_GET_META(item, meta);
1412 * data doesn't come in from just anywhere (e.g control hook)
1414 if ( NG_HOOK_PRIVATE(hook) == NULL) {
1420 * Now queue the data for when it can be sent
1422 if (meta && meta->priority > 0) {
1423 xmitq_p = (&sc->lmc_xmitq_hipri);
1425 xmitq_p = (&sc->lmc_xmitq);
1429 if (_IF_QFULL(xmitq_p)) {
1436 _IF_ENQUEUE(xmitq_p, m);
1438 lmc_ifstart_one(sc);
1444 * It was an error case.
1445 * check if we need to free the mbuf, and then return the error
1453 * do local shutdown processing..
1454 * this node will refuse to go away, unless the hardware says to..
1455 * don't unref the node, or remove our name. just clear our links up.
1458 ng_lmc_rmnode(node_p node)
1460 lmc_softc_t * sc = NG_NODE_PRIVATE(node);
1465 * Get rid of the old node.
1467 NG_NODE_SET_PRIVATE(node, NULL);
1468 NG_NODE_UNREF(node);
1471 * Create a new node. This is basically what a device
1472 * driver would do in the attach routine. So let's just do that..
1473 * The node is dead, long live the node!
1475 if (ng_make_node_common(&typestruct, &sc->lmc_node) != 0)
1477 sprintf(sc->lmc_nodename, "%s%d", NG_LMC_NODE_TYPE, sc->lmc_unit);
1478 if (ng_name_node(sc->lmc_node, sc->lmc_nodename)) {
1479 sc->lmc_node = NULL; /* to be sure */
1480 NG_NODE_UNREF(sc->lmc_node); /* make it go away */
1483 NG_NODE_SET_PRIVATE(sc->lmc_node, sc);
1484 callout_handle_init(&sc->lmc_handle);
1485 sc->lmc_running = 0;
1487 * turn off those LEDs...
1489 sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
1490 lmc_led_on(sc, LMC_MII16_LED0);
1493 /* already linked */
1495 ng_lmc_connect(hook_p hook)
1497 /* We are probably not at splnet.. force outward queueing */
1498 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
1499 /* be really amiable and just say "YUP that's OK by me! " */
1504 * notify on hook disconnection (destruction)
1506 * For this type, removal of the last link resets tries to destroy the node.
1507 * As the device still exists, the shutdown method will not actually
1508 * destroy the node, but reset the device and leave it 'fresh' :)
1510 * The node removal code will remove all references except that owned by the
1514 ng_lmc_disconnect(hook_p hook)
1516 lmc_softc_t * sc = (lmc_softc_t *) NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
1519 * If it's the data hook, then free resources etc.
1521 if (NG_HOOK_PRIVATE(hook)) {
1523 sc->lmc_datahooks--;
1524 if (sc->lmc_datahooks == 0)
1528 sc->lmc_debug_hook = NULL;
1534 * called during bootup
1535 * or LKM loading to put this type into the list of known modules
1538 ng_lmc_init(void *ignored)
1540 if (ng_newtype(&typestruct))
1541 printf("ng_lmc install failed\n");
1542 ng_lmc_done_init = 1;
1546 * This is the PCI configuration support.
1548 #define PCI_CFID 0x00 /* Configuration ID */
1549 #define PCI_CFCS 0x04 /* Configurtion Command/Status */
1550 #define PCI_CFRV 0x08 /* Configuration Revision */
1551 #define PCI_CFLT 0x0c /* Configuration Latency Timer */
1552 #define PCI_CBIO 0x10 /* Configuration Base IO Address */
1553 #define PCI_CBMA 0x14 /* Configuration Base Memory Address */
1554 #define PCI_SSID 0x2c /* subsystem config register */
1555 #define PCI_CFIT 0x3c /* Configuration Interrupt */
1556 #define PCI_CFDA 0x40 /* Configuration Driver Area */
1560 #include "dev/lmc/if_lmc_fbsd3.c"