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.20 2005/06/11 04:26:53 hsu 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>
75 #include <machine/bus.h>
76 #include <machine/resource.h>
79 #include <net/ethernet.h>
81 #include <net/ifq_var.h>
82 #include <net/if_dl.h>
83 #include <net/if_types.h>
85 #include <netinet/in.h>
86 #include <netinet/if_ether.h>
90 #include <machine/md_var.h>
92 #include <dev/netif/lnc/if_lncvar.h>
93 #include <dev/netif/lnc/if_lncreg.h>
95 DECLARE_DUMMY_MODULE(if_lnc);
97 devclass_t lnc_devclass;
99 static char const * const nic_ident[] = {
104 "CNET98S", /* PC-98 */
107 static char const * const ic_ident[] = {
122 static void lnc_setladrf(struct lnc_softc *sc);
123 static void lnc_reset(struct lnc_softc *sc);
124 static void lnc_free_mbufs(struct lnc_softc *sc);
125 static __inline int alloc_mbuf_cluster(struct lnc_softc *sc,
126 struct host_ring_entry *desc);
127 static __inline struct mbuf *chain_mbufs(struct lnc_softc *sc,
130 static __inline struct mbuf *mbuf_packet(struct lnc_softc *sc,
133 static void lnc_rint(struct lnc_softc *sc);
134 static void lnc_tint(struct lnc_softc *sc);
136 static void lnc_init(void *);
137 static __inline int mbuf_to_buffer(struct mbuf *m, char *buffer);
138 static __inline struct mbuf *chain_to_cluster(struct mbuf *m);
139 static void lnc_start(struct ifnet *ifp);
140 static int lnc_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
142 static void lnc_watchdog(struct ifnet *ifp);
144 void lnc_dump_state(struct lnc_softc *sc);
145 void mbuf_dump_chain(struct mbuf *m);
149 read_csr(struct lnc_softc *sc, u_short port)
151 lnc_outw(sc->rap, port);
152 return (lnc_inw(sc->rdp));
156 write_csr(struct lnc_softc *sc, u_short port, u_short val)
158 lnc_outw(sc->rap, port);
159 lnc_outw(sc->rdp, val);
163 write_bcr(struct lnc_softc *sc, u_short port, u_short val)
165 lnc_outw(sc->rap, port);
166 lnc_outw(sc->bdp, val);
169 static __inline u_short
170 read_bcr(struct lnc_softc *sc, u_short port)
172 lnc_outw(sc->rap, port);
173 return (lnc_inw(sc->bdp));
177 lance_probe(struct lnc_softc *sc)
179 write_csr(sc, CSR0, STOP);
181 if ((lnc_inw(sc->rdp) & STOP) && ! (read_csr(sc, CSR3))) {
183 * Check to see if it's a C-LANCE. For the LANCE the INEA bit
184 * cannot be set while the STOP bit is. This restriction is
185 * removed for the C-LANCE.
187 write_csr(sc, CSR0, INEA);
188 if (read_csr(sc, CSR0) & INEA)
196 static __inline u_long
197 ether_crc(const u_char *ether_addr)
199 #define POLYNOMIAL 0xEDB88320UL
201 u_int crc = 0xFFFFFFFFUL;
203 for (i = 0; i < ETHER_ADDR_LEN; i++) {
204 addr = *ether_addr++;
205 for (j = 0; j < MULTICAST_FILTER_LEN; j++) {
206 crc = (crc >> 1) ^ (((crc ^ addr) & 1) ? POLYNOMIAL : 0);
215 lnc_release_resources(device_t dev)
217 lnc_softc_t *sc = device_get_softc(dev);
220 bus_teardown_intr(dev, sc->irqres, sc->intrhand);
221 bus_release_resource(dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
225 bus_release_resource(dev, SYS_RES_IOPORT,
226 sc->portrid, sc->portres);
228 bus_release_resource(dev, SYS_RES_DRQ, sc->drqrid, sc->drqres);
232 bus_dmamap_unload(sc->dmat, sc->dmamap);
233 bus_dmamem_free(sc->dmat, sc->recv_ring, sc->dmamap);
235 bus_dma_tag_destroy(sc->dmat);
240 * Set up the logical address filter for multicast packets
243 lnc_setladrf(struct lnc_softc *sc)
245 struct ifnet *ifp = &sc->arpcom.ac_if;
246 struct ifmultiaddr *ifma;
250 if (sc->flags & IFF_ALLMULTI) {
251 for (i=0; i < MULTICAST_FILTER_LEN; i++)
252 sc->init_block->ladrf[i] = 0xFF;
257 * For each multicast address, calculate a crc for that address and
258 * then use the high order 6 bits of the crc as a hash code where
259 * bits 3-5 select the byte of the address filter and bits 0-2 select
260 * the bit within that byte.
263 bzero(sc->init_block->ladrf, MULTICAST_FILTER_LEN);
264 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
265 if (ifma->ifma_addr->sa_family != AF_LINK)
268 index = ether_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr))
270 sc->init_block->ladrf[index >> 3] |= 1 << (index & 7);
275 lnc_stop(struct lnc_softc *sc)
277 write_csr(sc, CSR0, STOP);
281 lnc_reset(struct lnc_softc *sc)
287 lnc_free_mbufs(struct lnc_softc *sc)
292 * We rely on other routines to keep the buff.mbuf field valid. If
293 * it's not NULL then we assume it points to an allocated mbuf.
296 for (i = 0; i < NDESC(sc->nrdre); i++)
297 if ((sc->recv_ring + i)->buff.mbuf)
298 m_free((sc->recv_ring + i)->buff.mbuf);
300 for (i = 0; i < NDESC(sc->ntdre); i++)
301 if ((sc->trans_ring + i)->buff.mbuf)
302 m_free((sc->trans_ring + i)->buff.mbuf);
309 alloc_mbuf_cluster(struct lnc_softc *sc, struct host_ring_entry *desc)
311 struct mds *md = desc->md;
315 /* Try and get cluster off local cache */
316 if (sc->mbuf_count) {
319 sc->mbufs = m->m_next;
320 /* XXX m->m_data = m->m_ext.ext_buf;*/
322 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
328 addr = kvtop(m->m_data);
330 md->md1= ((addr >> 16) & 0xff) | OWN;
331 md->md2 = -(short)(MCLBYTES - sizeof(struct pkthdr));
336 static __inline struct mbuf *
337 chain_mbufs(struct lnc_softc *sc, int start_of_packet, int pkt_len)
339 struct mbuf *head, *m;
340 struct host_ring_entry *desc;
343 * Turn head into a pkthdr mbuf --
344 * assumes a pkthdr type mbuf was
345 * allocated to the descriptor
349 desc = sc->recv_ring + start_of_packet;
351 head = desc->buff.mbuf;
352 head->m_flags |= M_PKTHDR;
353 bzero(&head->m_pkthdr, sizeof(head->m_pkthdr));
358 m->m_len = min((MCLBYTES - sizeof(struct pkthdr)), pkt_len);
360 if (alloc_mbuf_cluster(sc, desc))
361 return((struct mbuf *)NULL);
362 INC_MD_PTR(start_of_packet, sc->nrdre)
363 desc = sc->recv_ring + start_of_packet;
364 m->m_next = desc->buff.mbuf;
365 } while (start_of_packet != sc->recv_next);
371 static __inline struct mbuf *
372 mbuf_packet(struct lnc_softc *sc, int start_of_packet, int pkt_len)
375 struct host_ring_entry *start;
376 struct mbuf *head,*m,*m_prev;
377 char *data,*mbuf_data;
381 /* Get a pkthdr mbuf for the start of packet */
382 MGETHDR(head, MB_DONTWAIT, MT_DATA);
384 LNCSTATS(drop_packet)
390 start = sc->recv_ring + start_of_packet;
391 /*blen = -(start->md->md2);*/
392 blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
393 data = start->buff.data;
394 mbuf_data = m->m_data;
396 while (start_of_packet != sc->recv_next) {
398 * If the data left fits in a single buffer then set
399 * blen to the size of the data left.
405 * amount is least of data in current ring buffer and
406 * amount of space left in current mbuf.
408 amount = min(blen, M_TRAILINGSPACE(m));
410 /* mbuf must be empty */
412 m = m_getl(pkt_len, MB_DONTWAIT, MT_DATA, 0, NULL);
419 amount = min(blen, M_TRAILINGSPACE(m));
420 mbuf_data = m->m_data;
422 bcopy(data, mbuf_data, amount);
430 start->md->md1 &= HADR;
431 start->md->md1 |= OWN;
432 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
433 INC_MD_PTR(start_of_packet, sc->nrdre)
434 start = sc->recv_ring + start_of_packet;
435 data = start->buff.data;
436 /*blen = -(start->md->md2);*/
437 blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
445 lnc_rint(struct lnc_softc *sc)
447 struct ifnet *ifp = &sc->arpcom.ac_if;
448 struct host_ring_entry *next, *start;
451 struct ether_header *eh;
457 * The LANCE will issue a RINT interrupt when the ownership of the
458 * last buffer of a receive packet has been relinquished by the LANCE.
459 * Therefore, it can be assumed that a complete packet can be found
460 * before hitting buffers that are still owned by the LANCE, if not
461 * then there is a bug in the driver that is causing the descriptors
462 * to get out of sync.
466 if ((sc->recv_ring + sc->recv_next)->md->md1 & OWN) {
467 int unit = ifp->if_dunit;
468 log(LOG_ERR, "lnc%d: Receive interrupt with buffer still owned by controller -- Resetting\n", unit);
472 if (!((sc->recv_ring + sc->recv_next)->md->md1 & STP)) {
473 int unit = ifp->if_dunit;
474 log(LOG_ERR, "lnc%d: Receive interrupt but not start of packet -- Resetting\n", unit);
481 next = sc->recv_ring + sc->recv_next;
482 while ((flags = next->md->md1) & STP) {
484 /* Make a note of the start of the packet */
485 start_of_packet = sc->recv_next;
488 * Find the end of the packet. Even if not data chaining,
489 * jabber packets can overrun into a second descriptor.
490 * If there is no error, then the ENP flag is set in the last
491 * descriptor of the packet. If there is an error then the ERR
492 * flag will be set in the descriptor where the error occured.
493 * Therefore, to find the last buffer of a packet we search for
497 if (!(flags & (ENP | MDERR))) {
499 INC_MD_PTR(sc->recv_next, sc->nrdre)
500 next = sc->recv_ring + sc->recv_next;
501 flags = next->md->md1;
502 } while (!(flags & (STP | OWN | ENP | MDERR)));
505 int unit = ifp->if_dunit;
506 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in receive ring -- Resetting\n", unit);
513 * Looked ahead into a packet still
516 sc->recv_next = start_of_packet;
519 int unit = ifp->if_dunit;
520 log(LOG_ERR, "lnc%d: End of received packet not found-- Resetting\n", unit);
527 pkt_len = (next->md->md3 & MCNT) - FCS_LEN;
529 /* Move pointer onto start of next packet */
530 INC_MD_PTR(sc->recv_next, sc->nrdre)
531 next = sc->recv_ring + sc->recv_next;
534 int unit = ifp->if_dunit;
537 log(LOG_ERR, "lnc%d: Receive buffer error\n", unit);
540 /* OFLO only valid if ENP is not set */
541 if (!(flags & ENP)) {
543 log(LOG_ERR, "lnc%d: Receive overflow error \n", unit);
545 } else if (flags & ENP) {
546 if ((ifp->if_flags & IFF_PROMISC)==0) {
548 * FRAM and CRC are valid only if ENP
549 * is set and OFLO is not.
553 log(LOG_ERR, "lnc%d: Framing error\n", unit);
555 * FRAM is only set if there's a CRC
556 * error so avoid multiple messages
558 } else if (flags & CRC) {
560 log(LOG_ERR, "lnc%d: Receive CRC error\n", unit);
568 while (start_of_packet != sc->recv_next) {
569 start = sc->recv_ring + start_of_packet;
570 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
571 start->md->md1 &= HADR;
572 start->md->md1 |= OWN;
573 INC_MD_PTR(start_of_packet, sc->nrdre)
575 } else { /* Valid packet */
580 if (sc->nic.mem_mode == DMA_MBUF)
581 head = chain_mbufs(sc, start_of_packet, pkt_len);
583 head = mbuf_packet(sc, start_of_packet, pkt_len);
587 * First mbuf in packet holds the
588 * ethernet and packet headers
590 head->m_pkthdr.rcvif = ifp;
591 head->m_pkthdr.len = pkt_len ;
592 eh = mtod(head, struct ether_header *);
595 * vmware ethernet hardware emulation loops
596 * packets back to itself, violates IFF_SIMPLEX.
597 * drop it if it is from myself.
599 if (bcmp(eh->ether_shost,
600 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) == 0) {
603 (ifp->if_input)(ifp, head);
606 int unit = ifp->if_dunit;
607 log(LOG_ERR,"lnc%d: Packet dropped, no mbufs\n",unit);
608 LNCSTATS(drop_packet)
616 * At this point all completely received packets have been processed
617 * so clear RINT since any packets that have arrived while we were in
618 * here have been dealt with.
621 lnc_outw(sc->rdp, RINT | INEA);
625 lnc_tint(struct lnc_softc *sc)
627 struct host_ring_entry *next, *start;
632 * If the driver is reset in this routine then we return immediately to
633 * the interrupt driver routine. Any interrupts that have occured
634 * since the reset will be dealt with there. sc->trans_next
635 * should point to the start of the first packet that was awaiting
636 * transmission after the last transmit interrupt was dealt with. The
637 * LANCE should have relinquished ownership of that descriptor before
638 * the interrupt. Therefore, sc->trans_next should point to a
639 * descriptor with STP set and OWN cleared. If not then the driver's
640 * pointers are out of sync with the LANCE, which signifies a bug in
641 * the driver. Therefore, the following two checks are really
642 * diagnostic, since if the driver is working correctly they should
647 if ((sc->trans_ring + sc->trans_next)->md->md1 & OWN) {
648 int unit = sc->arpcom.ac_if.if_dunit;
649 log(LOG_ERR, "lnc%d: Transmit interrupt with buffer still owned by controller -- Resetting\n", unit);
657 * The LANCE will write the status information for the packet it just
658 * tried to transmit in one of two places. If the packet was
659 * transmitted successfully then the status will be written into the
660 * last descriptor of the packet. If the transmit failed then the
661 * status will be written into the descriptor that was being accessed
662 * when the error occured and all subsequent descriptors in that
663 * packet will have been relinquished by the LANCE.
665 * At this point we know that sc->trans_next points to the start
666 * of a packet that the LANCE has just finished trying to transmit.
667 * We now search for a buffer with either ENP or ERR set.
673 start_of_packet = sc->trans_next;
674 next = sc->trans_ring + sc->trans_next;
677 if (!(next->md->md1 & STP)) {
678 int unit = sc->arpcom.ac_if.if_dunit;
679 log(LOG_ERR, "lnc%d: Transmit interrupt but not start of packet -- Resetting\n", unit);
686 * Find end of packet.
689 if (!(next->md->md1 & (ENP | MDERR))) {
691 INC_MD_PTR(sc->trans_next, sc->ntdre)
692 next = sc->trans_ring + sc->trans_next;
693 } while (!(next->md->md1 & (STP | OWN | ENP | MDERR)));
695 if (next->md->md1 & STP) {
696 int unit = sc->arpcom.ac_if.if_dunit;
697 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in transmit ring -- Resetting\n", unit);
701 if (next->md->md1 & OWN) {
704 * Looked ahead into a packet still
707 sc->trans_next = start_of_packet;
710 int unit = sc->arpcom.ac_if.if_dunit;
711 log(LOG_ERR, "lnc%d: End of transmitted packet not found -- Resetting\n", unit);
718 * Check for ERR first since other flags are irrelevant if an
721 if (next->md->md1 & MDERR) {
723 int unit = sc->arpcom.ac_if.if_dunit;
726 sc->arpcom.ac_if.if_oerrors++;
728 if (next->md->md3 & LCOL) {
730 log(LOG_ERR, "lnc%d: Transmit late collision -- Net error?\n", unit);
731 sc->arpcom.ac_if.if_collisions++;
733 * Clear TBUFF since it's not valid when LCOL
736 next->md->md3 &= ~TBUFF;
738 if (next->md->md3 & LCAR) {
740 log(LOG_ERR, "lnc%d: Loss of carrier during transmit -- Net error?\n", unit);
742 if (next->md->md3 & RTRY) {
744 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));
745 sc->arpcom.ac_if.if_collisions += 16;
747 * Clear TBUFF since it's not valid when RTRY
750 next->md->md3 &= ~TBUFF;
753 * TBUFF is only valid if neither LCOL nor RTRY are set.
754 * We need to check UFLO after LCOL and RTRY so that we
755 * know whether or not TBUFF is valid. If either are
756 * set then TBUFF will have been cleared above. A
757 * UFLO error will turn off the transmitter so we
762 if (next->md->md3 & UFLO) {
765 * If an UFLO has occured it's possibly due
768 if (next->md->md3 & TBUFF) {
770 log(LOG_ERR, "lnc%d: Transmit buffer error -- Resetting\n", unit);
772 log(LOG_ERR, "lnc%d: Transmit underflow error -- Resetting\n", unit);
777 INC_MD_PTR(sc->trans_next, sc->ntdre)
778 next = sc->trans_ring + sc->trans_next;
779 } while (!(next->md->md1 & STP) && (sc->trans_next != sc->next_to_send));
783 * Since we check for ERR first then if we get here
784 * the packet was transmitted correctly. There may
785 * still have been non-fatal errors though.
786 * Don't bother checking for DEF, waste of time.
789 sc->arpcom.ac_if.if_opackets++;
791 if (next->md->md1 & MORE) {
793 sc->arpcom.ac_if.if_collisions += 2;
797 * ONE is invalid if LCOL is set. If LCOL was set then
798 * ERR would have also been set and we would have
799 * returned from lnc_tint above. Therefore we can
800 * assume if we arrive here that ONE is valid.
804 if (next->md->md1 & ONE) {
806 sc->arpcom.ac_if.if_collisions++;
808 INC_MD_PTR(sc->trans_next, sc->ntdre)
809 next = sc->trans_ring + sc->trans_next;
813 * Clear descriptors and free any mbufs.
817 start = sc->trans_ring + start_of_packet;
818 start->md->md1 &= HADR;
819 if (sc->nic.mem_mode == DMA_MBUF) {
820 /* Cache clusters on a local queue */
821 if ((start->buff.mbuf->m_flags & M_EXT) && (sc->mbuf_count < MBUF_CACHE_LIMIT)) {
822 if (sc->mbuf_count) {
823 start->buff.mbuf->m_next = sc->mbufs;
824 sc->mbufs = start->buff.mbuf;
826 sc->mbufs = start->buff.mbuf;
828 start->buff.mbuf = 0;
830 m_free(start->buff.mbuf);
831 start->buff.mbuf = NULL;
834 sc->pending_transmits--;
835 INC_MD_PTR(start_of_packet, sc->ntdre)
836 }while (start_of_packet != sc->trans_next);
839 * There's now at least one free descriptor
840 * in the ring so indicate that we can accept
841 * more packets again.
844 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
848 } while (sc->pending_transmits && !(next->md->md1 & OWN));
851 * Clear TINT since we've dealt with all
852 * the completed transmissions.
855 lnc_outw(sc->rdp, TINT | INEA);
859 lnc_attach_common(device_t dev)
861 lnc_softc_t *sc = device_get_softc(dev);
862 struct ifnet *ifp = &sc->arpcom.ac_if;
866 switch (sc->nic.ident) {
876 /* Set default mode */
877 sc->nic.mode = NORMAL;
879 /* Fill in arpcom structure entries */
882 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
883 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
885 ifp->if_start = lnc_start;
886 ifp->if_ioctl = lnc_ioctl;
887 ifp->if_watchdog = lnc_watchdog;
888 ifp->if_init = lnc_init;
889 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
890 ifq_set_ready(&ifp->if_snd);
892 /* Extract MAC address from PROM */
893 for (i = 0; i < ETHER_ADDR_LEN; i++)
894 sc->arpcom.ac_enaddr[i] = lnc_inb(i * skip);
897 * XXX -- should check return status of if_attach
900 ether_ifattach(&sc->arpcom.ac_if, sc->arpcom.ac_enaddr);
902 if (sc->nic.ic == LANCE || sc->nic.ic == C_LANCE)
903 if_printf(ifp, "%s (%s)", nic_ident[sc->nic.ident],
904 ic_ident[sc->nic.ic]);
906 if_printf(ifp, "%s\n", ic_ident[sc->nic.ic]);
915 struct lnc_softc *sc = xsc;
919 /* Check that interface has valid address */
921 if (TAILQ_EMPTY(&sc->arpcom.ac_if.if_addrhead)) { /* XXX unlikely */
922 printf("XXX no address?\n");
926 /* Shut down interface */
930 sc->arpcom.ac_if.if_flags |= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; /* XXX??? */
933 * This sets up the memory area for the controller. Memory is set up for
934 * the initialisation block (12 words of contiguous memory starting
935 * on a word boundary),the transmit and receive ring structures (each
936 * entry is 4 words long and must start on a quadword boundary) and
939 * The alignment tests are particularly paranoid.
943 sc->trans_ring = sc->recv_ring + NDESC(sc->nrdre);
946 if (sc->nic.mem_mode == SHMEM)
947 lnc_mem = (char *) sc->nic.iobase;
949 lnc_mem = (char *) (sc->trans_ring + NDESC(sc->ntdre));
951 lnc_mem = (char *)(((int)lnc_mem + 1) & ~1);
952 sc->init_block = (struct init_block *) ((int) lnc_mem & ~1);
953 lnc_mem = (char *) (sc->init_block + 1);
954 lnc_mem = (char *)(((int)lnc_mem + 7) & ~7);
956 /* Initialise pointers to descriptor entries */
957 for (i = 0; i < NDESC(sc->nrdre); i++) {
958 (sc->recv_ring + i)->md = (struct mds *) lnc_mem;
959 lnc_mem += sizeof(struct mds);
961 for (i = 0; i < NDESC(sc->ntdre); i++) {
962 (sc->trans_ring + i)->md = (struct mds *) lnc_mem;
963 lnc_mem += sizeof(struct mds);
966 /* Initialise the remaining ring entries */
968 if (sc->nic.mem_mode == DMA_MBUF) {
973 /* Free previously allocated mbufs */
974 if (sc->flags & LNC_INITIALISED)
978 for (i = 0; i < NDESC(sc->nrdre); i++) {
979 if (alloc_mbuf_cluster(sc, sc->recv_ring+i)) {
980 log(LOG_ERR, "Initialisation failed -- no mbufs\n");
986 for (i = 0; i < NDESC(sc->ntdre); i++) {
987 (sc->trans_ring + i)->buff.mbuf = 0;
988 (sc->trans_ring + i)->md->md0 = 0;
989 (sc->trans_ring + i)->md->md1 = 0;
990 (sc->trans_ring + i)->md->md2 = 0;
991 (sc->trans_ring + i)->md->md3 = 0;
994 for (i = 0; i < NDESC(sc->nrdre); i++) {
995 (sc->recv_ring + i)->md->md0 = kvtop(lnc_mem);
996 (sc->recv_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff) | OWN;
997 (sc->recv_ring + i)->md->md2 = -RECVBUFSIZE;
998 (sc->recv_ring + i)->md->md3 = 0;
999 (sc->recv_ring + i)->buff.data = lnc_mem;
1000 lnc_mem += RECVBUFSIZE;
1002 for (i = 0; i < NDESC(sc->ntdre); i++) {
1003 (sc->trans_ring + i)->md->md0 = kvtop(lnc_mem);
1004 (sc->trans_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff);
1005 (sc->trans_ring + i)->md->md2 = 0;
1006 (sc->trans_ring + i)->md->md3 = 0;
1007 (sc->trans_ring + i)->buff.data = lnc_mem;
1008 lnc_mem += TRANSBUFSIZE;
1012 sc->next_to_send = 0;
1014 /* Set up initialisation block */
1016 sc->init_block->mode = sc->nic.mode;
1018 for (i = 0; i < ETHER_ADDR_LEN; i++)
1019 sc->init_block->padr[i] = sc->arpcom.ac_enaddr[i];
1023 sc->init_block->rdra = kvtop(sc->recv_ring->md);
1024 sc->init_block->rlen = ((kvtop(sc->recv_ring->md) >> 16) & 0xff) | (sc->nrdre << 13);
1025 sc->init_block->tdra = kvtop(sc->trans_ring->md);
1026 sc->init_block->tlen = ((kvtop(sc->trans_ring->md) >> 16) & 0xff) | (sc->ntdre << 13);
1029 /* Set flags to show that the memory area is valid */
1030 sc->flags |= LNC_INITIALISED;
1032 sc->pending_transmits = 0;
1034 /* Give the LANCE the physical address of the initialisation block */
1036 if (sc->nic.ic == PCnet_Home) {
1038 /* Set PHY_SEL to HomeRun */
1039 media = read_bcr(sc, BCR49);
1042 write_bcr(sc, BCR49, media);
1045 write_csr(sc, CSR1, kvtop(sc->init_block));
1046 write_csr(sc, CSR2, (kvtop(sc->init_block) >> 16) & 0xff);
1049 * Depending on which controller this is, CSR3 has different meanings.
1050 * For the Am7990 it controls DMA operations, for the Am79C960 it
1051 * controls interrupt masks and transmitter algorithms. In either
1052 * case, none of the flags are set.
1056 write_csr(sc, CSR3, 0);
1058 /* Let's see if it starts */
1060 printf("Enabling lnc interrupts\n");
1061 sc->arpcom.ac_if.if_timer = 10;
1062 write_csr(sc, CSR0, INIT|INEA);
1066 * Now that the initialisation is complete there's no reason to
1067 * access anything except CSR0, so we leave RAP pointing there
1068 * so we can just access RDP from now on, saving an outw each
1072 write_csr(sc, CSR0, INIT);
1073 for(i=0; i < 1000; i++)
1074 if (read_csr(sc, CSR0) & IDON)
1077 if (read_csr(sc, CSR0) & IDON) {
1079 * Enable interrupts, start the LANCE, mark the interface as
1080 * running and transmit any pending packets.
1082 write_csr(sc, CSR0, STRT | INEA);
1083 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1084 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1085 lnc_start(&sc->arpcom.ac_if);
1087 log(LOG_ERR, "%s: Initialisation failed\n",
1088 sc->arpcom.ac_if.if_xname);
1094 * The interrupt flag (INTR) will be set and provided that the interrupt enable
1095 * flag (INEA) is also set, the interrupt pin will be driven low when any of
1096 * the following occur:
1098 * 1) Completion of the initialisation routine (IDON). 2) The reception of a
1099 * packet (RINT). 3) The transmission of a packet (TINT). 4) A transmitter
1100 * timeout error (BABL). 5) A missed packet (MISS). 6) A memory error (MERR).
1102 * The interrupt flag is cleared when all of the above conditions are cleared.
1104 * If the driver is reset from this routine then it first checks to see if any
1105 * interrupts have ocurred since the reset and handles them before returning.
1106 * This is because the NIC may signify a pending interrupt in CSR0 using the
1107 * INTR flag even if a hardware interrupt is currently inhibited (at least I
1108 * think it does from reading the data sheets). We may as well deal with
1109 * these pending interrupts now rather than get the overhead of another
1110 * hardware interrupt immediately upon returning from the interrupt handler.
1117 lnc_softc_t *sc = arg;
1118 int unit = sc->arpcom.ac_if.if_dunit;
1122 * INEA is the only bit that can be cleared by writing a 0 to it so
1123 * we have to include it in any writes that clear other flags.
1126 while ((csr0 = lnc_inw(sc->rdp)) & INTR) {
1129 * Clear interrupt flags early to avoid race conditions. The
1130 * controller can still set these flags even while we're in
1131 * this interrupt routine. If the flag is still set from the
1132 * event that caused this interrupt any new events will
1136 lnc_outw(sc->rdp, csr0);
1137 /*lnc_outw(sc->rdp, IDON | CERR | BABL | MISS | MERR | RINT | TINT | INEA);*/
1142 sc->arpcom.ac_if.if_timer = 0;
1143 write_csr(sc, CSR0, STRT | INEA);
1144 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1145 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1146 lnc_start(&sc->arpcom.ac_if);
1153 log(LOG_ERR, "lnc%d: Heartbeat error -- SQE test failed\n", unit);
1157 log(LOG_ERR, "lnc%d: Babble error - more than 1519 bytes transmitted\n", unit);
1159 sc->arpcom.ac_if.if_oerrors++;
1162 log(LOG_ERR, "lnc%d: Missed packet -- no receive buffer\n", unit);
1164 sc->arpcom.ac_if.if_ierrors++;
1167 log(LOG_ERR, "lnc%d: Memory error -- Resetting\n", unit);
1179 sc->arpcom.ac_if.if_timer = 0;
1184 * If there's room in the transmit descriptor ring then queue
1185 * some more transmit packets.
1188 if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
1189 lnc_start(&sc->arpcom.ac_if);
1194 mbuf_to_buffer(struct mbuf *m, char *buffer)
1199 for( ; m; m = m->m_next) {
1200 bcopy(mtod(m, caddr_t), buffer, m->m_len);
1208 static __inline struct mbuf *
1209 chain_to_cluster(struct mbuf *m)
1213 new = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1216 new->m_len = mbuf_to_buffer(m, new->m_data);
1222 * IFF_OACTIVE and IFF_RUNNING are checked in ether_output so it's redundant
1223 * to check them again since we wouldn't have got here if they were not
1224 * appropriately set. This is also called from lnc_init and lncintr but the
1225 * flags should be ok at those points too.
1229 lnc_start(struct ifnet *ifp)
1232 struct lnc_softc *sc = ifp->if_softc;
1233 struct host_ring_entry *desc;
1236 struct mbuf *head, *m;
1239 int no_entries_needed;
1242 head = ifq_dequeue(&sc->arpcom.ac_if.if_snd);
1246 if (sc->nic.mem_mode == DMA_MBUF) {
1248 no_entries_needed = 0;
1249 for (m=head; m; m = m->m_next)
1250 no_entries_needed++;
1253 * We try and avoid bcopy as much as possible
1254 * but there are two cases when we use it.
1256 * 1) If there are not enough free entries in the ring
1257 * to hold each mbuf in the chain then compact the
1258 * chain into a single cluster.
1260 * 2) The Am7990 and Am79C90 must not have less than
1261 * 100 bytes in the first descriptor of a chained
1262 * packet so it's necessary to shuffle the mbuf
1263 * contents to ensure this.
1267 if (no_entries_needed > (NDESC(sc->ntdre) - sc->pending_transmits)) {
1268 if (!(head = chain_to_cluster(head))) {
1269 log(LOG_ERR, "%s: Couldn't get mbuf for transmit packet -- Resetting \n ",ifp->if_xname);
1273 } else if ((sc->nic.ic == LANCE) || (sc->nic.ic == C_LANCE)) {
1274 if ((head->m_len < 100) && (head->m_next)) {
1275 len = 100 - head->m_len;
1276 if (M_TRAILINGSPACE(head) < len) {
1278 * Move data to start of data
1279 * area. We assume the first
1280 * mbuf has a packet header
1281 * and is not a cluster.
1283 bcopy((caddr_t)head->m_data, (caddr_t)head->m_pktdat, head->m_len);
1284 head->m_data = head->m_pktdat;
1287 while (m && (len > 0)) {
1288 chunk = min(len, m->m_len);
1289 bcopy(mtod(m, caddr_t), mtod(head, caddr_t) + head->m_len, chunk);
1291 head->m_len += chunk;
1294 if (m->m_len <= 0) {
1302 tmp = sc->next_to_send;
1305 * On entering this loop we know that tmp points to a
1306 * descriptor with a clear OWN bit.
1309 desc = sc->trans_ring + tmp;
1310 len = ETHER_MIN_LEN;
1311 for (m = head; m; m = m->m_next) {
1312 desc->buff.mbuf = m;
1313 addr = kvtop(m->m_data);
1314 desc->md->md0 = addr;
1315 desc->md->md1 = ((addr >> 16) & 0xff);
1317 desc->md->md2 = -m->m_len;
1318 sc->pending_transmits++;
1321 INC_MD_PTR(tmp, sc->ntdre)
1322 desc = sc->trans_ring + tmp;
1325 end_of_packet = tmp;
1326 DEC_MD_PTR(tmp, sc->ntdre)
1327 desc = sc->trans_ring + tmp;
1328 desc->md->md1 |= ENP;
1331 desc->md->md2 -= len;
1334 * Set OWN bits in reverse order, otherwise the Lance
1335 * could start sending the packet before all the
1336 * buffers have been relinquished by the host.
1339 while (tmp != sc->next_to_send) {
1340 desc->md->md1 |= OWN;
1341 DEC_MD_PTR(tmp, sc->ntdre)
1342 desc = sc->trans_ring + tmp;
1344 sc->next_to_send = end_of_packet;
1345 desc->md->md1 |= STP | OWN;
1347 sc->pending_transmits++;
1348 desc = sc->trans_ring + sc->next_to_send;
1349 len = mbuf_to_buffer(head, desc->buff.data);
1351 desc->md->md2 = -max(len, ETHER_MIN_LEN - ETHER_CRC_LEN);
1352 desc->md->md1 |= OWN | STP | ENP;
1353 INC_MD_PTR(sc->next_to_send, sc->ntdre)
1356 /* Force an immediate poll of the transmit ring */
1357 lnc_outw(sc->rdp, TDMD | INEA);
1360 * Set a timer so if the buggy Am7990.h shuts
1361 * down we can wake it up.
1366 BPF_MTAP(&sc->arpcom.ac_if, head);
1368 if (sc->nic.mem_mode != DMA_MBUF)
1371 } while (sc->pending_transmits < NDESC(sc->ntdre));
1374 * Transmit ring is full so set IFF_OACTIVE
1375 * since we can't buffer any more packets.
1378 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
1379 LNCSTATS(trans_ring_full)
1383 lnc_ioctl(struct ifnet * ifp, u_long command, caddr_t data, struct ucred *cr)
1386 struct lnc_softc *sc = ifp->if_softc;
1394 if (ifp->if_flags & IFF_DEBUG)
1399 if (ifp->if_flags & IFF_PROMISC) {
1400 if (!(sc->nic.mode & PROM)) {
1401 sc->nic.mode |= PROM;
1404 } else if (sc->nic.mode & PROM) {
1405 sc->nic.mode &= ~PROM;
1409 if ((ifp->if_flags & IFF_ALLMULTI) &&
1410 !(sc->flags & LNC_ALLMULTI)) {
1411 sc->flags |= LNC_ALLMULTI;
1413 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
1414 (sc->flags & LNC_ALLMULTI)) {
1415 sc->flags &= ~LNC_ALLMULTI;
1419 if ((ifp->if_flags & IFF_UP) == 0 &&
1420 (ifp->if_flags & IFF_RUNNING) != 0) {
1422 * If interface is marked down and it is running,
1426 ifp->if_flags &= ~IFF_RUNNING;
1427 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1428 (ifp->if_flags & IFF_RUNNING) == 0) {
1430 * If interface is marked up and it is stopped, then
1442 error = ether_ioctl(ifp, command, data);
1450 lnc_watchdog(struct ifnet *ifp)
1452 log(LOG_ERR, "%s: Device timeout -- Resetting\n", ifp->if_xname);
1454 lnc_reset(ifp->if_softc);
1459 lnc_dump_state(struct lnc_softc *sc)
1463 printf("\nDriver/NIC [%d] state dump\n", sc->arpcom.ac_if.if_dunit);
1464 printf("Memory access mode: %b\n", sc->nic.mem_mode, MEM_MODES);
1465 printf("Host memory\n");
1466 printf("-----------\n");
1468 printf("Receive ring: base = %p, next = %p\n",
1469 (void *)sc->recv_ring, (void *)(sc->recv_ring + sc->recv_next));
1470 for (i = 0; i < NDESC(sc->nrdre); i++)
1471 printf("\t%d:%p md = %p buff = %p\n",
1472 i, (void *)(sc->recv_ring + i),
1473 (void *)(sc->recv_ring + i)->md,
1474 (void *)(sc->recv_ring + i)->buff.data);
1476 printf("Transmit ring: base = %p, next = %p\n",
1477 (void *)sc->trans_ring, (void *)(sc->trans_ring + sc->trans_next));
1478 for (i = 0; i < NDESC(sc->ntdre); i++)
1479 printf("\t%d:%p md = %p buff = %p\n",
1480 i, (void *)(sc->trans_ring + i),
1481 (void *)(sc->trans_ring + i)->md,
1482 (void *)(sc->trans_ring + i)->buff.data);
1483 printf("Lance memory (may be on host(DMA) or card(SHMEM))\n");
1484 printf("Init block = %p\n", (void *)sc->init_block);
1485 printf("\tmode = %b rlen:rdra = %x:%x tlen:tdra = %x:%x\n",
1486 sc->init_block->mode, INIT_MODE, sc->init_block->rlen,
1487 sc->init_block->rdra, sc->init_block->tlen, sc->init_block->tdra);
1488 printf("Receive descriptor ring\n");
1489 for (i = 0; i < NDESC(sc->nrdre); i++)
1490 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tMCNT = %u,\tflags = %b\n",
1491 i, ((sc->recv_ring + i)->md->md1 & HADR),
1492 (sc->recv_ring + i)->md->md0,
1493 -(short) (sc->recv_ring + i)->md->md2,
1494 (sc->recv_ring + i)->md->md3,
1495 (((sc->recv_ring + i)->md->md1 & ~HADR) >> 8), RECV_MD1);
1496 printf("Transmit descriptor ring\n");
1497 for (i = 0; i < NDESC(sc->ntdre); i++)
1498 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tflags = %b %b\n",
1499 i, ((sc->trans_ring + i)->md->md1 & HADR),
1500 (sc->trans_ring + i)->md->md0,
1501 -(short) (sc->trans_ring + i)->md->md2,
1502 ((sc->trans_ring + i)->md->md1 >> 8), TRANS_MD1,
1503 ((sc->trans_ring + i)->md->md3 >> 10), TRANS_MD3);
1504 printf("\nnext_to_send = %x\n", sc->next_to_send);
1505 printf("\n CSR0 = %b CSR1 = %x CSR2 = %x CSR3 = %x\n\n",
1506 read_csr(sc, CSR0), CSR0_FLAGS, read_csr(sc, CSR1),
1507 read_csr(sc, CSR2), read_csr(sc, CSR3));
1509 /* Set RAP back to CSR0 */
1510 lnc_outw(sc->rap, CSR0);
1514 mbuf_dump_chain(struct mbuf * m)
1517 #define MBUF_FLAGS \
1518 "\20\1M_EXT\2M_PKTHDR\3M_EOR\4UNKNOWN\5M_BCAST\6M_MCAST"
1521 log(LOG_DEBUG, "m == NULL\n");
1523 log(LOG_DEBUG, "m = %p\n", (void *)m);
1524 log(LOG_DEBUG, "m_hdr.mh_next = %p\n",
1525 (void *)m->m_hdr.mh_next);
1526 log(LOG_DEBUG, "m_hdr.mh_nextpkt = %p\n",
1527 (void *)m->m_hdr.mh_nextpkt);
1528 log(LOG_DEBUG, "m_hdr.mh_len = %d\n", m->m_hdr.mh_len);
1529 log(LOG_DEBUG, "m_hdr.mh_data = %p\n",
1530 (void *)m->m_hdr.mh_data);
1531 log(LOG_DEBUG, "m_hdr.mh_type = %d\n", m->m_hdr.mh_type);
1532 log(LOG_DEBUG, "m_hdr.mh_flags = %b\n", m->m_hdr.mh_flags,
1534 if (!(m->m_hdr.mh_flags & (M_PKTHDR | M_EXT)))
1535 log(LOG_DEBUG, "M_dat.M_databuf = %p\n",
1536 (void *)m->M_dat.M_databuf);
1538 if (m->m_hdr.mh_flags & M_PKTHDR) {
1539 log(LOG_DEBUG, "M_dat.MH.MH_pkthdr.len = %d\n",
1540 m->M_dat.MH.MH_pkthdr.len);
1542 "M_dat.MH.MH_pkthdr.rcvif = %p\n",
1543 (void *)m->M_dat.MH.MH_pkthdr.rcvif);
1544 if (!(m->m_hdr.mh_flags & M_EXT))
1546 "M_dat.MH.MH_dat.MH_databuf = %p\n",
1547 (void *)m->M_dat.MH.MH_dat.MH_databuf);
1549 if (m->m_hdr.mh_flags & M_EXT) {
1551 "M_dat.MH.MH_dat.MH_ext.ext_buff %p\n",
1552 (void *)m->M_dat.MH.MH_dat.MH_ext.ext_buf);
1554 "M_dat.MH.MH_dat.MH_ext.ext_free %p\n",
1555 (void *)m->M_dat.MH.MH_dat.MH_ext.ext_free);
1557 "M_dat.MH.MH_dat.MH_ext.ext_size %d\n",
1558 m->M_dat.MH.MH_dat.MH_ext.ext_size);
1561 } while ((m = m->m_next) != NULL);