2 * Copyright (c) 1994-2000
3 * Paul Richards. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * verbatim and that no modifications are made prior to this
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name Paul Richards may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $FreeBSD: src/sys/dev/lnc/if_lnc.c,v 1.89 2001/07/04 13:00:19 nyan Exp $
31 * $DragonFly: src/sys/dev/netif/lnc/Attic/if_lnc.c,v 1.25 2005/12/11 01:54:08 swildner Exp $
40 * Check all the XXX comments -- some of them are just things I've left
41 * unfinished rather than "difficult" problems that were hacked around.
45 * Check how all the arpcom flags get set and used.
47 * Re-inline and re-static all routines after debugging.
49 * Remember to assign iobase in SHMEM probe routines.
51 * Replace all occurences of LANCE-controller-card etc in prints by the name
52 * strings of the appropriate type -- nifty window dressing
54 * Add DEPCA support -- mostly done.
60 /* Some defines that should really be in generic locations */
62 #define MULTICAST_FILTER_LEN 8
64 #include <sys/param.h>
65 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
70 #include <sys/module.h>
71 #include <sys/socket.h>
72 #include <sys/sockio.h>
73 #include <sys/syslog.h>
74 #include <sys/serialize.h>
75 #include <sys/thread2.h>
77 #include <machine/bus.h>
78 #include <machine/resource.h>
81 #include <net/ethernet.h>
83 #include <net/ifq_var.h>
84 #include <net/if_dl.h>
85 #include <net/if_types.h>
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
92 #include <machine/md_var.h>
94 #include <dev/netif/lnc/if_lncvar.h>
95 #include <dev/netif/lnc/if_lncreg.h>
97 DECLARE_DUMMY_MODULE(if_lnc);
99 devclass_t lnc_devclass;
101 static char const * const nic_ident[] = {
106 "CNET98S", /* PC-98 */
109 static char const * const ic_ident[] = {
124 static void lnc_setladrf(struct lnc_softc *sc);
125 static void lnc_reset(struct lnc_softc *sc);
126 static void lnc_free_mbufs(struct lnc_softc *sc);
127 static __inline int alloc_mbuf_cluster(struct lnc_softc *sc,
128 struct host_ring_entry *desc);
129 static __inline struct mbuf *chain_mbufs(struct lnc_softc *sc,
132 static __inline struct mbuf *mbuf_packet(struct lnc_softc *sc,
135 static void lnc_rint(struct lnc_softc *sc);
136 static void lnc_tint(struct lnc_softc *sc);
138 static void lnc_init(void *);
139 static __inline int mbuf_to_buffer(struct mbuf *m, char *buffer);
140 static __inline struct mbuf *chain_to_cluster(struct mbuf *m);
141 static void lnc_start(struct ifnet *ifp);
142 static int lnc_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
144 static void lnc_watchdog(struct ifnet *ifp);
146 void lnc_dump_state(struct lnc_softc *sc);
147 void mbuf_dump_chain(struct mbuf *m);
151 read_csr(struct lnc_softc *sc, u_short port)
153 lnc_outw(sc->rap, port);
154 return (lnc_inw(sc->rdp));
158 write_csr(struct lnc_softc *sc, u_short port, u_short val)
160 lnc_outw(sc->rap, port);
161 lnc_outw(sc->rdp, val);
165 write_bcr(struct lnc_softc *sc, u_short port, u_short val)
167 lnc_outw(sc->rap, port);
168 lnc_outw(sc->bdp, val);
171 static __inline u_short
172 read_bcr(struct lnc_softc *sc, u_short port)
174 lnc_outw(sc->rap, port);
175 return (lnc_inw(sc->bdp));
179 lance_probe(struct lnc_softc *sc)
181 write_csr(sc, CSR0, STOP);
183 if ((lnc_inw(sc->rdp) & STOP) && ! (read_csr(sc, CSR3))) {
185 * Check to see if it's a C-LANCE. For the LANCE the INEA bit
186 * cannot be set while the STOP bit is. This restriction is
187 * removed for the C-LANCE.
189 write_csr(sc, CSR0, INEA);
190 if (read_csr(sc, CSR0) & INEA)
198 static __inline u_long
199 ether_crc(const u_char *ether_addr)
201 #define POLYNOMIAL 0xEDB88320UL
203 u_int crc = 0xFFFFFFFFUL;
205 for (i = 0; i < ETHER_ADDR_LEN; i++) {
206 addr = *ether_addr++;
207 for (j = 0; j < MULTICAST_FILTER_LEN; j++) {
208 crc = (crc >> 1) ^ (((crc ^ addr) & 1) ? POLYNOMIAL : 0);
217 * Set up the logical address filter for multicast packets
220 lnc_setladrf(struct lnc_softc *sc)
222 struct ifnet *ifp = &sc->arpcom.ac_if;
223 struct ifmultiaddr *ifma;
227 if (sc->flags & IFF_ALLMULTI) {
228 for (i=0; i < MULTICAST_FILTER_LEN; i++)
229 sc->init_block->ladrf[i] = 0xFF;
234 * For each multicast address, calculate a crc for that address and
235 * then use the high order 6 bits of the crc as a hash code where
236 * bits 3-5 select the byte of the address filter and bits 0-2 select
237 * the bit within that byte.
240 bzero(sc->init_block->ladrf, MULTICAST_FILTER_LEN);
241 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
242 if (ifma->ifma_addr->sa_family != AF_LINK)
245 index = ether_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr))
247 sc->init_block->ladrf[index >> 3] |= 1 << (index & 7);
252 lnc_stop(struct lnc_softc *sc)
254 write_csr(sc, CSR0, STOP);
258 lnc_reset(struct lnc_softc *sc)
264 lnc_free_mbufs(struct lnc_softc *sc)
269 * We rely on other routines to keep the buff.mbuf field valid. If
270 * it's not NULL then we assume it points to an allocated mbuf.
273 for (i = 0; i < NDESC(sc->nrdre); i++)
274 if ((sc->recv_ring + i)->buff.mbuf)
275 m_free((sc->recv_ring + i)->buff.mbuf);
277 for (i = 0; i < NDESC(sc->ntdre); i++)
278 if ((sc->trans_ring + i)->buff.mbuf)
279 m_free((sc->trans_ring + i)->buff.mbuf);
286 alloc_mbuf_cluster(struct lnc_softc *sc, struct host_ring_entry *desc)
288 struct mds *md = desc->md;
292 /* Try and get cluster off local cache */
293 if (sc->mbuf_count) {
296 sc->mbufs = m->m_next;
297 /* XXX m->m_data = m->m_ext.ext_buf;*/
299 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
305 addr = kvtop(m->m_data);
307 md->md1= ((addr >> 16) & 0xff) | OWN;
308 md->md2 = -(short)(MCLBYTES - sizeof(struct pkthdr));
313 static __inline struct mbuf *
314 chain_mbufs(struct lnc_softc *sc, int start_of_packet, int pkt_len)
316 struct mbuf *head, *m;
317 struct host_ring_entry *desc;
320 * Turn head into a pkthdr mbuf --
321 * assumes a pkthdr type mbuf was
322 * allocated to the descriptor
326 desc = sc->recv_ring + start_of_packet;
328 head = desc->buff.mbuf;
329 head->m_flags |= M_PKTHDR;
330 bzero(&head->m_pkthdr, sizeof(head->m_pkthdr));
335 m->m_len = min((MCLBYTES - sizeof(struct pkthdr)), pkt_len);
337 if (alloc_mbuf_cluster(sc, desc))
338 return((struct mbuf *)NULL);
339 INC_MD_PTR(start_of_packet, sc->nrdre)
340 desc = sc->recv_ring + start_of_packet;
341 m->m_next = desc->buff.mbuf;
342 } while (start_of_packet != sc->recv_next);
348 static __inline struct mbuf *
349 mbuf_packet(struct lnc_softc *sc, int start_of_packet, int pkt_len)
352 struct host_ring_entry *start;
353 struct mbuf *head,*m,*m_prev;
354 char *data,*mbuf_data;
358 /* Get a pkthdr mbuf for the start of packet */
359 MGETHDR(head, MB_DONTWAIT, MT_DATA);
361 LNCSTATS(drop_packet)
367 start = sc->recv_ring + start_of_packet;
368 /*blen = -(start->md->md2);*/
369 blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
370 data = start->buff.data;
371 mbuf_data = m->m_data;
373 while (start_of_packet != sc->recv_next) {
375 * If the data left fits in a single buffer then set
376 * blen to the size of the data left.
382 * amount is least of data in current ring buffer and
383 * amount of space left in current mbuf.
385 amount = min(blen, M_TRAILINGSPACE(m));
387 /* mbuf must be empty */
389 m = m_getl(pkt_len, MB_DONTWAIT, MT_DATA, 0, NULL);
396 amount = min(blen, M_TRAILINGSPACE(m));
397 mbuf_data = m->m_data;
399 bcopy(data, mbuf_data, amount);
407 start->md->md1 &= HADR;
408 start->md->md1 |= OWN;
409 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
410 INC_MD_PTR(start_of_packet, sc->nrdre)
411 start = sc->recv_ring + start_of_packet;
412 data = start->buff.data;
413 /*blen = -(start->md->md2);*/
414 blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
422 lnc_rint(struct lnc_softc *sc)
424 struct ifnet *ifp = &sc->arpcom.ac_if;
425 struct host_ring_entry *next, *start;
428 struct ether_header *eh;
434 * The LANCE will issue a RINT interrupt when the ownership of the
435 * last buffer of a receive packet has been relinquished by the LANCE.
436 * Therefore, it can be assumed that a complete packet can be found
437 * before hitting buffers that are still owned by the LANCE, if not
438 * then there is a bug in the driver that is causing the descriptors
439 * to get out of sync.
443 if ((sc->recv_ring + sc->recv_next)->md->md1 & OWN) {
444 int unit = ifp->if_dunit;
445 log(LOG_ERR, "lnc%d: Receive interrupt with buffer still owned by controller -- Resetting\n", unit);
449 if (!((sc->recv_ring + sc->recv_next)->md->md1 & STP)) {
450 int unit = ifp->if_dunit;
451 log(LOG_ERR, "lnc%d: Receive interrupt but not start of packet -- Resetting\n", unit);
458 next = sc->recv_ring + sc->recv_next;
459 while ((flags = next->md->md1) & STP) {
461 /* Make a note of the start of the packet */
462 start_of_packet = sc->recv_next;
465 * Find the end of the packet. Even if not data chaining,
466 * jabber packets can overrun into a second descriptor.
467 * If there is no error, then the ENP flag is set in the last
468 * descriptor of the packet. If there is an error then the ERR
469 * flag will be set in the descriptor where the error occured.
470 * Therefore, to find the last buffer of a packet we search for
474 if (!(flags & (ENP | MDERR))) {
476 INC_MD_PTR(sc->recv_next, sc->nrdre)
477 next = sc->recv_ring + sc->recv_next;
478 flags = next->md->md1;
479 } while (!(flags & (STP | OWN | ENP | MDERR)));
482 int unit = ifp->if_dunit;
483 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in receive ring -- Resetting\n", unit);
490 * Looked ahead into a packet still
493 sc->recv_next = start_of_packet;
496 int unit = ifp->if_dunit;
497 log(LOG_ERR, "lnc%d: End of received packet not found-- Resetting\n", unit);
504 pkt_len = (next->md->md3 & MCNT) - FCS_LEN;
506 /* Move pointer onto start of next packet */
507 INC_MD_PTR(sc->recv_next, sc->nrdre)
508 next = sc->recv_ring + sc->recv_next;
511 int unit = ifp->if_dunit;
514 log(LOG_ERR, "lnc%d: Receive buffer error\n", unit);
517 /* OFLO only valid if ENP is not set */
518 if (!(flags & ENP)) {
520 log(LOG_ERR, "lnc%d: Receive overflow error \n", unit);
522 } else if (flags & ENP) {
523 if ((ifp->if_flags & IFF_PROMISC)==0) {
525 * FRAM and CRC are valid only if ENP
526 * is set and OFLO is not.
530 log(LOG_ERR, "lnc%d: Framing error\n", unit);
532 * FRAM is only set if there's a CRC
533 * error so avoid multiple messages
535 } else if (flags & CRC) {
537 log(LOG_ERR, "lnc%d: Receive CRC error\n", unit);
545 while (start_of_packet != sc->recv_next) {
546 start = sc->recv_ring + start_of_packet;
547 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
548 start->md->md1 &= HADR;
549 start->md->md1 |= OWN;
550 INC_MD_PTR(start_of_packet, sc->nrdre)
552 } else { /* Valid packet */
557 if (sc->nic.mem_mode == DMA_MBUF)
558 head = chain_mbufs(sc, start_of_packet, pkt_len);
560 head = mbuf_packet(sc, start_of_packet, pkt_len);
564 * First mbuf in packet holds the
565 * ethernet and packet headers
567 head->m_pkthdr.rcvif = ifp;
568 head->m_pkthdr.len = pkt_len ;
569 eh = mtod(head, struct ether_header *);
572 * vmware ethernet hardware emulation loops
573 * packets back to itself, violates IFF_SIMPLEX.
574 * drop it if it is from myself.
576 if (bcmp(eh->ether_shost,
577 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) == 0) {
580 ifp->if_input(ifp, head);
583 int unit = ifp->if_dunit;
584 log(LOG_ERR,"lnc%d: Packet dropped, no mbufs\n",unit);
585 LNCSTATS(drop_packet)
593 * At this point all completely received packets have been processed
594 * so clear RINT since any packets that have arrived while we were in
595 * here have been dealt with.
598 lnc_outw(sc->rdp, RINT | INEA);
602 lnc_tint(struct lnc_softc *sc)
604 struct host_ring_entry *next, *start;
609 * If the driver is reset in this routine then we return immediately to
610 * the interrupt driver routine. Any interrupts that have occured
611 * since the reset will be dealt with there. sc->trans_next
612 * should point to the start of the first packet that was awaiting
613 * transmission after the last transmit interrupt was dealt with. The
614 * LANCE should have relinquished ownership of that descriptor before
615 * the interrupt. Therefore, sc->trans_next should point to a
616 * descriptor with STP set and OWN cleared. If not then the driver's
617 * pointers are out of sync with the LANCE, which signifies a bug in
618 * the driver. Therefore, the following two checks are really
619 * diagnostic, since if the driver is working correctly they should
624 if ((sc->trans_ring + sc->trans_next)->md->md1 & OWN) {
625 int unit = sc->arpcom.ac_if.if_dunit;
626 log(LOG_ERR, "lnc%d: Transmit interrupt with buffer still owned by controller -- Resetting\n", unit);
634 * The LANCE will write the status information for the packet it just
635 * tried to transmit in one of two places. If the packet was
636 * transmitted successfully then the status will be written into the
637 * last descriptor of the packet. If the transmit failed then the
638 * status will be written into the descriptor that was being accessed
639 * when the error occured and all subsequent descriptors in that
640 * packet will have been relinquished by the LANCE.
642 * At this point we know that sc->trans_next points to the start
643 * of a packet that the LANCE has just finished trying to transmit.
644 * We now search for a buffer with either ENP or ERR set.
650 start_of_packet = sc->trans_next;
651 next = sc->trans_ring + sc->trans_next;
654 if (!(next->md->md1 & STP)) {
655 int unit = sc->arpcom.ac_if.if_dunit;
656 log(LOG_ERR, "lnc%d: Transmit interrupt but not start of packet -- Resetting\n", unit);
663 * Find end of packet.
666 if (!(next->md->md1 & (ENP | MDERR))) {
668 INC_MD_PTR(sc->trans_next, sc->ntdre)
669 next = sc->trans_ring + sc->trans_next;
670 } while (!(next->md->md1 & (STP | OWN | ENP | MDERR)));
672 if (next->md->md1 & STP) {
673 int unit = sc->arpcom.ac_if.if_dunit;
674 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in transmit ring -- Resetting\n", unit);
678 if (next->md->md1 & OWN) {
681 * Looked ahead into a packet still
684 sc->trans_next = start_of_packet;
687 int unit = sc->arpcom.ac_if.if_dunit;
688 log(LOG_ERR, "lnc%d: End of transmitted packet not found -- Resetting\n", unit);
695 * Check for ERR first since other flags are irrelevant if an
698 if (next->md->md1 & MDERR) {
700 int unit = sc->arpcom.ac_if.if_dunit;
703 sc->arpcom.ac_if.if_oerrors++;
705 if (next->md->md3 & LCOL) {
707 log(LOG_ERR, "lnc%d: Transmit late collision -- Net error?\n", unit);
708 sc->arpcom.ac_if.if_collisions++;
710 * Clear TBUFF since it's not valid when LCOL
713 next->md->md3 &= ~TBUFF;
715 if (next->md->md3 & LCAR) {
717 log(LOG_ERR, "lnc%d: Loss of carrier during transmit -- Net error?\n", unit);
719 if (next->md->md3 & RTRY) {
721 log(LOG_ERR, "lnc%d: Transmit of packet failed after 16 attempts -- TDR = %d\n", unit, ((sc->trans_ring + sc->trans_next)->md->md3 & TDR));
722 sc->arpcom.ac_if.if_collisions += 16;
724 * Clear TBUFF since it's not valid when RTRY
727 next->md->md3 &= ~TBUFF;
730 * TBUFF is only valid if neither LCOL nor RTRY are set.
731 * We need to check UFLO after LCOL and RTRY so that we
732 * know whether or not TBUFF is valid. If either are
733 * set then TBUFF will have been cleared above. A
734 * UFLO error will turn off the transmitter so we
739 if (next->md->md3 & UFLO) {
742 * If an UFLO has occured it's possibly due
745 if (next->md->md3 & TBUFF) {
747 log(LOG_ERR, "lnc%d: Transmit buffer error -- Resetting\n", unit);
749 log(LOG_ERR, "lnc%d: Transmit underflow error -- Resetting\n", unit);
754 INC_MD_PTR(sc->trans_next, sc->ntdre)
755 next = sc->trans_ring + sc->trans_next;
756 } while (!(next->md->md1 & STP) && (sc->trans_next != sc->next_to_send));
760 * Since we check for ERR first then if we get here
761 * the packet was transmitted correctly. There may
762 * still have been non-fatal errors though.
763 * Don't bother checking for DEF, waste of time.
766 sc->arpcom.ac_if.if_opackets++;
768 if (next->md->md1 & MORE) {
770 sc->arpcom.ac_if.if_collisions += 2;
774 * ONE is invalid if LCOL is set. If LCOL was set then
775 * ERR would have also been set and we would have
776 * returned from lnc_tint above. Therefore we can
777 * assume if we arrive here that ONE is valid.
781 if (next->md->md1 & ONE) {
783 sc->arpcom.ac_if.if_collisions++;
785 INC_MD_PTR(sc->trans_next, sc->ntdre)
786 next = sc->trans_ring + sc->trans_next;
790 * Clear descriptors and free any mbufs.
794 start = sc->trans_ring + start_of_packet;
795 start->md->md1 &= HADR;
796 if (sc->nic.mem_mode == DMA_MBUF) {
797 /* Cache clusters on a local queue */
798 if ((start->buff.mbuf->m_flags & M_EXT) && (sc->mbuf_count < MBUF_CACHE_LIMIT)) {
799 if (sc->mbuf_count) {
800 start->buff.mbuf->m_next = sc->mbufs;
801 sc->mbufs = start->buff.mbuf;
803 sc->mbufs = start->buff.mbuf;
805 start->buff.mbuf = 0;
807 m_free(start->buff.mbuf);
808 start->buff.mbuf = NULL;
811 sc->pending_transmits--;
812 INC_MD_PTR(start_of_packet, sc->ntdre)
813 }while (start_of_packet != sc->trans_next);
816 * There's now at least one free descriptor
817 * in the ring so indicate that we can accept
818 * more packets again.
821 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
825 } while (sc->pending_transmits && !(next->md->md1 & OWN));
828 * Clear TINT since we've dealt with all
829 * the completed transmissions.
832 lnc_outw(sc->rdp, TINT | INEA);
836 lnc_attach_common(device_t dev)
838 lnc_softc_t *sc = device_get_softc(dev);
839 struct ifnet *ifp = &sc->arpcom.ac_if;
843 switch (sc->nic.ident) {
853 /* Set default mode */
854 sc->nic.mode = NORMAL;
856 /* Fill in arpcom structure entries */
859 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
860 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
862 ifp->if_start = lnc_start;
863 ifp->if_ioctl = lnc_ioctl;
864 ifp->if_watchdog = lnc_watchdog;
865 ifp->if_init = lnc_init;
866 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
867 ifq_set_ready(&ifp->if_snd);
869 /* Extract MAC address from PROM */
870 for (i = 0; i < ETHER_ADDR_LEN; i++)
871 sc->arpcom.ac_enaddr[i] = lnc_inb(i * skip);
874 * XXX -- should check return status of if_attach
877 ether_ifattach(&sc->arpcom.ac_if, sc->arpcom.ac_enaddr, NULL);
879 if (sc->nic.ic == LANCE || sc->nic.ic == C_LANCE)
880 if_printf(ifp, "%s (%s)", nic_ident[sc->nic.ident],
881 ic_ident[sc->nic.ic]);
883 if_printf(ifp, "%s\n", ic_ident[sc->nic.ic]);
891 struct lnc_softc *sc = xsc;
897 /* Shut down interface */
899 sc->arpcom.ac_if.if_flags |= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; /* XXX??? */
902 * This sets up the memory area for the controller. Memory is set up for
903 * the initialisation block (12 words of contiguous memory starting
904 * on a word boundary),the transmit and receive ring structures (each
905 * entry is 4 words long and must start on a quadword boundary) and
908 * The alignment tests are particularly paranoid.
912 sc->trans_ring = sc->recv_ring + NDESC(sc->nrdre);
915 if (sc->nic.mem_mode == SHMEM)
916 lnc_mem = (char *) sc->nic.iobase;
918 lnc_mem = (char *) (sc->trans_ring + NDESC(sc->ntdre));
920 lnc_mem = (char *)(((int)lnc_mem + 1) & ~1);
921 sc->init_block = (struct init_block *) ((int) lnc_mem & ~1);
922 lnc_mem = (char *) (sc->init_block + 1);
923 lnc_mem = (char *)(((int)lnc_mem + 7) & ~7);
925 /* Initialise pointers to descriptor entries */
926 for (i = 0; i < NDESC(sc->nrdre); i++) {
927 (sc->recv_ring + i)->md = (struct mds *) lnc_mem;
928 lnc_mem += sizeof(struct mds);
930 for (i = 0; i < NDESC(sc->ntdre); i++) {
931 (sc->trans_ring + i)->md = (struct mds *) lnc_mem;
932 lnc_mem += sizeof(struct mds);
935 /* Initialise the remaining ring entries */
937 if (sc->nic.mem_mode == DMA_MBUF) {
942 /* Free previously allocated mbufs */
943 if (sc->flags & LNC_INITIALISED)
947 for (i = 0; i < NDESC(sc->nrdre); i++) {
948 if (alloc_mbuf_cluster(sc, sc->recv_ring+i)) {
949 log(LOG_ERR, "Initialisation failed -- no mbufs\n");
955 for (i = 0; i < NDESC(sc->ntdre); i++) {
956 (sc->trans_ring + i)->buff.mbuf = 0;
957 (sc->trans_ring + i)->md->md0 = 0;
958 (sc->trans_ring + i)->md->md1 = 0;
959 (sc->trans_ring + i)->md->md2 = 0;
960 (sc->trans_ring + i)->md->md3 = 0;
963 for (i = 0; i < NDESC(sc->nrdre); i++) {
964 (sc->recv_ring + i)->md->md0 = kvtop(lnc_mem);
965 (sc->recv_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff) | OWN;
966 (sc->recv_ring + i)->md->md2 = -RECVBUFSIZE;
967 (sc->recv_ring + i)->md->md3 = 0;
968 (sc->recv_ring + i)->buff.data = lnc_mem;
969 lnc_mem += RECVBUFSIZE;
971 for (i = 0; i < NDESC(sc->ntdre); i++) {
972 (sc->trans_ring + i)->md->md0 = kvtop(lnc_mem);
973 (sc->trans_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff);
974 (sc->trans_ring + i)->md->md2 = 0;
975 (sc->trans_ring + i)->md->md3 = 0;
976 (sc->trans_ring + i)->buff.data = lnc_mem;
977 lnc_mem += TRANSBUFSIZE;
981 sc->next_to_send = 0;
983 /* Set up initialisation block */
985 sc->init_block->mode = sc->nic.mode;
987 for (i = 0; i < ETHER_ADDR_LEN; i++)
988 sc->init_block->padr[i] = sc->arpcom.ac_enaddr[i];
992 sc->init_block->rdra = kvtop(sc->recv_ring->md);
993 sc->init_block->rlen = ((kvtop(sc->recv_ring->md) >> 16) & 0xff) | (sc->nrdre << 13);
994 sc->init_block->tdra = kvtop(sc->trans_ring->md);
995 sc->init_block->tlen = ((kvtop(sc->trans_ring->md) >> 16) & 0xff) | (sc->ntdre << 13);
998 /* Set flags to show that the memory area is valid */
999 sc->flags |= LNC_INITIALISED;
1001 sc->pending_transmits = 0;
1003 /* Give the LANCE the physical address of the initialisation block */
1005 if (sc->nic.ic == PCnet_Home) {
1007 /* Set PHY_SEL to HomeRun */
1008 media = read_bcr(sc, BCR49);
1011 write_bcr(sc, BCR49, media);
1014 write_csr(sc, CSR1, kvtop(sc->init_block));
1015 write_csr(sc, CSR2, (kvtop(sc->init_block) >> 16) & 0xff);
1018 * Depending on which controller this is, CSR3 has different meanings.
1019 * For the Am7990 it controls DMA operations, for the Am79C960 it
1020 * controls interrupt masks and transmitter algorithms. In either
1021 * case, none of the flags are set.
1025 write_csr(sc, CSR3, 0);
1027 /* Let's see if it starts */
1029 printf("Enabling lnc interrupts\n");
1030 sc->arpcom.ac_if.if_timer = 10;
1031 write_csr(sc, CSR0, INIT|INEA);
1035 * Now that the initialisation is complete there's no reason to
1036 * access anything except CSR0, so we leave RAP pointing there
1037 * so we can just access RDP from now on, saving an outw each
1041 write_csr(sc, CSR0, INIT);
1042 for(i=0; i < 1000; i++)
1043 if (read_csr(sc, CSR0) & IDON)
1046 if (read_csr(sc, CSR0) & IDON) {
1048 * Enable interrupts, start the LANCE, mark the interface as
1049 * running and transmit any pending packets.
1051 write_csr(sc, CSR0, STRT | INEA);
1052 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1053 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1054 lnc_start(&sc->arpcom.ac_if);
1056 log(LOG_ERR, "%s: Initialisation failed\n",
1057 sc->arpcom.ac_if.if_xname);
1063 * The interrupt flag (INTR) will be set and provided that the interrupt enable
1064 * flag (INEA) is also set, the interrupt pin will be driven low when any of
1065 * the following occur:
1067 * 1) Completion of the initialisation routine (IDON). 2) The reception of a
1068 * packet (RINT). 3) The transmission of a packet (TINT). 4) A transmitter
1069 * timeout error (BABL). 5) A missed packet (MISS). 6) A memory error (MERR).
1071 * The interrupt flag is cleared when all of the above conditions are cleared.
1073 * If the driver is reset from this routine then it first checks to see if any
1074 * interrupts have ocurred since the reset and handles them before returning.
1075 * This is because the NIC may signify a pending interrupt in CSR0 using the
1076 * INTR flag even if a hardware interrupt is currently inhibited (at least I
1077 * think it does from reading the data sheets). We may as well deal with
1078 * these pending interrupts now rather than get the overhead of another
1079 * hardware interrupt immediately upon returning from the interrupt handler.
1086 lnc_softc_t *sc = arg;
1087 int unit = sc->arpcom.ac_if.if_dunit;
1091 * INEA is the only bit that can be cleared by writing a 0 to it so
1092 * we have to include it in any writes that clear other flags.
1095 while ((csr0 = lnc_inw(sc->rdp)) & INTR) {
1098 * Clear interrupt flags early to avoid race conditions. The
1099 * controller can still set these flags even while we're in
1100 * this interrupt routine. If the flag is still set from the
1101 * event that caused this interrupt any new events will
1105 lnc_outw(sc->rdp, csr0);
1106 /*lnc_outw(sc->rdp, IDON | CERR | BABL | MISS | MERR | RINT | TINT | INEA);*/
1111 sc->arpcom.ac_if.if_timer = 0;
1112 write_csr(sc, CSR0, STRT | INEA);
1113 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1114 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1115 lnc_start(&sc->arpcom.ac_if);
1122 log(LOG_ERR, "lnc%d: Heartbeat error -- SQE test failed\n", unit);
1126 log(LOG_ERR, "lnc%d: Babble error - more than 1519 bytes transmitted\n", unit);
1128 sc->arpcom.ac_if.if_oerrors++;
1131 log(LOG_ERR, "lnc%d: Missed packet -- no receive buffer\n", unit);
1133 sc->arpcom.ac_if.if_ierrors++;
1136 log(LOG_ERR, "lnc%d: Memory error -- Resetting\n", unit);
1148 sc->arpcom.ac_if.if_timer = 0;
1153 * If there's room in the transmit descriptor ring then queue
1154 * some more transmit packets.
1157 if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
1158 lnc_start(&sc->arpcom.ac_if);
1163 mbuf_to_buffer(struct mbuf *m, char *buffer)
1168 for( ; m; m = m->m_next) {
1169 bcopy(mtod(m, caddr_t), buffer, m->m_len);
1177 static __inline struct mbuf *
1178 chain_to_cluster(struct mbuf *m)
1182 new = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1185 new->m_len = mbuf_to_buffer(m, new->m_data);
1191 * IFF_OACTIVE and IFF_RUNNING are checked in ether_output so it's redundant
1192 * to check them again since we wouldn't have got here if they were not
1193 * appropriately set. This is also called from lnc_init and lncintr but the
1194 * flags should be ok at those points too.
1198 lnc_start(struct ifnet *ifp)
1201 struct lnc_softc *sc = ifp->if_softc;
1202 struct host_ring_entry *desc;
1205 struct mbuf *head, *m;
1208 int no_entries_needed;
1211 head = ifq_dequeue(&sc->arpcom.ac_if.if_snd, NULL);
1215 if (sc->nic.mem_mode == DMA_MBUF) {
1217 no_entries_needed = 0;
1218 for (m=head; m; m = m->m_next)
1219 no_entries_needed++;
1222 * We try and avoid bcopy as much as possible
1223 * but there are two cases when we use it.
1225 * 1) If there are not enough free entries in the ring
1226 * to hold each mbuf in the chain then compact the
1227 * chain into a single cluster.
1229 * 2) The Am7990 and Am79C90 must not have less than
1230 * 100 bytes in the first descriptor of a chained
1231 * packet so it's necessary to shuffle the mbuf
1232 * contents to ensure this.
1236 if (no_entries_needed > (NDESC(sc->ntdre) - sc->pending_transmits)) {
1237 if (!(head = chain_to_cluster(head))) {
1238 log(LOG_ERR, "%s: Couldn't get mbuf for transmit packet -- Resetting \n ",ifp->if_xname);
1242 } else if ((sc->nic.ic == LANCE) || (sc->nic.ic == C_LANCE)) {
1243 if ((head->m_len < 100) && (head->m_next)) {
1244 len = 100 - head->m_len;
1245 if (M_TRAILINGSPACE(head) < len) {
1247 * Move data to start of data
1248 * area. We assume the first
1249 * mbuf has a packet header
1250 * and is not a cluster.
1252 bcopy((caddr_t)head->m_data, (caddr_t)head->m_pktdat, head->m_len);
1253 head->m_data = head->m_pktdat;
1256 while (m && (len > 0)) {
1257 chunk = min(len, m->m_len);
1258 bcopy(mtod(m, caddr_t), mtod(head, caddr_t) + head->m_len, chunk);
1260 head->m_len += chunk;
1263 if (m->m_len <= 0) {
1271 tmp = sc->next_to_send;
1274 * On entering this loop we know that tmp points to a
1275 * descriptor with a clear OWN bit.
1278 desc = sc->trans_ring + tmp;
1279 len = ETHER_MIN_LEN;
1280 for (m = head; m; m = m->m_next) {
1281 desc->buff.mbuf = m;
1282 addr = kvtop(m->m_data);
1283 desc->md->md0 = addr;
1284 desc->md->md1 = ((addr >> 16) & 0xff);
1286 desc->md->md2 = -m->m_len;
1287 sc->pending_transmits++;
1290 INC_MD_PTR(tmp, sc->ntdre)
1291 desc = sc->trans_ring + tmp;
1294 end_of_packet = tmp;
1295 DEC_MD_PTR(tmp, sc->ntdre)
1296 desc = sc->trans_ring + tmp;
1297 desc->md->md1 |= ENP;
1300 desc->md->md2 -= len;
1303 * Set OWN bits in reverse order, otherwise the Lance
1304 * could start sending the packet before all the
1305 * buffers have been relinquished by the host.
1308 while (tmp != sc->next_to_send) {
1309 desc->md->md1 |= OWN;
1310 DEC_MD_PTR(tmp, sc->ntdre)
1311 desc = sc->trans_ring + tmp;
1313 sc->next_to_send = end_of_packet;
1314 desc->md->md1 |= STP | OWN;
1316 sc->pending_transmits++;
1317 desc = sc->trans_ring + sc->next_to_send;
1318 len = mbuf_to_buffer(head, desc->buff.data);
1320 desc->md->md2 = -max(len, ETHER_MIN_LEN - ETHER_CRC_LEN);
1321 desc->md->md1 |= OWN | STP | ENP;
1322 INC_MD_PTR(sc->next_to_send, sc->ntdre)
1325 /* Force an immediate poll of the transmit ring */
1326 lnc_outw(sc->rdp, TDMD | INEA);
1329 * Set a timer so if the buggy Am7990.h shuts
1330 * down we can wake it up.
1335 BPF_MTAP(&sc->arpcom.ac_if, head);
1337 if (sc->nic.mem_mode != DMA_MBUF)
1340 } while (sc->pending_transmits < NDESC(sc->ntdre));
1343 * Transmit ring is full so set IFF_OACTIVE
1344 * since we can't buffer any more packets.
1347 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
1348 LNCSTATS(trans_ring_full)
1352 lnc_ioctl(struct ifnet * ifp, u_long command, caddr_t data, struct ucred *cr)
1355 struct lnc_softc *sc = ifp->if_softc;
1363 if (ifp->if_flags & IFF_DEBUG)
1368 if (ifp->if_flags & IFF_PROMISC) {
1369 if (!(sc->nic.mode & PROM)) {
1370 sc->nic.mode |= PROM;
1373 } else if (sc->nic.mode & PROM) {
1374 sc->nic.mode &= ~PROM;
1378 if ((ifp->if_flags & IFF_ALLMULTI) &&
1379 !(sc->flags & LNC_ALLMULTI)) {
1380 sc->flags |= LNC_ALLMULTI;
1382 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
1383 (sc->flags & LNC_ALLMULTI)) {
1384 sc->flags &= ~LNC_ALLMULTI;
1388 if ((ifp->if_flags & IFF_UP) == 0 &&
1389 (ifp->if_flags & IFF_RUNNING) != 0) {
1391 * If interface is marked down and it is running,
1395 ifp->if_flags &= ~IFF_RUNNING;
1396 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1397 (ifp->if_flags & IFF_RUNNING) == 0) {
1399 * If interface is marked up and it is stopped, then
1411 error = ether_ioctl(ifp, command, data);
1421 lnc_watchdog(struct ifnet *ifp)
1423 log(LOG_ERR, "%s: Device timeout -- Resetting\n", ifp->if_xname);
1425 lnc_reset(ifp->if_softc);
1430 lnc_dump_state(struct lnc_softc *sc)
1434 printf("\nDriver/NIC [%d] state dump\n", sc->arpcom.ac_if.if_dunit);
1435 printf("Memory access mode: %b\n", sc->nic.mem_mode, MEM_MODES);
1436 printf("Host memory\n");
1437 printf("-----------\n");
1439 printf("Receive ring: base = %p, next = %p\n",
1440 (void *)sc->recv_ring, (void *)(sc->recv_ring + sc->recv_next));
1441 for (i = 0; i < NDESC(sc->nrdre); i++)
1442 printf("\t%d:%p md = %p buff = %p\n",
1443 i, (void *)(sc->recv_ring + i),
1444 (void *)(sc->recv_ring + i)->md,
1445 (void *)(sc->recv_ring + i)->buff.data);
1447 printf("Transmit ring: base = %p, next = %p\n",
1448 (void *)sc->trans_ring, (void *)(sc->trans_ring + sc->trans_next));
1449 for (i = 0; i < NDESC(sc->ntdre); i++)
1450 printf("\t%d:%p md = %p buff = %p\n",
1451 i, (void *)(sc->trans_ring + i),
1452 (void *)(sc->trans_ring + i)->md,
1453 (void *)(sc->trans_ring + i)->buff.data);
1454 printf("Lance memory (may be on host(DMA) or card(SHMEM))\n");
1455 printf("Init block = %p\n", (void *)sc->init_block);
1456 printf("\tmode = %b rlen:rdra = %x:%x tlen:tdra = %x:%x\n",
1457 sc->init_block->mode, INIT_MODE, sc->init_block->rlen,
1458 sc->init_block->rdra, sc->init_block->tlen, sc->init_block->tdra);
1459 printf("Receive descriptor ring\n");
1460 for (i = 0; i < NDESC(sc->nrdre); i++)
1461 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tMCNT = %u,\tflags = %b\n",
1462 i, ((sc->recv_ring + i)->md->md1 & HADR),
1463 (sc->recv_ring + i)->md->md0,
1464 -(short) (sc->recv_ring + i)->md->md2,
1465 (sc->recv_ring + i)->md->md3,
1466 (((sc->recv_ring + i)->md->md1 & ~HADR) >> 8), RECV_MD1);
1467 printf("Transmit descriptor ring\n");
1468 for (i = 0; i < NDESC(sc->ntdre); i++)
1469 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tflags = %b %b\n",
1470 i, ((sc->trans_ring + i)->md->md1 & HADR),
1471 (sc->trans_ring + i)->md->md0,
1472 -(short) (sc->trans_ring + i)->md->md2,
1473 ((sc->trans_ring + i)->md->md1 >> 8), TRANS_MD1,
1474 ((sc->trans_ring + i)->md->md3 >> 10), TRANS_MD3);
1475 printf("\nnext_to_send = %x\n", sc->next_to_send);
1476 printf("\n CSR0 = %b CSR1 = %x CSR2 = %x CSR3 = %x\n\n",
1477 read_csr(sc, CSR0), CSR0_FLAGS, read_csr(sc, CSR1),
1478 read_csr(sc, CSR2), read_csr(sc, CSR3));
1480 /* Set RAP back to CSR0 */
1481 lnc_outw(sc->rap, CSR0);
1485 mbuf_dump_chain(struct mbuf * m)
1488 #define MBUF_FLAGS \
1489 "\20\1M_EXT\2M_PKTHDR\3M_EOR\4UNKNOWN\5M_BCAST\6M_MCAST"
1492 log(LOG_DEBUG, "m == NULL\n");
1494 log(LOG_DEBUG, "m = %p\n", (void *)m);
1495 log(LOG_DEBUG, "m_hdr.mh_next = %p\n",
1496 (void *)m->m_hdr.mh_next);
1497 log(LOG_DEBUG, "m_hdr.mh_nextpkt = %p\n",
1498 (void *)m->m_hdr.mh_nextpkt);
1499 log(LOG_DEBUG, "m_hdr.mh_len = %d\n", m->m_hdr.mh_len);
1500 log(LOG_DEBUG, "m_hdr.mh_data = %p\n",
1501 (void *)m->m_hdr.mh_data);
1502 log(LOG_DEBUG, "m_hdr.mh_type = %d\n", m->m_hdr.mh_type);
1503 log(LOG_DEBUG, "m_hdr.mh_flags = %b\n", m->m_hdr.mh_flags,
1505 if (!(m->m_hdr.mh_flags & (M_PKTHDR | M_EXT)))
1506 log(LOG_DEBUG, "M_dat.M_databuf = %p\n",
1507 (void *)m->M_dat.M_databuf);
1509 if (m->m_hdr.mh_flags & M_PKTHDR) {
1510 log(LOG_DEBUG, "M_dat.MH.MH_pkthdr.len = %d\n",
1511 m->M_dat.MH.MH_pkthdr.len);
1513 "M_dat.MH.MH_pkthdr.rcvif = %p\n",
1514 (void *)m->M_dat.MH.MH_pkthdr.rcvif);
1515 if (!(m->m_hdr.mh_flags & M_EXT))
1517 "M_dat.MH.MH_dat.MH_databuf = %p\n",
1518 (void *)m->M_dat.MH.MH_dat.MH_databuf);
1520 if (m->m_hdr.mh_flags & M_EXT) {
1522 "M_dat.MH.MH_dat.MH_ext.ext_buff %p\n",
1523 (void *)m->M_dat.MH.MH_dat.MH_ext.ext_buf);
1525 "M_dat.MH.MH_dat.MH_ext.ext_free %p\n",
1526 (void *)m->M_dat.MH.MH_dat.MH_ext.ext_free);
1528 "M_dat.MH.MH_dat.MH_ext.ext_size %d\n",
1529 m->M_dat.MH.MH_dat.MH_ext.ext_size);
1532 } while ((m = m->m_next) != NULL);