]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/lnc/if_lnc.c
Use BUS_PROBE_DEFAULT for pci probe return value
[FreeBSD/FreeBSD.git] / sys / dev / lnc / if_lnc.c
1 /*-
2  * Copyright (c) 1994-2000
3  *      Paul Richards.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
11  *    point in the file.
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35 #define DIAGNOSTIC
36 #define DEBUG
37  *
38  * TODO ----
39  *
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.
42  *
43  * Check log settings.
44  *
45  * Check how all the arpcom flags get set and used.
46  *
47  * Re-inline and re-static all routines after debugging.
48  *
49  * Remember to assign iobase in SHMEM probe routines.
50  *
51  * Replace all occurences of LANCE-controller-card etc in prints by the name
52  * strings of the appropriate type -- nifty window dressing
53  *
54  * Add DEPCA support -- mostly done.
55  *
56  */
57
58 #include "opt_inet.h"
59
60 /* Some defines that should really be in generic locations */
61 #define FCS_LEN 4
62 #define MULTICAST_FILTER_LEN 8
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/bus.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/module.h>
71 #include <sys/socket.h>
72 #include <sys/sockio.h>
73 #include <sys/syslog.h>
74
75 #include <machine/bus.h>
76 #include <machine/resource.h>
77 #include <sys/rman.h>
78
79 #include <net/ethernet.h>
80 #include <net/if.h>
81 #include <net/if_dl.h>
82 #include <net/if_types.h>
83
84 #include <netinet/in.h>
85 #include <netinet/if_ether.h>
86
87 #include <net/bpf.h>
88
89 #include <machine/md_var.h>
90
91 #include <dev/lnc/if_lncvar.h>
92 #include <dev/lnc/if_lncreg.h>
93
94 devclass_t lnc_devclass;
95
96 static char const * const nic_ident[] = {
97         "Unknown",
98         "BICC",
99         "NE2100",
100         "DEPCA",
101         "CNET98S",      /* PC-98 */
102 };
103
104 static char const * const ic_ident[] = {
105         "Unknown",
106         "LANCE",
107         "C-LANCE",
108         "PCnet-ISA",
109         "PCnet-ISA+",
110         "PCnet-ISA II",
111         "PCnet-32 VL-Bus",
112         "PCnet-PCI",
113         "PCnet-PCI II",
114         "PCnet-FAST",
115         "PCnet-FAST+",
116         "PCnet-Home",
117 };
118
119 static void lnc_setladrf(struct lnc_softc *sc);
120 static void lnc_reset(struct lnc_softc *sc);
121 static void lnc_free_mbufs(struct lnc_softc *sc);
122 static __inline int alloc_mbuf_cluster(struct lnc_softc *sc,
123                                             struct host_ring_entry *desc);
124 static __inline struct mbuf *chain_mbufs(struct lnc_softc *sc,
125                                               int start_of_packet,
126                                               int pkt_len);
127 static __inline struct mbuf *mbuf_packet(struct lnc_softc *sc,
128                                               int start_of_packet,
129                                               int pkt_len);
130 static void lnc_rint(struct lnc_softc *sc);
131 static void lnc_tint(struct lnc_softc *sc);
132
133 static void lnc_init(void *);
134 static __inline int mbuf_to_buffer(struct mbuf *m, char *buffer);
135 static __inline struct mbuf *chain_to_cluster(struct mbuf *m);
136 static void lnc_start(struct ifnet *ifp);
137 static int lnc_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
138 static void lnc_watchdog(struct ifnet *ifp);
139 #ifdef DEBUG
140 void lnc_dump_state(struct lnc_softc *sc);
141 void mbuf_dump_chain(struct mbuf *m);
142 #endif
143
144 u_short
145 read_csr(struct lnc_softc *sc, u_short port)
146 {
147         lnc_outw(sc->rap, port);
148         return (lnc_inw(sc->rdp));
149 }
150
151 void
152 write_csr(struct lnc_softc *sc, u_short port, u_short val)
153 {
154         lnc_outw(sc->rap, port);
155         lnc_outw(sc->rdp, val);
156 }
157
158 static __inline void
159 write_bcr(struct lnc_softc *sc, u_short port, u_short val)
160 {
161         lnc_outw(sc->rap, port);
162         lnc_outw(sc->bdp, val);
163 }
164
165 static __inline u_short
166 read_bcr(struct lnc_softc *sc, u_short port)
167 {
168         lnc_outw(sc->rap, port);
169         return (lnc_inw(sc->bdp));
170 }
171
172 int
173 lance_probe(struct lnc_softc *sc)
174 {
175         write_csr(sc, CSR0, STOP);
176
177         if ((lnc_inw(sc->rdp) & STOP) && ! (read_csr(sc, CSR3))) {
178                 /*
179                  * Check to see if it's a C-LANCE. For the LANCE the INEA bit
180                  * cannot be set while the STOP bit is. This restriction is
181                  * removed for the C-LANCE.
182                  */
183                 write_csr(sc, CSR0, INEA);
184                 if (read_csr(sc, CSR0) & INEA)
185                         return (C_LANCE);
186                 else
187                         return (LANCE);
188         } else
189                 return (UNKNOWN);
190 }
191
192 void
193 lnc_release_resources(device_t dev)
194 {
195         lnc_softc_t *sc = device_get_softc(dev);
196
197         if (sc->irqres) {
198                 bus_teardown_intr(dev, sc->irqres, sc->intrhand);
199                 bus_release_resource(dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
200         }
201
202         if (sc->portres)
203                 bus_release_resource(dev, SYS_RES_IOPORT,
204                                      sc->portrid, sc->portres);
205         if (sc->drqres)
206                 bus_release_resource(dev, SYS_RES_DRQ, sc->drqrid, sc->drqres);
207
208         if (sc->dmat) {
209                 if (sc->dmamap) {
210                         bus_dmamap_unload(sc->dmat, sc->dmamap);
211                         bus_dmamem_free(sc->dmat, sc->recv_ring, sc->dmamap);
212                 }
213                 bus_dma_tag_destroy(sc->dmat);
214         }
215 }
216
217 /*
218  * Set up the logical address filter for multicast packets
219  */
220 static __inline void
221 lnc_setladrf(struct lnc_softc *sc)
222 {
223         struct ifnet *ifp = &sc->arpcom.ac_if;
224         struct ifmultiaddr *ifma;
225         u_long index;
226         int i;
227
228         if (sc->flags & IFF_ALLMULTI) {
229             for (i=0; i < MULTICAST_FILTER_LEN; i++)
230                 sc->init_block->ladrf[i] = 0xFF;
231             return;
232         }
233
234         /*
235          * For each multicast address, calculate a crc for that address and
236          * then use the high order 6 bits of the crc as a hash code where
237          * bits 3-5 select the byte of the address filter and bits 0-2 select
238          * the bit within that byte.
239          */
240
241         bzero(sc->init_block->ladrf, MULTICAST_FILTER_LEN);
242         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
243                 if (ifma->ifma_addr->sa_family != AF_LINK)
244                         continue;
245
246                 index = ether_crc32_le(LLADDR((struct sockaddr_dl *)
247                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
248                 sc->init_block->ladrf[index >> 3] |= 1 << (index & 7);
249         }
250 }
251
252 void
253 lnc_stop(struct lnc_softc *sc)
254 {
255         write_csr(sc, CSR0, STOP);
256 }
257
258 static void
259 lnc_reset(struct lnc_softc *sc)
260 {
261         lnc_init(sc);
262 }
263
264 static void
265 lnc_free_mbufs(struct lnc_softc *sc)
266 {
267         int i;
268
269         /*
270          * We rely on other routines to keep the buff.mbuf field valid. If
271          * it's not NULL then we assume it points to an allocated mbuf.
272          */
273
274         for (i = 0; i < NDESC(sc->nrdre); i++)
275                 if ((sc->recv_ring + i)->buff.mbuf)
276                         m_free((sc->recv_ring + i)->buff.mbuf);
277
278         for (i = 0; i < NDESC(sc->ntdre); i++)
279                 if ((sc->trans_ring + i)->buff.mbuf)
280                         m_free((sc->trans_ring + i)->buff.mbuf);
281
282         if (sc->mbuf_count)
283                 m_freem(sc->mbufs);
284 }
285
286 static __inline int
287 alloc_mbuf_cluster(struct lnc_softc *sc, struct host_ring_entry *desc)
288 {
289         register struct mds *md = desc->md;
290         struct mbuf *m=0;
291         int addr;
292
293         /* Try and get cluster off local cache */
294         if (sc->mbuf_count) {
295                 sc->mbuf_count--;
296                 m = sc->mbufs;
297                 sc->mbufs = m->m_next;
298                 /* XXX m->m_data = m->m_ext.ext_buf;*/
299         } else {
300                 MGET(m, M_DONTWAIT, MT_DATA);
301         if (!m)
302                         return(1);
303         MCLGET(m, M_DONTWAIT);
304         if (!m->m_ext.ext_buf) {
305                         m_free(m);
306                         return(1);
307                 }
308         }
309
310         desc->buff.mbuf = m;
311         addr = kvtop(m->m_data);
312         md->md0 = addr;
313         md->md1= ((addr >> 16) & 0xff) | OWN;
314         md->md2 = -(short)(MCLBYTES - sizeof(struct pkthdr));
315         md->md3 = 0;
316         return(0);
317 }
318
319 static __inline struct mbuf *
320 chain_mbufs(struct lnc_softc *sc, int start_of_packet, int pkt_len)
321 {
322         struct mbuf *head, *m;
323         struct host_ring_entry *desc;
324
325         /*
326          * Turn head into a pkthdr mbuf --
327          * assumes a pkthdr type mbuf was
328          * allocated to the descriptor
329          * originally.
330          */
331
332         desc = sc->recv_ring + start_of_packet;
333
334         head = desc->buff.mbuf;
335         head->m_flags |= M_PKTHDR;
336         bzero(&head->m_pkthdr, sizeof(head->m_pkthdr));
337
338         m = head;
339         do {
340                 m = desc->buff.mbuf;
341                 m->m_len = min((MCLBYTES - sizeof(struct pkthdr)), pkt_len);
342                 pkt_len -= m->m_len;
343                 if (alloc_mbuf_cluster(sc, desc))
344                         return((struct mbuf *)NULL);
345                 INC_MD_PTR(start_of_packet, sc->nrdre)
346                 desc = sc->recv_ring + start_of_packet;
347                 m->m_next = desc->buff.mbuf;
348         } while (start_of_packet != sc->recv_next);
349
350         m->m_next = 0;
351         return(head);
352 }
353
354 static __inline struct mbuf *
355 mbuf_packet(struct lnc_softc *sc, int start_of_packet, int pkt_len)
356 {
357
358         struct host_ring_entry *start;
359         struct mbuf *head,*m,*m_prev;
360         char *data,*mbuf_data;
361         short blen;
362         int amount;
363
364         /* Get a pkthdr mbuf for the start of packet */
365         MGETHDR(head, M_DONTWAIT, MT_DATA);
366         if (!head) {
367                 LNCSTATS(drop_packet)
368                 return(0);
369         }
370
371         m = head;
372         m->m_len = 0;
373         start = sc->recv_ring + start_of_packet;
374         /*blen = -(start->md->md2);*/
375         blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
376         data = start->buff.data;
377         mbuf_data = m->m_data;
378
379         while (start_of_packet != sc->recv_next) {
380                 /*
381                  * If the data left fits in a single buffer then set
382                  * blen to the size of the data left.
383                  */
384                 if (pkt_len < blen)
385                         blen = pkt_len;
386
387                 /*
388                  * amount is least of data in current ring buffer and
389                  * amount of space left in current mbuf.
390                  */
391                 amount = min(blen, M_TRAILINGSPACE(m));
392                 if (amount == 0) {
393                         /* mbuf must be empty */
394                         m_prev = m;
395                         MGET(m, M_DONTWAIT, MT_DATA);
396                         if (!m) {
397                                 m_freem(head);
398                                 return(0);
399                         }
400                         if (pkt_len >= MINCLSIZE)
401                                 MCLGET(m, M_DONTWAIT);
402                         m->m_len = 0;
403                         m_prev->m_next = m;
404                         amount = min(blen, M_TRAILINGSPACE(m));
405                         mbuf_data = m->m_data;
406                 }
407                 bcopy(data, mbuf_data, amount);
408                 blen -= amount;
409                 pkt_len -= amount;
410                 m->m_len += amount;
411                 data += amount;
412                 mbuf_data += amount;
413
414                 if (blen == 0) {
415                         start->md->md1 &= HADR;
416                         start->md->md1 |= OWN;
417                         start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
418                         INC_MD_PTR(start_of_packet, sc->nrdre)
419                         start = sc->recv_ring + start_of_packet;
420                         data = start->buff.data;
421                         /*blen = -(start->md->md2);*/
422                         blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
423                 }
424         }
425         return(head);
426 }
427
428
429 static void
430 lnc_rint(struct lnc_softc *sc)
431 {
432         struct ifnet *ifp = &sc->arpcom.ac_if;
433         struct host_ring_entry *next, *start;
434         int start_of_packet;
435         struct mbuf *head;
436         struct ether_header *eh;
437         int lookahead;
438         int flags;
439         int pkt_len;
440
441         /*
442          * The LANCE will issue a RINT interrupt when the ownership of the
443          * last buffer of a receive packet has been relinquished by the LANCE.
444          * Therefore, it can be assumed that a complete packet can be found
445          * before hitting buffers that are still owned by the LANCE, if not
446          * then there is a bug in the driver that is causing the descriptors
447          * to get out of sync.
448          */
449
450 #ifdef DIAGNOSTIC
451         if ((sc->recv_ring + sc->recv_next)->md->md1 & OWN) {
452                 log(LOG_ERR, "%s: Receive interrupt with buffer still owned by controller -- Resetting\n", ifp->if_xname);
453                 lnc_reset(sc);
454                 return;
455         }
456         if (!((sc->recv_ring + sc->recv_next)->md->md1 & STP)) {
457                 log(LOG_ERR, "%s: Receive interrupt but not start of packet -- Resetting\n", ifp->if_xname);
458                 lnc_reset(sc);
459                 return;
460         }
461 #endif
462
463         lookahead = 0;
464         next = sc->recv_ring + sc->recv_next;
465         while ((flags = next->md->md1) & STP) {
466
467                 /* Make a note of the start of the packet */
468                 start_of_packet = sc->recv_next;
469
470                 /*
471                  * Find the end of the packet. Even if not data chaining,
472                  * jabber packets can overrun into a second descriptor.
473                  * If there is no error, then the ENP flag is set in the last
474                  * descriptor of the packet. If there is an error then the ERR
475                  * flag will be set in the descriptor where the error occured.
476                  * Therefore, to find the last buffer of a packet we search for
477                  * either ERR or ENP.
478                  */
479
480                 if (!(flags & (ENP | MDERR))) {
481                         do {
482                                 INC_MD_PTR(sc->recv_next, sc->nrdre)
483                                 next = sc->recv_ring + sc->recv_next;
484                                 flags = next->md->md1;
485                         } while (!(flags & (STP | OWN | ENP | MDERR)));
486
487                         if (flags & STP) {
488                                 log(LOG_ERR, "%s: Start of packet found before end of previous in receive ring -- Resetting\n", ifp->if_xname);
489                                 lnc_reset(sc);
490                                 return;
491                         }
492                         if (flags & OWN) {
493                                 if (lookahead) {
494                                         /*
495                                          * Looked ahead into a packet still
496                                          * being received
497                                          */
498                                         sc->recv_next = start_of_packet;
499                                         break;
500                                 } else {
501                                         log(LOG_ERR, "%s: End of received packet not found-- Resetting\n", ifp->if_xname);
502                                         lnc_reset(sc);
503                                         return;
504                                 }
505                         }
506                 }
507
508                 pkt_len = (next->md->md3 & MCNT) - FCS_LEN;
509
510                 /* Move pointer onto start of next packet */
511                 INC_MD_PTR(sc->recv_next, sc->nrdre)
512                 next = sc->recv_ring + sc->recv_next;
513
514                 if (flags & MDERR) {
515                         const char *if_xname = ifp->if_xname;
516                         if (flags & RBUFF) {
517                                 LNCSTATS(rbuff)
518                                 log(LOG_ERR, "%s: Receive buffer error\n", if_xname);
519                         }
520                         if (flags & OFLO) {
521                                 /* OFLO only valid if ENP is not set */
522                                 if (!(flags & ENP)) {
523                                         LNCSTATS(oflo)
524                                         log(LOG_ERR, "%s: Receive overflow error \n", if_xname);
525                                 }
526                         } else if (flags & ENP) {
527                             if ((ifp->if_flags & IFF_PROMISC)==0) {
528                                 /*
529                                  * FRAM and CRC are valid only if ENP
530                                  * is set and OFLO is not.
531                                  */
532                                 if (flags & FRAM) {
533                                         LNCSTATS(fram)
534                                         log(LOG_ERR, "%s: Framing error\n", if_xname);
535                                         /*
536                                          * FRAM is only set if there's a CRC
537                                          * error so avoid multiple messages
538                                          */
539                                 } else if (flags & CRC) {
540                                         LNCSTATS(crc)
541                                         log(LOG_ERR, "%s: Receive CRC error\n", if_xname);
542                                 }
543                             }
544                         }
545
546                         /* Drop packet */
547                         LNCSTATS(rerr)
548                         ifp->if_ierrors++;
549                         while (start_of_packet != sc->recv_next) {
550                                 start = sc->recv_ring + start_of_packet;
551                                 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
552                                 start->md->md1 &= HADR;
553                                 start->md->md1 |= OWN;
554                                 INC_MD_PTR(start_of_packet, sc->nrdre)
555                         }
556                 } else { /* Valid packet */
557
558                         ifp->if_ipackets++;
559
560
561                         if (sc->nic.mem_mode == DMA_MBUF)
562                                 head = chain_mbufs(sc, start_of_packet, pkt_len);
563                         else
564                                 head = mbuf_packet(sc, start_of_packet, pkt_len);
565
566                         if (head) {
567                                 /*
568                                  * First mbuf in packet holds the
569                                  * ethernet and packet headers
570                                  */
571                                 head->m_pkthdr.rcvif = ifp;
572                                 head->m_pkthdr.len = pkt_len ;
573                                 eh = (struct ether_header *) head->m_data;
574
575                                 /*
576                                  * vmware ethernet hardware emulation loops
577                                  * packets back to itself, violates IFF_SIMPLEX.
578                                  * drop it if it is from myself.
579                                 */
580                                 if (bcmp(eh->ether_shost,
581                                       sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) == 0) {
582                                     m_freem(head);
583                                 } else {
584                                     (*ifp->if_input)(ifp, head);
585                                 }
586                         } else {
587                                 log(LOG_ERR,"%s: Packet dropped, no mbufs\n",ifp->if_xname);
588                                 LNCSTATS(drop_packet)
589                         }
590                 }
591
592                 lookahead++;
593         }
594
595         /*
596          * At this point all completely received packets have been processed
597          * so clear RINT since any packets that have arrived while we were in
598          * here have been dealt with.
599          */
600
601         lnc_outw(sc->rdp, RINT | INEA);
602 }
603
604 static void
605 lnc_tint(struct lnc_softc *sc)
606 {
607         struct host_ring_entry *next, *start;
608         int start_of_packet;
609         int lookahead;
610
611         /*
612          * If the driver is reset in this routine then we return immediately to
613          * the interrupt driver routine. Any interrupts that have occured
614          * since the reset will be dealt with there. sc->trans_next
615          * should point to the start of the first packet that was awaiting
616          * transmission after the last transmit interrupt was dealt with. The
617          * LANCE should have relinquished ownership of that descriptor before
618          * the interrupt. Therefore, sc->trans_next should point to a
619          * descriptor with STP set and OWN cleared. If not then the driver's
620          * pointers are out of sync with the LANCE, which signifies a bug in
621          * the driver. Therefore, the following two checks are really
622          * diagnostic, since if the driver is working correctly they should
623          * never happen.
624          */
625
626 #ifdef DIAGNOSTIC
627         if ((sc->trans_ring + sc->trans_next)->md->md1 & OWN) {
628                 log(LOG_ERR, "%s: Transmit interrupt with buffer still owned by controller -- Resetting\n", sc->arpcom.ac_if.if_xname);
629                 lnc_reset(sc);
630                 return;
631         }
632 #endif
633
634
635         /*
636          * The LANCE will write the status information for the packet it just
637          * tried to transmit in one of two places. If the packet was
638          * transmitted successfully then the status will be written into the
639          * last descriptor of the packet. If the transmit failed then the
640          * status will be written into the descriptor that was being accessed
641          * when the error occured and all subsequent descriptors in that
642          * packet will have been relinquished by the LANCE.
643          *
644          * At this point we know that sc->trans_next points to the start
645          * of a packet that the LANCE has just finished trying to transmit.
646          * We now search for a buffer with either ENP or ERR set.
647          */
648
649         lookahead = 0;
650
651         do {
652                 start_of_packet = sc->trans_next;
653                 next = sc->trans_ring + sc->trans_next;
654
655 #ifdef DIAGNOSTIC
656         if (!(next->md->md1 & STP)) {
657                 log(LOG_ERR, "%s: Transmit interrupt but not start of packet -- Resetting\n", sc->arpcom.ac_if.if_xname);
658                 lnc_reset(sc);
659                 return;
660         }
661 #endif
662
663                 /*
664                  * Find end of packet.
665                  */
666
667                 if (!(next->md->md1 & (ENP | MDERR))) {
668                         do {
669                                 INC_MD_PTR(sc->trans_next, sc->ntdre)
670                                 next = sc->trans_ring + sc->trans_next;
671                         } while (!(next->md->md1 & (STP | OWN | ENP | MDERR)));
672
673                         if (next->md->md1 & STP) {
674                                 log(LOG_ERR, "%s: Start of packet found before end of previous in transmit ring -- Resetting\n", sc->arpcom.ac_if.if_xname);
675                                 lnc_reset(sc);
676                                 return;
677                         }
678                         if (next->md->md1 & OWN) {
679                                 if (lookahead) {
680                                         /*
681                                          * Looked ahead into a packet still
682                                          * being transmitted
683                                          */
684                                         sc->trans_next = start_of_packet;
685                                         break;
686                                 } else {
687                                         log(LOG_ERR, "%s: End of transmitted packet not found -- Resetting\n", sc->arpcom.ac_if.if_xname);
688                                         lnc_reset(sc);
689                                         return;
690                                 }
691                         }
692                 }
693                 /*
694                  * Check for ERR first since other flags are irrelevant if an
695                  * error occurred.
696                  */
697                 if (next->md->md1 & MDERR) {
698
699                         LNCSTATS(terr)
700                                 sc->arpcom.ac_if.if_oerrors++;
701
702                         if (next->md->md3 & LCOL) {
703                                 LNCSTATS(lcol)
704                                 log(LOG_ERR, "%s: Transmit late collision  -- Net error?\n", sc->arpcom.ac_if.if_xname);
705                                 sc->arpcom.ac_if.if_collisions++;
706                                 /*
707                                  * Clear TBUFF since it's not valid when LCOL
708                                  * set
709                                  */
710                                 next->md->md3 &= ~TBUFF;
711                         }
712                         if (next->md->md3 & LCAR) {
713                                 LNCSTATS(lcar)
714                                 log(LOG_ERR, "%s: Loss of carrier during transmit -- Net error?\n", sc->arpcom.ac_if.if_xname);
715                         }
716                         if (next->md->md3 & RTRY) {
717                                 LNCSTATS(rtry)
718                                 log(LOG_ERR, "%s: Transmit of packet failed after 16 attempts -- TDR = %d\n", sc->arpcom.ac_if.if_xname, ((sc->trans_ring + sc->trans_next)->md->md3 & TDR));
719                                 sc->arpcom.ac_if.if_collisions += 16;
720                                 /*
721                                  * Clear TBUFF since it's not valid when RTRY
722                                  * set
723                                  */
724                                 next->md->md3 &= ~TBUFF;
725                         }
726                         /*
727                          * TBUFF is only valid if neither LCOL nor RTRY are set.
728                          * We need to check UFLO after LCOL and RTRY so that we
729                          * know whether or not TBUFF is valid. If either are
730                          * set then TBUFF will have been cleared above. A
731                          * UFLO error will turn off the transmitter so we
732                          * have to reset.
733                          *
734                          */
735
736                         if (next->md->md3 & UFLO) {
737                                 LNCSTATS(uflo)
738                                 /*
739                                  * If an UFLO has occured it's possibly due
740                                  * to a TBUFF error
741                                  */
742                                 if (next->md->md3 & TBUFF) {
743                                         LNCSTATS(tbuff)
744                                         log(LOG_ERR, "%s: Transmit buffer error -- Resetting\n", sc->arpcom.ac_if.if_xname);
745                                 } else
746                                         log(LOG_ERR, "%s: Transmit underflow error -- Resetting\n", sc->arpcom.ac_if.if_xname);
747                                 lnc_reset(sc);
748                                 return;
749                         }
750                         do {
751                                 INC_MD_PTR(sc->trans_next, sc->ntdre)
752                                 next = sc->trans_ring + sc->trans_next;
753                         } while (!(next->md->md1 & STP) && (sc->trans_next != sc->next_to_send));
754
755                 } else {
756                         /*
757                          * Since we check for ERR first then if we get here
758                          * the packet was transmitted correctly. There may
759                          * still have been non-fatal errors though.
760                          * Don't bother checking for DEF, waste of time.
761                          */
762
763                         sc->arpcom.ac_if.if_opackets++;
764
765                         if (next->md->md1 & MORE) {
766                                 LNCSTATS(more)
767                                 sc->arpcom.ac_if.if_collisions += 2;
768                         }
769
770                         /*
771                          * ONE is invalid if LCOL is set. If LCOL was set then
772                          * ERR would have also been set and we would have
773                          * returned from lnc_tint above. Therefore we can
774                          * assume if we arrive here that ONE is valid.
775                          *
776                          */
777
778                         if (next->md->md1 & ONE) {
779                                 LNCSTATS(one)
780                                 sc->arpcom.ac_if.if_collisions++;
781                         }
782                         INC_MD_PTR(sc->trans_next, sc->ntdre)
783                         next = sc->trans_ring + sc->trans_next;
784                 }
785
786                 /*
787                  * Clear descriptors and free any mbufs.
788                  */
789
790                 do {
791                         start = sc->trans_ring + start_of_packet;
792                         start->md->md1 &= HADR;
793                         if (sc->nic.mem_mode == DMA_MBUF) {
794                                 /* Cache clusters on a local queue */
795                                 if ((start->buff.mbuf->m_flags & M_EXT) && (sc->mbuf_count < MBUF_CACHE_LIMIT)) {
796                                         if (sc->mbuf_count) {
797                                                 start->buff.mbuf->m_next = sc->mbufs;
798                                                 sc->mbufs = start->buff.mbuf;
799                                         } else
800                                                 sc->mbufs = start->buff.mbuf;
801                                         sc->mbuf_count++;
802                                         start->buff.mbuf = 0;
803                                 } else {
804                                         /*
805                                          * XXX should this be m_freem()?
806                                          */
807                                         m_free(start->buff.mbuf);
808                                         start->buff.mbuf = NULL;
809                                 }
810                         }
811                         sc->pending_transmits--;
812                         INC_MD_PTR(start_of_packet, sc->ntdre)
813                 }while (start_of_packet != sc->trans_next);
814
815                 /*
816                  * There's now at least one free descriptor
817                  * in the ring so indicate that we can accept
818                  * more packets again.
819                  */
820
821                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
822
823                 lookahead++;
824
825         } while (sc->pending_transmits && !(next->md->md1 & OWN));
826
827         /*
828          * Clear TINT since we've dealt with all
829          * the completed transmissions.
830          */
831
832         lnc_outw(sc->rdp, TINT | INEA);
833 }
834
835 int
836 lnc_attach_common(device_t dev)
837 {
838         lnc_softc_t *sc = device_get_softc(dev);
839         int i;
840         int skip;
841
842         switch (sc->nic.ident) {
843         case BICC:
844         case CNET98S:
845                 skip = 2;
846                 break;
847         default:
848                 skip = 1;
849                 break;
850         }
851
852         /* Set default mode */
853         sc->nic.mode = NORMAL;
854
855         /* Fill in arpcom structure entries */
856
857         sc->arpcom.ac_if.if_softc = sc;
858         if_initname(&sc->arpcom.ac_if, device_get_name(dev),
859             device_get_unit(dev));
860         sc->arpcom.ac_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX |
861             IFF_MULTICAST | IFF_NEEDSGIANT;
862         sc->arpcom.ac_if.if_timer = 0;
863         sc->arpcom.ac_if.if_start = lnc_start;
864         sc->arpcom.ac_if.if_ioctl = lnc_ioctl;
865         sc->arpcom.ac_if.if_watchdog = lnc_watchdog;
866         sc->arpcom.ac_if.if_init = lnc_init;
867         IFQ_SET_MAXLEN(&sc->arpcom.ac_if.if_snd, IFQ_MAXLEN);
868         sc->arpcom.ac_if.if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
869         IFQ_SET_READY(&sc->arpcom.ac_if.if_snd);
870
871         /* Extract MAC address from PROM */
872         for (i = 0; i < ETHER_ADDR_LEN; i++)
873                 sc->arpcom.ac_enaddr[i] = lnc_inb(i * skip);
874
875         /*
876          * XXX -- should check return status of if_attach
877          */
878
879         ether_ifattach(&sc->arpcom.ac_if, sc->arpcom.ac_enaddr);
880
881         if (sc->nic.ic == LANCE || sc->nic.ic == C_LANCE)
882                 if_printf(&sc->arpcom.ac_if, "%s (%s)\n",
883                        nic_ident[sc->nic.ident], ic_ident[sc->nic.ic]);
884         else
885                 if_printf(&sc->arpcom.ac_if, "%s\n", ic_ident[sc->nic.ic]);
886
887         return (1);
888 }
889
890 static void
891 lnc_init(xsc)
892         void *xsc;
893 {
894         struct lnc_softc *sc = xsc;
895         int s, i;
896         char *lnc_mem;
897
898         /* Shut down interface */
899
900         s = splimp();
901         lnc_stop(sc);
902         sc->arpcom.ac_if.if_flags |= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; /* XXX??? */
903
904         /*
905          * This sets up the memory area for the controller. Memory is set up for
906          * the initialisation block (12 words of contiguous memory starting
907          * on a word boundary),the transmit and receive ring structures (each
908          * entry is 4 words long and must start on a quadword boundary) and
909          * the data buffers.
910          *
911          * The alignment tests are particularly paranoid.
912          */
913
914         sc->recv_next = 0;
915         sc->trans_ring = sc->recv_ring + NDESC(sc->nrdre);
916         sc->trans_next = 0;
917
918         if (sc->nic.mem_mode == SHMEM)
919                 lnc_mem = (char *)(uintptr_t)sc->nic.iobase;
920         else
921                 lnc_mem = (char *) (sc->trans_ring + NDESC(sc->ntdre));
922
923         lnc_mem = (char *)(((long)lnc_mem + 1) & ~1);
924         sc->init_block = (struct init_block *) ((long) lnc_mem & ~1);
925         lnc_mem = (char *) (sc->init_block + 1);
926         lnc_mem = (char *)(((long)lnc_mem + 7) & ~7);
927
928         /* Initialise pointers to descriptor entries */
929         for (i = 0; i < NDESC(sc->nrdre); i++) {
930                 (sc->recv_ring + i)->md = (struct mds *) lnc_mem;
931                 lnc_mem += sizeof(struct mds);
932         }
933         for (i = 0; i < NDESC(sc->ntdre); i++) {
934                 (sc->trans_ring + i)->md = (struct mds *) lnc_mem;
935                 lnc_mem += sizeof(struct mds);
936         }
937
938         /* Initialise the remaining ring entries */
939
940         if (sc->nic.mem_mode == DMA_MBUF) {
941
942                 sc->mbufs = 0;
943                 sc->mbuf_count = 0;
944
945                 /* Free previously allocated mbufs */
946                 if (sc->flags & LNC_INITIALISED)
947                         lnc_free_mbufs(sc);
948
949
950                 for (i = 0; i < NDESC(sc->nrdre); i++) {
951                         if (alloc_mbuf_cluster(sc, sc->recv_ring+i)) {
952                                 log(LOG_ERR, "Initialisation failed -- no mbufs\n");
953                                 splx(s);
954                                 return;
955                         }
956                 }
957
958                 for (i = 0; i < NDESC(sc->ntdre); i++) {
959                         (sc->trans_ring + i)->buff.mbuf = 0;
960                         (sc->trans_ring + i)->md->md0 = 0;
961                         (sc->trans_ring + i)->md->md1 = 0;
962                         (sc->trans_ring + i)->md->md2 = 0;
963                         (sc->trans_ring + i)->md->md3 = 0;
964                 }
965         } else {
966                 for (i = 0; i < NDESC(sc->nrdre); i++) {
967                         (sc->recv_ring + i)->md->md0 = kvtop(lnc_mem);
968                         (sc->recv_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff) | OWN;
969                         (sc->recv_ring + i)->md->md2 = -RECVBUFSIZE;
970                         (sc->recv_ring + i)->md->md3 = 0;
971                         (sc->recv_ring + i)->buff.data = lnc_mem;
972                         lnc_mem += RECVBUFSIZE;
973                 }
974                 for (i = 0; i < NDESC(sc->ntdre); i++) {
975                         (sc->trans_ring + i)->md->md0 = kvtop(lnc_mem);
976                         (sc->trans_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff);
977                         (sc->trans_ring + i)->md->md2 = 0;
978                         (sc->trans_ring + i)->md->md3 = 0;
979                         (sc->trans_ring + i)->buff.data = lnc_mem;
980                         lnc_mem += TRANSBUFSIZE;
981                 }
982         }
983
984         sc->next_to_send = 0;
985
986         /* Set up initialisation block */
987
988         sc->init_block->mode = sc->nic.mode;
989
990         for (i = 0; i < ETHER_ADDR_LEN; i++)
991                 sc->init_block->padr[i] = sc->arpcom.ac_enaddr[i];
992
993         lnc_setladrf(sc);
994
995         sc->init_block->rdra = kvtop(sc->recv_ring->md);
996         sc->init_block->rlen = ((kvtop(sc->recv_ring->md) >> 16) & 0xff) | (sc->nrdre << 13);
997         sc->init_block->tdra = kvtop(sc->trans_ring->md);
998         sc->init_block->tlen = ((kvtop(sc->trans_ring->md) >> 16) & 0xff) | (sc->ntdre << 13);
999
1000
1001         /* Set flags to show that the memory area is valid */
1002         sc->flags |= LNC_INITIALISED;
1003
1004         sc->pending_transmits = 0;
1005
1006         /* Give the LANCE the physical address of the initialisation block */
1007
1008         if (sc->nic.ic == PCnet_Home) {
1009                 u_short media;
1010                 /* Set PHY_SEL to HomeRun */
1011                 media = read_bcr(sc, BCR49);
1012                 media &= ~3;
1013                 media |= 1;
1014                 write_bcr(sc, BCR49, media);
1015         }
1016
1017         write_csr(sc, CSR1, kvtop(sc->init_block));
1018         write_csr(sc, CSR2, (kvtop(sc->init_block) >> 16) & 0xff);
1019
1020         /*
1021          * Depending on which controller this is, CSR3 has different meanings.
1022          * For the Am7990 it controls DMA operations, for the Am79C960 it
1023          * controls interrupt masks and transmitter algorithms. In either
1024          * case, none of the flags are set.
1025          *
1026          */
1027
1028         write_csr(sc, CSR3, 0);
1029
1030         /* Let's see if it starts */
1031 /*
1032 printf("Enabling lnc interrupts\n");
1033         sc->arpcom.ac_if.if_timer = 10;
1034         write_csr(sc, CSR0, INIT|INEA);
1035 */
1036
1037         /*
1038          * Now that the initialisation is complete there's no reason to
1039          * access anything except CSR0, so we leave RAP pointing there
1040          * so we can just access RDP from now on, saving an outw each
1041          * time.
1042          */
1043
1044         write_csr(sc, CSR0, INIT);
1045         for(i=0; i < 1000; i++)
1046                 if (read_csr(sc, CSR0) & IDON)
1047                         break;
1048
1049         if (read_csr(sc, CSR0) & IDON) {
1050                 /*
1051                  * Enable interrupts, start the LANCE, mark the interface as
1052                  * running and transmit any pending packets.
1053                  */
1054                 write_csr(sc, CSR0, STRT | INEA);
1055                 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1056                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1057                 lnc_start(&sc->arpcom.ac_if);
1058         } else
1059                 log(LOG_ERR, "%s: Initialisation failed\n", 
1060                     sc->arpcom.ac_if.if_xname);
1061
1062         splx(s);
1063 }
1064
1065 /*
1066  * The interrupt flag (INTR) will be set and provided that the interrupt enable
1067  * flag (INEA) is also set, the interrupt pin will be driven low when any of
1068  * the following occur:
1069  *
1070  * 1) Completion of the initialisation routine (IDON). 2) The reception of a
1071  * packet (RINT). 3) The transmission of a packet (TINT). 4) A transmitter
1072  * timeout error (BABL). 5) A missed packet (MISS). 6) A memory error (MERR).
1073  *
1074  * The interrupt flag is cleared when all of the above conditions are cleared.
1075  *
1076  * If the driver is reset from this routine then it first checks to see if any
1077  * interrupts have ocurred since the reset and handles them before returning.
1078  * This is because the NIC may signify a pending interrupt in CSR0 using the
1079  * INTR flag even if a hardware interrupt is currently inhibited (at least I
1080  * think it does from reading the data sheets). We may as well deal with
1081  * these pending interrupts now rather than get the overhead of another
1082  * hardware interrupt immediately upon returning from the interrupt handler.
1083  *
1084  */
1085
1086 void
1087 lncintr(void *arg)
1088 {
1089         lnc_softc_t *sc = arg;
1090         u_short csr0;
1091
1092         /*
1093          * INEA is the only bit that can be cleared by writing a 0 to it so
1094          * we have to include it in any writes that clear other flags.
1095          */
1096
1097         while ((csr0 = lnc_inw(sc->rdp)) & INTR) {
1098
1099                 /*
1100                  * Clear interrupt flags early to avoid race conditions. The
1101                  * controller can still set these flags even while we're in
1102                  * this interrupt routine. If the flag is still set from the
1103                  * event that caused this interrupt any new events will
1104                  * be missed.
1105                  */
1106
1107                 lnc_outw(sc->rdp, csr0);
1108                 /*lnc_outw(sc->rdp, IDON | CERR | BABL | MISS | MERR | RINT | TINT | INEA);*/
1109
1110 #ifdef notyet
1111                 if (csr0 & IDON) {
1112 printf("IDON\n");
1113                         sc->arpcom.ac_if.if_timer = 0;
1114                         write_csr(sc, CSR0, STRT | INEA);
1115                         sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
1116                         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1117                         lnc_start(&sc->arpcom.ac_if);
1118                         continue;
1119                 }
1120 #endif
1121
1122                 if (csr0 & ERR) {
1123                         if (csr0 & CERR) {
1124                                 log(LOG_ERR, "%s: Heartbeat error -- SQE test failed\n", sc->arpcom.ac_if.if_xname);
1125                                 LNCSTATS(cerr)
1126                         }
1127                         if (csr0 & BABL) {
1128                                 log(LOG_ERR, "%s: Babble error - more than 1519 bytes transmitted\n", sc->arpcom.ac_if.if_xname);
1129                                 LNCSTATS(babl)
1130                                 sc->arpcom.ac_if.if_oerrors++;
1131                         }
1132                         if (csr0 & MISS) {
1133                                 log(LOG_ERR, "%s: Missed packet -- no receive buffer\n", sc->arpcom.ac_if.if_xname);
1134                                 LNCSTATS(miss)
1135                                 sc->arpcom.ac_if.if_ierrors++;
1136                         }
1137                         if (csr0 & MERR) {
1138                                 log(LOG_ERR, "%s: Memory error  -- Resetting\n", sc->arpcom.ac_if.if_xname);
1139                                 LNCSTATS(merr)
1140                                 lnc_reset(sc);
1141                                 continue;
1142                         }
1143                 }
1144                 if (csr0 & RINT) {
1145                         LNCSTATS(rint)
1146                         lnc_rint(sc);
1147                 }
1148                 if (csr0 & TINT) {
1149                         LNCSTATS(tint)
1150                         sc->arpcom.ac_if.if_timer = 0;
1151                         lnc_tint(sc);
1152                 }
1153
1154                 /*
1155                  * If there's room in the transmit descriptor ring then queue
1156                  * some more transmit packets.
1157                  */
1158
1159                 if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
1160                         lnc_start(&sc->arpcom.ac_if);
1161         }
1162 }
1163
1164 static __inline int
1165 mbuf_to_buffer(struct mbuf *m, char *buffer)
1166 {
1167
1168         int len=0;
1169
1170         for( ; m; m = m->m_next) {
1171                 bcopy(mtod(m, caddr_t), buffer, m->m_len);
1172                 buffer += m->m_len;
1173                 len += m->m_len;
1174         }
1175
1176         return(len);
1177 }
1178
1179 static __inline struct mbuf *
1180 chain_to_cluster(struct mbuf *m)
1181 {
1182         struct mbuf *new;
1183
1184         MGET(new, M_DONTWAIT, MT_DATA);
1185         if (new) {
1186                 MCLGET(new, M_DONTWAIT);
1187                 if (new->m_ext.ext_buf) {
1188                         new->m_len = mbuf_to_buffer(m, new->m_data);
1189                         m_freem(m);
1190                         return(new);
1191                 } else
1192                         m_free(new);
1193         }
1194         return(0);
1195 }
1196
1197 /*
1198  * IFF_OACTIVE and IFF_RUNNING are checked in ether_output so it's redundant
1199  * to check them again since we wouldn't have got here if they were not
1200  * appropriately set. This is also called from lnc_init and lncintr but the
1201  * flags should be ok at those points too.
1202  */
1203
1204 static void
1205 lnc_start(struct ifnet *ifp)
1206 {
1207
1208         struct lnc_softc *sc = ifp->if_softc;
1209         struct host_ring_entry *desc;
1210         int tmp;
1211         int end_of_packet;
1212         struct mbuf *head, *m;
1213         int len, chunk;
1214         int addr;
1215         int no_entries_needed;
1216
1217         do {
1218
1219                 IFQ_DRV_DEQUEUE(&sc->arpcom.ac_if.if_snd, head);
1220                 if (!head)
1221                         return;
1222
1223                 if (sc->nic.mem_mode == DMA_MBUF) {
1224
1225                         no_entries_needed = 0;
1226                         for (m=head; m; m = m->m_next)
1227                                 no_entries_needed++;
1228
1229                         /*
1230                          * We try and avoid bcopy as much as possible
1231                          * but there are two cases when we use it.
1232                          *
1233                          * 1) If there are not enough free entries in the ring
1234                          * to hold each mbuf in the chain then compact the
1235                          * chain into a single cluster.
1236                          *
1237                          * 2) The Am7990 and Am79C90 must not have less than
1238                          * 100 bytes in the first descriptor of a chained
1239                          * packet so it's necessary to shuffle the mbuf
1240                          * contents to ensure this.
1241                          */
1242
1243
1244                         if (no_entries_needed > (NDESC(sc->ntdre) - sc->pending_transmits)) {
1245                                 if (!(head = chain_to_cluster(head))) {
1246                                         log(LOG_ERR, "%s: Couldn't get mbuf for transmit packet -- Resetting \n ",ifp->if_xname);
1247                                         lnc_reset(sc);
1248                                         return;
1249                                 }
1250                         } else if ((sc->nic.ic == LANCE) || (sc->nic.ic == C_LANCE)) {
1251                                 if ((head->m_len < 100) && (head->m_next)) {
1252                                         len = 100 - head->m_len;
1253                                         if (M_TRAILINGSPACE(head) < len) {
1254                                                 /*
1255                                                  * Move data to start of data
1256                                                  * area. We assume the first
1257                                                  * mbuf has a packet header
1258                                                  * and is not a cluster.
1259                                                  */
1260                                                 bcopy((caddr_t)head->m_data, (caddr_t)head->m_pktdat, head->m_len);
1261                                                 head->m_data = head->m_pktdat;
1262                                         }
1263                                         m = head->m_next;
1264                                         while (m && (len > 0)) {
1265                                                 chunk = min(len, m->m_len);
1266                                                 bcopy(mtod(m, caddr_t), mtod(head, caddr_t) + head->m_len, chunk);
1267                                                 len -= chunk;
1268                                                 head->m_len += chunk;
1269                                                 m->m_len -= chunk;
1270                                                 m->m_data += chunk;
1271                                                 if (m->m_len <= 0) {
1272                                                         m = m_free(m);
1273                                                         head->m_next = m;
1274                                                 }
1275                                         }
1276                                 }
1277                         }
1278
1279                         tmp = sc->next_to_send;
1280
1281                         /*
1282                          * On entering this loop we know that tmp points to a
1283                          * descriptor with a clear OWN bit.
1284                          */
1285
1286                         desc = sc->trans_ring + tmp;
1287                         len = ETHER_MIN_LEN;
1288                         for (m = head; m; m = m->m_next) {
1289                                 desc->buff.mbuf = m;
1290                                 addr = kvtop(m->m_data);
1291                                 desc->md->md0 = addr;
1292                                 desc->md->md1 = ((addr >> 16) & 0xff);
1293                                 desc->md->md3 = 0;
1294                                 desc->md->md2 = -m->m_len;
1295                                 sc->pending_transmits++;
1296                                 len -= m->m_len;
1297
1298                                 INC_MD_PTR(tmp, sc->ntdre)
1299                                 desc = sc->trans_ring + tmp;
1300                         }
1301
1302                         end_of_packet = tmp;
1303                         DEC_MD_PTR(tmp, sc->ntdre)
1304                         desc = sc->trans_ring + tmp;
1305                         desc->md->md1 |= ENP;
1306
1307                         if (len > 0)
1308                                 desc->md->md2 -= len;
1309
1310                         /*
1311                          * Set OWN bits in reverse order, otherwise the Lance
1312                          * could start sending the packet before all the
1313                          * buffers have been relinquished by the host.
1314                          */
1315
1316                         while (tmp != sc->next_to_send) {
1317                                 desc->md->md1 |= OWN;
1318                                 DEC_MD_PTR(tmp, sc->ntdre)
1319                                 desc = sc->trans_ring + tmp;
1320                         }
1321                         sc->next_to_send = end_of_packet;
1322                         desc->md->md1 |= STP | OWN;
1323                 } else {
1324                         sc->pending_transmits++;
1325                         desc = sc->trans_ring + sc->next_to_send;
1326                         len = mbuf_to_buffer(head, desc->buff.data);
1327                         desc->md->md3 = 0;
1328                         desc->md->md2 = -max(len, ETHER_MIN_LEN - ETHER_CRC_LEN);
1329                         desc->md->md1 |= OWN | STP | ENP;
1330                         INC_MD_PTR(sc->next_to_send, sc->ntdre)
1331                 }
1332
1333                 /* Force an immediate poll of the transmit ring */
1334                 lnc_outw(sc->rdp, TDMD | INEA);
1335
1336                 /*
1337                  * Set a timer so if the buggy Am7990.h shuts
1338                  * down we can wake it up.
1339                  */
1340
1341                 ifp->if_timer = 2;
1342
1343                 BPF_MTAP(&sc->arpcom.ac_if, head);
1344
1345                 if (sc->nic.mem_mode != DMA_MBUF)
1346                         m_freem(head);
1347
1348         } while (sc->pending_transmits < NDESC(sc->ntdre));
1349
1350         /*
1351          * Transmit ring is full so set IFF_OACTIVE
1352          * since we can't buffer any more packets.
1353          */
1354
1355         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
1356         LNCSTATS(trans_ring_full)
1357 }
1358
1359 static int
1360 lnc_ioctl(struct ifnet * ifp, u_long command, caddr_t data)
1361 {
1362
1363         struct lnc_softc *sc = ifp->if_softc;
1364         int s, error = 0;
1365
1366         s = splimp();
1367
1368         switch (command) {
1369         case SIOCSIFFLAGS:
1370 #ifdef DEBUG
1371                 if (ifp->if_flags & IFF_DEBUG)
1372                         sc->lnc_debug = 1;
1373                 else
1374                         sc->lnc_debug = 0;
1375 #endif
1376                 if (ifp->if_flags & IFF_PROMISC) {
1377                         if (!(sc->nic.mode & PROM)) {
1378                                 sc->nic.mode |= PROM;
1379                                 lnc_init(sc);
1380                         }
1381                 } else if (sc->nic.mode & PROM) {
1382                         sc->nic.mode &= ~PROM;
1383                         lnc_init(sc);
1384                 }
1385
1386                 if ((ifp->if_flags & IFF_ALLMULTI) &&
1387                     !(sc->flags & LNC_ALLMULTI)) {
1388                         sc->flags |= LNC_ALLMULTI;
1389                         lnc_init(sc);
1390                 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
1391                             (sc->flags & LNC_ALLMULTI)) {
1392                         sc->flags &= ~LNC_ALLMULTI;
1393                         lnc_init(sc);
1394                 }
1395
1396                 if ((ifp->if_flags & IFF_UP) == 0 &&
1397                     (ifp->if_flags & IFF_RUNNING) != 0) {
1398                         /*
1399                          * If interface is marked down and it is running,
1400                          * then stop it.
1401                          */
1402                         lnc_stop(sc);
1403                         ifp->if_flags &= ~IFF_RUNNING;
1404                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1405                            (ifp->if_flags & IFF_RUNNING) == 0) {
1406                         /*
1407                          * If interface is marked up and it is stopped, then
1408                          * start it.
1409                          */
1410                         lnc_init(sc);
1411                 }
1412                 break;
1413         case SIOCADDMULTI:
1414         case SIOCDELMULTI:
1415                 lnc_init(sc);
1416                 error = 0;
1417                 break;
1418         default:
1419                 error = ether_ioctl(ifp, command, data);
1420                 break;
1421         }
1422         (void) splx(s);
1423         return error;
1424 }
1425
1426 static void
1427 lnc_watchdog(struct ifnet *ifp)
1428 {
1429         log(LOG_ERR, "%s: Device timeout -- Resetting\n", ifp->if_xname);
1430         ifp->if_oerrors++;
1431         lnc_reset(ifp->if_softc);
1432 }
1433
1434 #ifdef DEBUG
1435 void
1436 lnc_dump_state(struct lnc_softc *sc)
1437 {
1438         int             i;
1439
1440         printf("\nDriver/NIC [%s] state dump\n", sc->arpcom.ac_if.if_xname);
1441         printf("Memory access mode: %b\n", sc->nic.mem_mode, MEM_MODES);
1442         printf("Host memory\n");
1443         printf("-----------\n");
1444
1445         printf("Receive ring: base = %p, next = %p\n",
1446             (void *)sc->recv_ring, (void *)(sc->recv_ring + sc->recv_next));
1447         for (i = 0; i < NDESC(sc->nrdre); i++)
1448                 printf("\t%d:%p md = %p buff = %p\n",
1449                     i, (void *)(sc->recv_ring + i),
1450                     (void *)(sc->recv_ring + i)->md,
1451                     (void *)(sc->recv_ring + i)->buff.data);
1452
1453         printf("Transmit ring: base = %p, next = %p\n",
1454             (void *)sc->trans_ring, (void *)(sc->trans_ring + sc->trans_next));
1455         for (i = 0; i < NDESC(sc->ntdre); i++)
1456                 printf("\t%d:%p md = %p buff = %p\n",
1457                     i, (void *)(sc->trans_ring + i),
1458                     (void *)(sc->trans_ring + i)->md,
1459                     (void *)(sc->trans_ring + i)->buff.data);
1460         printf("Lance memory (may be on host(DMA) or card(SHMEM))\n");
1461         printf("Init block = %p\n", (void *)sc->init_block);
1462         printf("\tmode = %b rlen:rdra = %x:%x tlen:tdra = %x:%x\n",
1463             sc->init_block->mode, INIT_MODE, sc->init_block->rlen,
1464           sc->init_block->rdra, sc->init_block->tlen, sc->init_block->tdra);
1465         printf("Receive descriptor ring\n");
1466         for (i = 0; i < NDESC(sc->nrdre); i++)
1467                 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tMCNT = %u,\tflags = %b\n",
1468                     i, ((sc->recv_ring + i)->md->md1 & HADR),
1469                     (sc->recv_ring + i)->md->md0,
1470                     -(short) (sc->recv_ring + i)->md->md2,
1471                     (sc->recv_ring + i)->md->md3,
1472                     (((sc->recv_ring + i)->md->md1 & ~HADR) >> 8), RECV_MD1);
1473         printf("Transmit descriptor ring\n");
1474         for (i = 0; i < NDESC(sc->ntdre); i++)
1475                 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tflags = %b %b\n",
1476                     i, ((sc->trans_ring + i)->md->md1 & HADR),
1477                     (sc->trans_ring + i)->md->md0,
1478                     -(short) (sc->trans_ring + i)->md->md2,
1479                     ((sc->trans_ring + i)->md->md1 >> 8), TRANS_MD1,
1480                     ((sc->trans_ring + i)->md->md3 >> 10), TRANS_MD3);
1481         printf("\nnext_to_send = %x\n", sc->next_to_send);
1482         printf("\n CSR0 = %b CSR1 = %x CSR2 = %x CSR3 = %x\n\n",
1483             read_csr(sc, CSR0), CSR0_FLAGS, read_csr(sc, CSR1),
1484             read_csr(sc, CSR2), read_csr(sc, CSR3));
1485
1486         /* Set RAP back to CSR0 */
1487         lnc_outw(sc->rap, CSR0);
1488 }
1489
1490 void
1491 mbuf_dump_chain(struct mbuf * m)
1492 {
1493
1494 #define MBUF_FLAGS \
1495         "\20\1M_EXT\2M_PKTHDR\3M_EOR\4UNKNOWN\5M_BCAST\6M_MCAST"
1496
1497         if (!m)
1498                 log(LOG_DEBUG, "m == NULL\n");
1499         do {
1500                 log(LOG_DEBUG, "m = %p\n", (void *)m);
1501                 log(LOG_DEBUG, "m_hdr.mh_next = %p\n",
1502                     (void *)m->m_hdr.mh_next);
1503                 log(LOG_DEBUG, "m_hdr.mh_nextpkt = %p\n",
1504                     (void *)m->m_hdr.mh_nextpkt);
1505                 log(LOG_DEBUG, "m_hdr.mh_len = %d\n", m->m_hdr.mh_len);
1506                 log(LOG_DEBUG, "m_hdr.mh_data = %p\n",
1507                     (void *)m->m_hdr.mh_data);
1508                 log(LOG_DEBUG, "m_hdr.mh_type = %d\n", m->m_hdr.mh_type);
1509                 log(LOG_DEBUG, "m_hdr.mh_flags = %b\n", m->m_hdr.mh_flags,
1510                     MBUF_FLAGS);
1511                 if (!(m->m_hdr.mh_flags & (M_PKTHDR | M_EXT)))
1512                         log(LOG_DEBUG, "M_dat.M_databuf = %p\n",
1513                             (void *)m->M_dat.M_databuf);
1514                 else {
1515                         if (m->m_hdr.mh_flags & M_PKTHDR) {
1516                                 log(LOG_DEBUG, "M_dat.MH.MH_pkthdr.len = %d\n",
1517                                     m->M_dat.MH.MH_pkthdr.len);
1518                                 log(LOG_DEBUG,
1519                                     "M_dat.MH.MH_pkthdr.rcvif = %p\n",
1520                                     (void *)m->M_dat.MH.MH_pkthdr.rcvif);
1521                                 if (!(m->m_hdr.mh_flags & M_EXT))
1522                                         log(LOG_DEBUG,
1523                                             "M_dat.MH.MH_dat.MH_databuf = %p\n",
1524                                         (void *)m->M_dat.MH.MH_dat.MH_databuf);
1525                         }
1526                         if (m->m_hdr.mh_flags & M_EXT) {
1527                                 log(LOG_DEBUG,
1528                                     "M_dat.MH.MH_dat.MH_ext.ext_buff %p\n",
1529                                     (void *)m->M_dat.MH.MH_dat.MH_ext.ext_buf);
1530                                 log(LOG_DEBUG,
1531                                     "M_dat.MH.MH_dat.MH_ext.ext_free %p\n",
1532                                     (void *)m->M_dat.MH.MH_dat.MH_ext.ext_free);
1533                                 log(LOG_DEBUG,
1534                                     "M_dat.MH.MH_dat.MH_ext.ext_size %d\n",
1535                                     m->M_dat.MH.MH_dat.MH_ext.ext_size);
1536                         }
1537                 }
1538         } while ((m = m->m_next) != NULL);
1539 }
1540 #endif