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