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