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