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