]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ie/if_ie.c
Merge OpenSSL 1.0.1i.
[FreeBSD/FreeBSD.git] / sys / dev / ie / if_ie.c
1 /*-
2  * Copyright (c) 1992, 1993, University of Vermont and State
3  *  Agricultural College.
4  * Copyright (c) 1992, 1993, Garrett A. Wollman.
5  *
6  * Portions:
7  * Copyright (c) 1990, 1991, William F. Jolitz
8  * Copyright (c) 1990, The Regents of the University of California
9  *
10  * 3Com 3C507 support:
11  * Copyright (c) 1993, 1994, Charles M. Hannum
12  *
13  * EtherExpress 16 support:
14  * Copyright (c) 1993, 1994, 1995, Rodney W. Grimes
15  * Copyright (c) 1997, Aaron C. Smith
16  *
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *      This product includes software developed by the University of
30  *      Vermont and State Agricultural College and Garrett A. Wollman, by
31  *      William F. Jolitz, by the University of California, Berkeley,
32  *      Lawrence Berkeley Laboratory, and their contributors, by
33  *      Charles M. Hannum, by Rodney W. Grimes, and by Aaron C. Smith.
34  * 4. Neither the names of the Universities nor the names of the authors
35  *    may be used to endorse or promote products derived from this software
36  *    without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  *
50  * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
51  */
52
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 /*
57  * Intel 82586 Ethernet chip
58  * Register, bit, and structure definitions.
59  *
60  * Written by GAW with reference to the Clarkson Packet Driver code for this
61  * chip written by Russ Nelson and others.
62  *
63  * Intel EtherExpress 16 support from if_ix.c, written by Rodney W. Grimes.
64  */
65
66 /*
67  * The i82586 is a very versatile chip, found in many implementations.
68  * Programming this chip is mostly the same, but certain details differ
69  * from card to card.  This driver is written so that different cards
70  * can be automatically detected at run-time.
71  */
72
73 /*
74  * Mode of operation:
75  *
76  * We run the 82586 in a standard Ethernet mode.  We keep NFRAMES   
77  * received frame descriptors around for the receiver to use, and   
78  * NRXBUFS associated receive buffer descriptors, both in a circular
79  * list.  Whenever a frame is received, we rotate both lists as
80  * necessary.  (The 586 treats both lists as a simple queue.)  We also
81  * keep a transmit command around so that packets can be sent off
82  * quickly.
83  *
84  * We configure the adapter in AL-LOC = 1 mode, which means that the
85  * Ethernet/802.3 MAC header is placed at the beginning of the receive
86  * buffer rather than being split off into various fields in the RFD. 
87  * This also means that we must include this header in the transmit 
88  * buffer as well.
89  *
90  * By convention, all transmit commands, and only transmit commands,
91  * shall have the I (IE_CMD_INTR) bit set in the command.  This way, 
92  * when an interrupt arrives at ieintr(), it is immediately possible
93  * to tell what precisely caused it.  ANY OTHER command-sending routines
94  * should run at splimp(), and should post an acknowledgement to every
95  * interrupt they generate.
96  *
97  * The 82586 has a 24-bit address space internally, and the adaptor's
98  * memory is located at the top of this region.  However, the value
99  * we are given in configuration is normally the *bottom* of the adaptor
100  * RAM.  So, we must go through a few gyrations to come up with a
101  * kernel virtual address which represents the actual beginning of the
102  * 586 address space.  First, we autosize the RAM by running through
103  * several possible sizes and trying to initialize the adapter under
104  * the assumption that the selected size is correct.  Then, knowing
105  * the correct RAM size, we set up our pointers in the softc `iomem'
106  * represents the computed base of the 586 address space.  `iomembot'
107  * represents the actual configured base of adapter RAM.  Finally,
108  * `iosize' represents the calculated size of 586 RAM.  Then, when
109  * laying out commands, we use the interval [iomembot, iomembot +
110  * iosize); to make 24-pointers, we subtract iomem, and to make
111  * 16-pointers, we subtract iomem and and with 0xffff.
112  */
113
114 #include <sys/param.h>
115 #include <sys/systm.h>
116 #include <sys/eventhandler.h>
117 #include <sys/kernel.h>
118 #include <sys/malloc.h>
119 #include <sys/mbuf.h>
120 #include <sys/socket.h>
121 #include <sys/sockio.h>
122 #include <sys/syslog.h>
123
124 #include <sys/module.h>
125 #include <sys/bus.h>
126
127 #include <machine/bus.h>
128 #include <machine/resource.h>
129 #include <sys/rman.h>
130
131 #include <net/ethernet.h>
132 #include <net/if.h>
133 #include <net/if_var.h>
134 #include <net/if_types.h>
135 #include <net/if_dl.h>
136
137 #include <netinet/in.h>
138 #include <netinet/if_ether.h>
139
140 #include <dev/ic/i82586.h>
141 #include <dev/ie/if_ievar.h>
142 #include <dev/ie/if_iereg.h>
143 #include <dev/ie/if_ie507.h>
144 #include <dev/ie/if_iee16.h>
145 #include <i386/isa/elink.h>
146
147 #include <net/bpf.h>
148
149 #ifdef DEBUG
150 #define IED_RINT        0x01
151 #define IED_TINT        0x02
152 #define IED_RNR         0x04
153 #define IED_CNA         0x08
154 #define IED_READFRAME   0x10
155 static int      ie_debug = IED_RNR;
156
157 #endif
158
159 #define IE_BUF_LEN      ETHER_MAX_LEN   /* length of transmit buffer */
160
161 /* XXX this driver uses `volatile' and `caddr_t' to a fault. */
162 typedef volatile char *v_caddr_t;       /* core address, pointer to volatile */
163
164 /* Forward declaration */
165 struct ie_softc;
166
167 static void     ieinit                  (void *);
168 static void     ieinit_locked           (struct ie_softc *);
169 static void     ie_stop                 (struct ie_softc *);
170 static int      ieioctl                 (struct ifnet *, u_long, caddr_t);
171 static void     iestart                 (struct ifnet *);
172 static void     iestart_locked          (struct ifnet *);
173
174 static __inline void
175                 ee16_interrupt_enable   (struct ie_softc *);
176
177 static __inline void
178                 ie_ack                  (struct ie_softc *, u_int);
179 static void     iereset                 (struct ie_softc *);
180 static void     ie_readframe            (struct ie_softc *, int);
181 static void     ie_drop_packet_buffer   (struct ie_softc *);
182 static int      command_and_wait        (struct ie_softc *,
183                                          int, void volatile *, int);
184 static void     run_tdr                 (struct ie_softc *,
185                                          volatile struct ie_tdr_cmd *);
186 static int      ierint                  (struct ie_softc *);
187 static int      ietint                  (struct ie_softc *);
188 static int      iernr                   (struct ie_softc *);
189 static void     start_receiver          (struct ie_softc *);
190 static __inline int
191                 ieget                   (struct ie_softc *, struct mbuf **);
192 static v_caddr_t setup_rfa              (struct ie_softc *, v_caddr_t);
193 static int      mc_setup                (struct ie_softc *);
194 static void     ie_mc_reset             (struct ie_softc *);
195
196 #ifdef DEBUG
197 static void     print_rbd               (volatile struct ie_recv_buf_desc * rbd);
198 static int      in_ierint = 0;
199 static int      in_ietint = 0;
200 #endif
201
202 static const char *ie_hardware_names[] = {
203         "None",
204         "StarLAN 10",
205         "EN100",
206         "StarLAN Fiber",
207         "3C507",
208         "NI5210",
209         "EtherExpress 16",
210         "Unknown"
211 };
212
213 /*
214  * sizeof(iscp) == 1+1+2+4 == 8
215  * sizeof(scb) == 2+2+2+2+2+2+2+2 == 16
216  * NFRAMES * sizeof(rfd) == NFRAMES*(2+2+2+2+6+6+2+2) == NFRAMES*24 == 384
217  * sizeof(xmit_cmd) == 2+2+2+2+6+2 == 18
218  * sizeof(transmit buffer) == 1512
219  * sizeof(transmit buffer desc) == 8
220  * -----
221  * 1946
222  * 
223  * NRXBUFS * sizeof(rbd) == NRXBUFS*(2+2+4+2+2) == NRXBUFS*12
224  * NRXBUFS * IE_RBUF_SIZE == NRXBUFS*256
225  * 
226  * NRXBUFS should be (16384 - 1946) / (256 + 12) == 14438 / 268 == 53
227  * 
228  * With NRXBUFS == 48, this leaves us 1574 bytes for another command or
229  * more buffers.  Another transmit command would be 18+8+1512 == 1538
230  * ---just barely fits!
231  * 
232  * Obviously all these would have to be reduced for smaller memory sizes.
233  * With a larger memory, it would be possible to roughly double the number
234  * of both transmit and receive buffers.
235  */
236
237 #define NFRAMES         4       /* number of receive frames */
238 #define NRXBUFS         24      /* number of buffers to allocate */
239 #define IE_RBUF_SIZE    256     /* size of each buffer, MUST BE POWER OF TWO */
240 #define NTXBUFS         1       /* number of transmit commands */
241 #define IE_TBUF_SIZE    ETHER_MAX_LEN   /* size of transmit buffer */
242
243 #define MK_24(base, ptr) ((caddr_t)((uintptr_t)ptr - (uintptr_t)base))
244 #define MK_16(base, ptr) ((u_short)(uintptr_t)MK_24(base, ptr))
245
246 void
247 ee16_shutdown(struct ie_softc *sc)
248 {
249
250         ee16_reset_586(sc);
251         outb(PORT(sc) + IEE16_ECTRL, IEE16_RESET_ASIC);
252         outb(PORT(sc) + IEE16_ECTRL, 0);
253 }
254
255 /*
256  * Taken almost exactly from Bill's if_is.c, then modified beyond recognition.
257  */
258 int
259 ie_attach(device_t dev)
260 {
261         struct ie_softc *       sc;
262         struct ifnet *          ifp;
263         size_t                  allocsize;
264         int                     error, factor;
265
266         sc = device_get_softc(dev);
267         ifp = sc->ifp = if_alloc(IFT_ETHER);
268         if (ifp == NULL) {
269                 device_printf(sc->dev, "can not if_alloc()\n");
270                 return (ENOSPC);
271         }
272
273         sc->dev = dev;
274         mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
275             MTX_DEF);
276
277         /*
278          * based on the amount of memory we have, allocate our tx and rx
279          * resources.
280          */
281         factor = rman_get_size(sc->mem_res) / 8192;
282         sc->nframes = factor * NFRAMES;
283         sc->nrxbufs = factor * NRXBUFS;
284         sc->ntxbufs = factor * NTXBUFS;
285
286         /*
287          * Since all of these guys are arrays of pointers, allocate as one
288          * big chunk and dole out accordingly.
289          */
290         allocsize = sizeof(void *) * (sc->nframes
291                                       + (sc->nrxbufs * 2)
292                                       + (sc->ntxbufs * 3));
293         sc->rframes = (volatile struct ie_recv_frame_desc **) malloc(allocsize,
294                                                                      M_DEVBUF,
295                                                                    M_NOWAIT);
296         if (sc->rframes == NULL) {
297                 mtx_destroy(&sc->lock);
298                 return (ENXIO);
299         }
300         sc->rbuffs =
301             (volatile struct ie_recv_buf_desc **)&sc->rframes[sc->nframes];
302         sc->cbuffs = (volatile u_char **)&sc->rbuffs[sc->nrxbufs];
303         sc->xmit_cmds =
304             (volatile struct ie_xmit_cmd **)&sc->cbuffs[sc->nrxbufs];
305         sc->xmit_buffs =
306             (volatile struct ie_xmit_buf **)&sc->xmit_cmds[sc->ntxbufs];
307         sc->xmit_cbuffs = (volatile u_char **)&sc->xmit_buffs[sc->ntxbufs];
308
309         if (bootverbose)
310                 device_printf(sc->dev, "hardware type %s, revision %d\n",
311                         ie_hardware_names[sc->hard_type], sc->hard_vers + 1);
312
313         ifp->if_softc = sc;
314         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
315         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
316         ifp->if_start = iestart;
317         ifp->if_ioctl = ieioctl;
318         ifp->if_init = ieinit;
319         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
320
321         ether_ifattach(ifp, sc->enaddr);
322
323         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
324                                 NULL, ie_intr, sc, &sc->irq_ih);
325         if (error) {
326                 device_printf(dev, "Unable to register interrupt handler\n"); 
327                 mtx_destroy(&sc->lock);
328                 return (error);
329         }
330
331         return (0);
332 }
333
334 static __inline void
335 ie_ack(struct ie_softc *sc, u_int mask)
336 {
337
338         sc->scb->ie_command = sc->scb->ie_status & mask;
339         (*sc->ie_chan_attn) (sc);
340 }
341
342 /*
343  * What to do upon receipt of an interrupt.
344  */
345 void
346 ie_intr(void *xsc)
347 {
348         struct ie_softc *sc = (struct ie_softc *)xsc;
349         u_short status;
350
351         IE_LOCK(sc);
352
353         /* Clear the interrupt latch on the 3C507. */
354         if (sc->hard_type == IE_3C507
355          && (inb(PORT(sc) + IE507_CTRL) & EL_CTRL_INTL))
356                 outb(PORT(sc) + IE507_ICTRL, 1);
357
358         /* disable interrupts on the EE16. */
359         if (sc->hard_type == IE_EE16)
360                 outb(PORT(sc) + IEE16_IRQ, sc->irq_encoded);
361
362         status = sc->scb->ie_status;
363
364 loop:
365
366         /* Don't ack interrupts which we didn't receive */
367         ie_ack(sc, IE_ST_WHENCE & status);
368
369         if (status & (IE_ST_RECV | IE_ST_RNR)) {
370 #ifdef DEBUG
371                 in_ierint++;
372                 if (ie_debug & IED_RINT)
373                         if_printf(sc->ifp, "rint\n");
374 #endif
375                 ierint(sc);
376 #ifdef DEBUG
377                 in_ierint--;
378 #endif
379         }
380         if (status & IE_ST_DONE) {
381 #ifdef DEBUG
382                 in_ietint++;
383                 if (ie_debug & IED_TINT)
384                         if_printf(sc->ifp, "tint\n");
385 #endif
386                 ietint(sc);
387 #ifdef DEBUG
388                 in_ietint--;
389 #endif
390         }
391         if (status & IE_ST_RNR) {
392 #ifdef DEBUG
393                 if (ie_debug & IED_RNR)
394                         if_printf(sc->ifp, "rnr\n");
395 #endif
396                 iernr(sc);
397         }
398 #ifdef DEBUG
399         if ((status & IE_ST_ALLDONE) && (ie_debug & IED_CNA))
400                 if_printf(sc->ifp, "cna\n");
401 #endif
402
403         if ((status = sc->scb->ie_status) & IE_ST_WHENCE)
404                 goto loop;
405
406         /* Clear the interrupt latch on the 3C507. */
407         if (sc->hard_type == IE_3C507)
408                 outb(PORT(sc) + IE507_ICTRL, 1);
409
410         /* enable interrupts on the EE16. */
411         if (sc->hard_type == IE_EE16)
412                 outb(PORT(sc) + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
413         IE_UNLOCK(sc);
414 }
415
416 /*
417  * Process a received-frame interrupt.
418  */
419 static int
420 ierint(struct ie_softc *sc)
421 {
422         int     i, status;
423         static int timesthru = 1024;
424
425         i = sc->rfhead;
426         while (1) {
427                 status = sc->rframes[i]->ie_fd_status;
428
429                 if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
430                         sc->ifp->if_ipackets++;
431                         if (!--timesthru) {
432                                 sc->ifp->if_ierrors +=
433                                     sc->scb->ie_err_crc +
434                                     sc->scb->ie_err_align +
435                                     sc->scb->ie_err_resource +
436                                     sc->scb->ie_err_overrun;
437                                 sc->scb->ie_err_crc = 0;
438                                 sc->scb->ie_err_align = 0;
439                                 sc->scb->ie_err_resource = 0;
440                                 sc->scb->ie_err_overrun = 0;
441                                 timesthru = 1024;
442                         }
443                         ie_readframe(sc, i);
444                 } else {
445                         if (status & IE_FD_RNR) {
446                                 if (!(sc->scb->ie_status & IE_RU_READY)) {
447                                         sc->rframes[0]->ie_fd_next =
448                                             MK_16(MEM(sc), sc->rbuffs[0]);
449                                         sc->scb->ie_recv_list =
450                                             MK_16(MEM(sc), sc->rframes[0]);
451                                         command_and_wait(sc, IE_RU_START, 0, 0);
452                                 }
453                         }
454                         break;
455                 }
456                 i = (i + 1) % sc->nframes;
457         }
458         return (0);
459 }
460
461 /*
462  * Process a command-complete interrupt.  These are only generated by
463  * the transmission of frames.  This routine is deceptively simple, since
464  * most of the real work is done by iestart().
465  */
466 static int
467 ietint(struct ie_softc *sc)
468 {
469         struct ifnet *ifp = sc->ifp;
470         int     status;
471         int     i;
472
473         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
474
475         for (i = 0; i < sc->xmit_count; i++) {
476                 status = sc->xmit_cmds[i]->ie_xmit_status;
477
478                 if (status & IE_XS_LATECOLL) {
479                         if_printf(ifp, "late collision\n");
480                         ifp->if_collisions++;
481                         ifp->if_oerrors++;
482                 } else if (status & IE_XS_NOCARRIER) {
483                         if_printf(ifp, "no carrier\n");
484                         ifp->if_oerrors++;
485                 } else if (status & IE_XS_LOSTCTS) {
486                         if_printf(ifp, "lost CTS\n");
487                         ifp->if_oerrors++;
488                 } else if (status & IE_XS_UNDERRUN) {
489                         if_printf(ifp, "DMA underrun\n");
490                         ifp->if_oerrors++;
491                 } else if (status & IE_XS_EXCMAX) {
492                         if_printf(ifp, "too many collisions\n");
493                         ifp->if_collisions += 16;
494                         ifp->if_oerrors++;
495                 } else {
496                         ifp->if_opackets++;
497                         ifp->if_collisions += status & IE_XS_MAXCOLL;
498                 }
499         }
500         sc->xmit_count = 0;
501
502         /*
503          * If multicast addresses were added or deleted while we were
504          * transmitting, ie_mc_reset() set the want_mcsetup flag indicating
505          * that we should do it.
506          */
507         if (sc->want_mcsetup) {
508                 mc_setup(sc);
509                 sc->want_mcsetup = 0;
510         }
511         /* Wish I knew why this seems to be necessary... */
512         sc->xmit_cmds[0]->ie_xmit_status |= IE_STAT_COMPL;
513
514         iestart_locked(ifp);
515         return (0);             /* shouldn't be necessary */
516 }
517
518 /*
519  * Process a receiver-not-ready interrupt.  I believe that we get these
520  * when there aren't enough buffers to go around.  For now (FIXME), we
521  * just restart the receiver, and hope everything's ok.
522  */
523 static int
524 iernr(struct ie_softc *sc)
525 {
526 #ifdef doesnt_work
527         setup_rfa(sc, (v_caddr_t) sc->rframes[0]);
528
529         sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
530         command_and_wait(sc, IE_RU_START, 0, 0);
531 #else
532         /* This doesn't work either, but it doesn't hang either. */
533         command_and_wait(sc, IE_RU_DISABLE, 0, 0);      /* just in case */
534         setup_rfa(sc, (v_caddr_t) sc->rframes[0]);      /* ignore cast-qual */
535
536         sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
537         command_and_wait(sc, IE_RU_START, 0, 0);        /* was ENABLE */
538
539 #endif
540         ie_ack(sc, IE_ST_WHENCE);
541
542         sc->ifp->if_ierrors++;
543         return (0);
544 }
545
546 /*
547  * Compare two Ether/802 addresses for equality, inlined and
548  * unrolled for speed.  I'd love to have an inline assembler
549  * version of this...
550  */
551 static __inline int
552 ether_equal(u_char * one, u_char * two)
553 {
554         if (one[0] != two[0])
555                 return (0);
556         if (one[1] != two[1])
557                 return (0);
558         if (one[2] != two[2])
559                 return (0);
560         if (one[3] != two[3])
561                 return (0);
562         if (one[4] != two[4])
563                 return (0);
564         if (one[5] != two[5])
565                 return (0);
566         return 1;
567 }
568
569 /*
570  * Determine quickly whether we should bother reading in this packet.
571  * This depends on whether BPF and/or bridging is enabled, whether we
572  * are receiving multicast address, and whether promiscuous mode is enabled.
573  * We assume that if IFF_PROMISC is set, then *somebody* wants to see
574  * all incoming packets.
575  */
576 static __inline int
577 check_eh(struct ie_softc *sc, struct ether_header *eh)
578 {
579         /* Optimize the common case: normal operation. We've received
580            either a unicast with our dest or a multicast packet. */
581         if (sc->promisc == 0) {
582                 int i;
583
584                 /* If not multicast, it's definitely for us */
585                 if ((eh->ether_dhost[0] & 1) == 0)
586                         return (1);
587
588                 /* Accept broadcasts (loose but fast check) */
589                 if (eh->ether_dhost[0] == 0xff)
590                         return (1);
591
592                 /* Compare against our multicast addresses */
593                 for (i = 0; i < sc->mcast_count; i++) {
594                         if (ether_equal(eh->ether_dhost,
595                             (u_char *)&sc->mcast_addrs[i]))
596                                 return (1);
597                 }
598                 return (0);
599         }
600
601         /* Always accept packets when in promiscuous mode */
602         if ((sc->promisc & IFF_PROMISC) != 0)
603                 return (1);
604
605         /* Always accept packets directed at us */
606         if (ether_equal(eh->ether_dhost, IF_LLADDR(sc->ifp)))
607                 return (1);
608
609         /* Must have IFF_ALLMULTI but not IFF_PROMISC set. The chip is
610            actually in promiscuous mode, so discard unicast packets. */
611         return((eh->ether_dhost[0] & 1) != 0);
612 }
613
614 /*
615  * We want to isolate the bits that have meaning...  This assumes that
616  * IE_RBUF_SIZE is an even power of two.  If somehow the act_len exceeds
617  * the size of the buffer, then we are screwed anyway.
618  */
619 static __inline int
620 ie_buflen(struct ie_softc *sc, int head)
621 {
622         return (sc->rbuffs[head]->ie_rbd_actual
623                 & (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
624 }
625
626 static __inline int
627 ie_packet_len(struct ie_softc *sc)
628 {
629         int     i;
630         int     head = sc->rbhead;
631         int     acc = 0;
632
633         do {
634                 if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
635 #ifdef DEBUG
636                         print_rbd(sc->rbuffs[sc->rbhead]);
637 #endif
638                         log(LOG_ERR,
639                             "%s: receive descriptors out of sync at %d\n",
640                             sc->ifp->if_xname, sc->rbhead);
641                         iereset(sc);
642                         return (-1);
643                 }
644                 i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
645
646                 acc += ie_buflen(sc, head);
647                 head = (head + 1) % sc->nrxbufs;
648         } while (!i);
649
650         return (acc);
651 }
652
653 /*
654  * Read data off the interface, and turn it into an mbuf chain.
655  *
656  * This code is DRAMATICALLY different from the previous version; this
657  * version tries to allocate the entire mbuf chain up front, given the
658  * length of the data available.  This enables us to allocate mbuf
659  * clusters in many situations where before we would have had a long
660  * chain of partially-full mbufs.  This should help to speed up the
661  * operation considerably.  (Provided that it works, of course.)
662  */
663 static __inline int
664 ieget(struct ie_softc *sc, struct mbuf **mp)
665 {
666         struct  ether_header eh;
667         struct  mbuf *m, *top, **mymp;
668         int     offset;
669         int     totlen, resid;
670         int     thismboff;
671         int     head;
672
673         totlen = ie_packet_len(sc);
674         if (totlen <= 0)
675                 return (-1);
676
677         /*
678          * Snarf the Ethernet header.
679          */
680         bcopy(sc->cbuffs[sc->rbhead], &eh, sizeof(struct ether_header));
681         /* ignore cast-qual warning here */
682
683         /*
684          * As quickly as possible, check if this packet is for us. If not,
685          * don't waste a single cycle copying the rest of the packet in.
686          * This is only a consideration when FILTER is defined; i.e., when
687          * we are either running BPF or doing multicasting.
688          */
689         if (!check_eh(sc, &eh)) {
690                 ie_drop_packet_buffer(sc);
691                 sc->ifp->if_ierrors--;  /* just this case, it's not an
692                                                  * error
693                                                  */
694                 return (-1);
695         }
696
697         MGETHDR(m, M_NOWAIT, MT_DATA);
698         if (!m) {
699                 ie_drop_packet_buffer(sc);
700                 /* XXXX if_ierrors++; */
701                 return (-1);
702         }
703
704         *mp = m;
705         m->m_pkthdr.rcvif = sc->ifp;
706         m->m_len = MHLEN;
707         resid = m->m_pkthdr.len = totlen;
708         top = 0;
709
710         mymp = &top;
711
712         /*
713          * This loop goes through and allocates mbufs for all the data we
714          * will be copying in.  It does not actually do the copying yet.
715          */
716         do {                    /* while(resid > 0) */
717                 /*
718                  * Try to allocate an mbuf to hold the data that we have.
719                  * If we already allocated one, just get another one and
720                  * stick it on the end (eventually).  If we don't already
721                  * have one, try to allocate an mbuf cluster big enough to
722                  * hold the whole packet, if we think it's reasonable, or a
723                  * single mbuf which may or may not be big enough. Got that?
724                  */
725                 if (top) {
726                         MGET(m, M_NOWAIT, MT_DATA);
727                         if (!m) {
728                                 m_freem(top);
729                                 ie_drop_packet_buffer(sc);
730                                 return (-1);
731                         }
732                         m->m_len = MLEN;
733                 }
734                 if (resid >= MINCLSIZE) {
735                         MCLGET(m, M_NOWAIT);
736                         if (m->m_flags & M_EXT)
737                                 m->m_len = min(resid, MCLBYTES);
738                 } else {
739                         if (resid < m->m_len) {
740                                 if (!top && resid + max_linkhdr <= m->m_len)
741                                         m->m_data += max_linkhdr;
742                                 m->m_len = resid;
743                         }
744                 }
745                 resid -= m->m_len;
746                 *mymp = m;
747                 mymp = &m->m_next;
748         } while (resid > 0);
749
750         resid = totlen;                                 /* remaining data */
751         offset = 0;                                     /* packet offset */
752         thismboff = 0;                                  /* offset in m */
753
754         m = top;                                        /* current mbuf */
755         head = sc->rbhead;                              /* current rx buffer */
756
757         /*
758          * Now we take the mbuf chain (hopefully only one mbuf most of the
759          * time) and stuff the data into it.  There are no possible failures
760          * at or after this point.
761          */
762         while (resid > 0) {     /* while there's stuff left */
763                 int     thislen = ie_buflen(sc, head) - offset;
764
765                 /*
766                  * If too much data for the current mbuf, then fill the
767                  * current one up, go to the next one, and try again.
768                  */
769                 if (thislen > m->m_len - thismboff) {
770                         int     newlen = m->m_len - thismboff;
771
772                         bcopy((v_caddr_t) (sc->cbuffs[head] + offset),
773                               mtod(m, caddr_t) +thismboff, (unsigned) newlen);
774                         /* ignore cast-qual warning */
775                         m = m->m_next;
776                         thismboff = 0;          /* new mbuf, so no offset */
777                         offset += newlen;       /* we are now this far into
778                                                  * the packet */
779                         resid -= newlen;        /* so there is this much left
780                                                  * to get */
781                         continue;
782                 }
783                 /*
784                  * If there is more than enough space in the mbuf to hold
785                  * the contents of this buffer, copy everything in, advance
786                  * pointers, and so on.
787                  */
788                 if (thislen < m->m_len - thismboff) {
789                         bcopy((v_caddr_t) (sc->cbuffs[head] + offset),
790                             mtod(m, caddr_t) +thismboff, (unsigned) thislen);
791                         thismboff += thislen;   /* we are this far into the
792                                                  * mbuf */
793                         resid -= thislen;       /* and this much is left */
794                         goto nextbuf;
795                 }
796                 /*
797                  * Otherwise, there is exactly enough space to put this
798                  * buffer's contents into the current mbuf.  Do the
799                  * combination of the above actions.
800                  */
801                 bcopy((v_caddr_t) (sc->cbuffs[head] + offset),
802                       mtod(m, caddr_t) + thismboff, (unsigned) thislen);
803                 m = m->m_next;
804                 thismboff = 0;          /* new mbuf, start at the beginning */
805                 resid -= thislen;       /* and we are this far through */
806
807                 /*
808                  * Advance all the pointers.  We can get here from either of
809                  * the last two cases, but never the first.
810                  */
811 nextbuf:
812                 offset = 0;
813                 sc->rbuffs[head]->ie_rbd_actual = 0;
814                 sc->rbuffs[head]->ie_rbd_length |= IE_RBD_LAST;
815                 sc->rbhead = head = (head + 1) % sc->nrxbufs;
816                 sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
817                 sc->rbtail = (sc->rbtail + 1) % sc->nrxbufs;
818         }
819
820         /*
821          * Unless something changed strangely while we were doing the copy,
822          * we have now copied everything in from the shared memory. This
823          * means that we are done.
824          */
825         return (0);
826 }
827
828 /*
829  * Read frame NUM from unit UNIT (pre-cached as IE).
830  *
831  * This routine reads the RFD at NUM, and copies in the buffers from
832  * the list of RBD, then rotates the RBD and RFD lists so that the receiver
833  * doesn't start complaining.  Trailers are DROPPED---there's no point
834  * in wasting time on confusing code to deal with them.  Hopefully,
835  * this machine will never ARP for trailers anyway.
836  */
837 static void
838 ie_readframe(struct ie_softc *sc, int   num/* frame number to read */)
839 {
840         struct ifnet *ifp = sc->ifp;
841         struct ie_recv_frame_desc rfd;
842         struct mbuf *m = 0;
843 #ifdef DEBUG
844         struct ether_header *eh;
845 #endif
846
847         bcopy((v_caddr_t) (sc->rframes[num]), &rfd,
848               sizeof(struct ie_recv_frame_desc));
849
850         /*
851          * Immediately advance the RFD list, since we we have copied ours
852          * now.
853          */
854         sc->rframes[num]->ie_fd_status = 0;
855         sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
856         sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
857         sc->rftail = (sc->rftail + 1) % sc->nframes;
858         sc->rfhead = (sc->rfhead + 1) % sc->nframes;
859
860         if (rfd.ie_fd_status & IE_FD_OK) {
861                 if (ieget(sc, &m)) {
862                         sc->ifp->if_ierrors++;  /* this counts as an
863                                                          * error */
864                         return;
865                 }
866         }
867 #ifdef DEBUG
868         eh = mtod(m, struct ether_header *);
869         if (ie_debug & IED_READFRAME) {
870                 if_printf(ifp, "frame from ether %6D type %x\n",
871                        eh->ether_shost, ":", (unsigned) eh->ether_type);
872         }
873         if (ntohs(eh->ether_type) > ETHERTYPE_TRAIL
874             && ntohs(eh->ether_type) < (ETHERTYPE_TRAIL + ETHERTYPE_NTRAILER))
875                 printf("received trailer!\n");
876 #endif
877
878         if (!m)
879                 return;
880
881         /*
882          * Finally pass this packet up to higher layers.
883          */
884         IE_UNLOCK(sc);
885         (*ifp->if_input)(ifp, m);
886         IE_LOCK(sc);
887 }
888
889 static void
890 ie_drop_packet_buffer(struct ie_softc *sc)
891 {
892         int     i;
893
894         do {
895                 /*
896                  * This means we are somehow out of sync.  So, we reset the
897                  * adapter.
898                  */
899                 if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
900 #ifdef DEBUG
901                         print_rbd(sc->rbuffs[sc->rbhead]);
902 #endif
903                         log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
904                             sc->ifp->if_xname, sc->rbhead);
905                         iereset(sc);
906                         return;
907                 }
908                 i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
909
910                 sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
911                 sc->rbuffs[sc->rbhead]->ie_rbd_actual = 0;
912                 sc->rbhead = (sc->rbhead + 1) % sc->nrxbufs;
913                 sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
914                 sc->rbtail = (sc->rbtail + 1) % sc->nrxbufs;
915         } while (!i);
916 }
917
918
919 /*
920  * Start transmission on an interface.
921  */
922 static void
923 iestart(struct ifnet *ifp)
924 {
925         struct   ie_softc *sc = ifp->if_softc;
926
927         IE_LOCK(sc);
928         iestart_locked(ifp);
929         IE_UNLOCK(sc);
930 }
931
932 static void
933 iestart_locked(struct ifnet *ifp)
934 {
935         struct   ie_softc *sc = ifp->if_softc;
936         struct   mbuf *m0, *m;
937         volatile unsigned char *buffer;
938         u_short  len;
939
940         /*
941          * This is not really volatile, in this routine, but it makes gcc
942          * happy.
943          */
944         volatile u_short *bptr = &sc->scb->ie_command_list;
945
946         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
947                 return;
948         if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
949                 return;
950
951         do {
952                 IF_DEQUEUE(&sc->ifp->if_snd, m);
953                 if (!m)
954                         break;
955
956                 buffer = sc->xmit_cbuffs[sc->xmit_count];
957                 len = 0;
958
959                 for (m0 = m; m && len < IE_BUF_LEN; m = m->m_next) {
960                         bcopy(mtod(m, caddr_t), buffer, m->m_len);
961                         buffer += m->m_len;
962                         len += m->m_len;
963                 }
964
965                 m_freem(m0);
966                 len = max(len, ETHER_MIN_LEN);
967
968                 /*
969                  * See if bpf is listening on this interface, let it see the
970                  * packet before we commit it to the wire.
971                  */
972                 BPF_TAP(sc->ifp,
973                         (void *)sc->xmit_cbuffs[sc->xmit_count], len);
974
975                 sc->xmit_buffs[sc->xmit_count]->ie_xmit_flags =
976                     IE_XMIT_LAST|len;
977                 sc->xmit_buffs[sc->xmit_count]->ie_xmit_next = 0xffff;
978                 sc->xmit_buffs[sc->xmit_count]->ie_xmit_buf =
979                     MK_24(sc->iomem, sc->xmit_cbuffs[sc->xmit_count]);
980
981                 sc->xmit_cmds[sc->xmit_count]->com.ie_cmd_cmd = IE_CMD_XMIT;
982                 sc->xmit_cmds[sc->xmit_count]->ie_xmit_status = 0;
983                 sc->xmit_cmds[sc->xmit_count]->ie_xmit_desc =
984                     MK_16(sc->iomem, sc->xmit_buffs[sc->xmit_count]);
985
986                 *bptr = MK_16(sc->iomem, sc->xmit_cmds[sc->xmit_count]);
987                 bptr = &sc->xmit_cmds[sc->xmit_count]->com.ie_cmd_link;
988                 sc->xmit_count++;
989         } while (sc->xmit_count < sc->ntxbufs);
990
991         /*
992          * If we queued up anything for transmission, send it.
993          */
994         if (sc->xmit_count) {
995                 sc->xmit_cmds[sc->xmit_count - 1]->com.ie_cmd_cmd |=
996                     IE_CMD_LAST | IE_CMD_INTR;
997
998                 /*
999                  * By passing the command pointer as a null, we tell
1000                  * command_and_wait() to pretend that this isn't an action
1001                  * command.  I wish I understood what was happening here.
1002                  */
1003                 command_and_wait(sc, IE_CU_START, 0, 0);
1004                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1005         }
1006         return;
1007 }
1008
1009 /*
1010  * Check to see if there's an 82586 out there.
1011  */
1012 int
1013 check_ie_present(struct ie_softc *sc)
1014 {
1015         volatile struct ie_sys_conf_ptr *scp;
1016         volatile struct ie_int_sys_conf_ptr *iscp;
1017         volatile struct ie_sys_ctl_block *scb;
1018         u_long  realbase;
1019
1020         realbase = (uintptr_t) sc->iomembot + sc->iosize  - (1 << 24);
1021
1022         scp = (volatile struct ie_sys_conf_ptr *) (uintptr_t)
1023               (realbase + IE_SCP_ADDR);
1024         bzero((volatile char *) scp, sizeof *scp);
1025
1026         /*
1027          * First we put the ISCP at the bottom of memory; this tests to make
1028          * sure that our idea of the size of memory is the same as the
1029          * controller's. This is NOT where the ISCP will be in normal
1030          * operation.
1031          */
1032         iscp = (volatile struct ie_int_sys_conf_ptr *) sc->iomembot;
1033         bzero((volatile char *)iscp, sizeof *iscp);
1034
1035         scb = (volatile struct ie_sys_ctl_block *) sc->iomembot;
1036         bzero((volatile char *)scb, sizeof *scb);
1037
1038         scp->ie_bus_use = sc->bus_use;  /* 8-bit or 16-bit */
1039         scp->ie_iscp_ptr = (caddr_t) (uintptr_t)
1040             ((volatile char *) iscp - (volatile char *) (uintptr_t) realbase);
1041
1042         iscp->ie_busy = 1;
1043         iscp->ie_scb_offset = MK_16(realbase, scb) + 256;
1044
1045         (*sc->ie_reset_586) (sc);
1046         (*sc->ie_chan_attn) (sc);
1047
1048         DELAY(100);             /* wait a while... */
1049
1050         if (iscp->ie_busy) {
1051                 return (0);
1052         }
1053         /*
1054          * Now relocate the ISCP to its real home, and reset the controller
1055          * again.
1056          */
1057         iscp = (void *) Align((caddr_t) (uintptr_t)
1058                               (realbase + IE_SCP_ADDR -
1059                                sizeof(struct ie_int_sys_conf_ptr)));
1060         bzero((volatile char *) iscp, sizeof *iscp);    /* ignore cast-qual */
1061
1062         scp->ie_iscp_ptr = (caddr_t) (uintptr_t)
1063             ((volatile char *) iscp - (volatile char *) (uintptr_t) realbase);
1064
1065         iscp->ie_busy = 1;
1066         iscp->ie_scb_offset = MK_16(realbase, scb);
1067
1068         (*sc->ie_reset_586) (sc);
1069         (*sc->ie_chan_attn) (sc);
1070
1071         DELAY(100);
1072
1073         if (iscp->ie_busy) {
1074                 return (0);
1075         }
1076         sc->iomem = (caddr_t) (uintptr_t) realbase;
1077
1078         sc->iscp = iscp;
1079         sc->scb = scb;
1080
1081         /*
1082          * Acknowledge any interrupts we may have caused...
1083          */
1084         ie_ack(sc, IE_ST_WHENCE);
1085
1086         return (1);
1087 }
1088
1089 void
1090 el_reset_586(struct ie_softc *sc)
1091 {
1092         outb(PORT(sc) + IE507_CTRL, EL_CTRL_RESET);
1093         DELAY(100);
1094         outb(PORT(sc) + IE507_CTRL, EL_CTRL_NORMAL);
1095         DELAY(100);
1096 }
1097
1098 void
1099 sl_reset_586(struct ie_softc *sc)
1100 {
1101         outb(PORT(sc) + IEATT_RESET, 0);
1102 }
1103
1104 void
1105 ee16_reset_586(struct ie_softc *sc)
1106 {
1107         outb(PORT(sc) + IEE16_ECTRL, IEE16_RESET_586);
1108         DELAY(100);
1109         outb(PORT(sc) + IEE16_ECTRL, 0);
1110         DELAY(100);
1111 }
1112
1113 void
1114 el_chan_attn(struct ie_softc *sc)
1115 {
1116         outb(PORT(sc) + IE507_ATTN, 1);
1117 }
1118
1119 void
1120 sl_chan_attn(struct ie_softc *sc)
1121 {
1122         outb(PORT(sc) + IEATT_ATTN, 0);
1123 }
1124
1125 void
1126 ee16_chan_attn(struct ie_softc *sc)
1127 {
1128         outb(PORT(sc) + IEE16_ATTN, 0);
1129 }
1130
1131 static __inline void
1132 ee16_interrupt_enable(struct ie_softc *sc)
1133 {
1134         DELAY(100);
1135         outb(sc->port + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
1136         DELAY(100);
1137 }
1138
1139 void
1140 sl_read_ether(struct ie_softc *sc, unsigned char *addr)
1141 {
1142         int     i;
1143
1144         for (i = 0; i < 6; i++)
1145                 addr[i] = inb(PORT(sc) + i);
1146 }
1147
1148 static void
1149 iereset(struct ie_softc *sc)
1150 {
1151         struct ifnet *ifp = sc->ifp;
1152
1153         if_printf(ifp, "reset\n");
1154         ie_stop(sc);
1155
1156         /*
1157          * Stop i82586 dead in its tracks.
1158          */
1159         if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1160                 if_printf(ifp, "abort commands timed out\n");
1161
1162         if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1163                 if_printf(ifp, "disable commands timed out\n");
1164
1165 #ifdef notdef
1166         if (!check_ie_present(sc))
1167                 panic("ie disappeared!");
1168 #endif
1169
1170         if (ifp->if_flags & IFF_UP)
1171                 ieinit_locked(sc);
1172
1173         return;
1174 }
1175
1176 /*
1177  * Send a command to the controller and wait for it to either
1178  * complete or be accepted, depending on the command.  If the
1179  * command pointer is null, then pretend that the command is
1180  * not an action command.  If the command pointer is not null,
1181  * and the command is an action command, wait for
1182  * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1183  * to become true.
1184  */
1185 static int
1186 command_and_wait(struct ie_softc *sc, int cmd, volatile void *pcmd, int mask)
1187 {
1188         volatile struct ie_cmd_common *cc = pcmd;
1189         int i;
1190
1191         sc->scb->ie_command = (u_short) cmd;
1192
1193         if (IE_ACTION_COMMAND(cmd) && pcmd) {
1194                 (*sc->ie_chan_attn) (sc);
1195                 
1196                 /*
1197                  * Now spin-lock waiting for status.  This is not a very
1198                  * nice thing to do, but I haven't figured out how, or
1199                  * indeed if, we can put the process waiting for action to
1200                  * sleep.  (We may be getting called through some other
1201                  * timeout running in the kernel.)
1202                  *
1203                  * According to the packet driver, the minimum timeout
1204                  * should be .369 seconds, which we round up to .37.
1205                  */
1206                 for (i = 0; i < 370; i++) {
1207                         if (cc->ie_cmd_status & mask)
1208                                 return (0);
1209                         DELAY(1000);
1210                 }
1211
1212                 return (1);
1213         } else {
1214
1215                 /*
1216                  * Otherwise, just wait for the command to be accepted.
1217                  */
1218                 (*sc->ie_chan_attn) (sc);
1219
1220                 while (sc->scb->ie_command);    /* spin lock */
1221
1222                 return (0);
1223         }
1224 }
1225
1226 /*
1227  * Run the time-domain reflectometer...
1228  */
1229 static void
1230 run_tdr(struct ie_softc *sc, volatile struct ie_tdr_cmd *cmd)
1231 {
1232         int     result;
1233
1234         cmd->com.ie_cmd_status = 0;
1235         cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1236         cmd->com.ie_cmd_link = 0xffff;
1237         cmd->ie_tdr_time = 0;
1238
1239         sc->scb->ie_command_list = MK_16(MEM(sc), cmd);
1240         cmd->ie_tdr_time = 0;
1241
1242         if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL))
1243                 result = 0x2000;
1244         else
1245                 result = cmd->ie_tdr_time;
1246
1247         ie_ack(sc, IE_ST_WHENCE);
1248
1249         if (result & IE_TDR_SUCCESS)
1250                 return;
1251
1252         if (result & IE_TDR_XCVR) {
1253                 if_printf(sc->ifp, "transceiver problem\n");
1254         } else if (result & IE_TDR_OPEN) {
1255                 if_printf(sc->ifp, "TDR detected an open %d clocks away\n",
1256                        result & IE_TDR_TIME);
1257         } else if (result & IE_TDR_SHORT) {
1258                 if_printf(sc->ifp, "TDR detected a short %d clocks away\n",
1259                        result & IE_TDR_TIME);
1260         } else {
1261                 if_printf(sc->ifp, "TDR returned unknown status %x\n", result);
1262         }
1263 }
1264
1265 static void
1266 start_receiver(struct ie_softc *sc)
1267 {
1268
1269         sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
1270         command_and_wait(sc, IE_RU_START, 0, 0);
1271
1272         ie_ack(sc, IE_ST_WHENCE);
1273 }
1274
1275 /*
1276  * Here is a helper routine for iernr() and ieinit().  This sets up
1277  * the RFA.
1278  */
1279 static v_caddr_t
1280 setup_rfa(struct ie_softc *sc, v_caddr_t ptr)
1281 {
1282         volatile struct ie_recv_frame_desc *rfd = (volatile void *)ptr;
1283         volatile struct ie_recv_buf_desc *rbd;
1284         int     i;
1285
1286         /* First lay them out */
1287         for (i = 0; i < sc->nframes; i++) {
1288                 sc->rframes[i] = rfd;
1289                 bzero((volatile char *) rfd, sizeof *rfd);      /* ignore cast-qual */
1290                 rfd++;
1291         }
1292
1293         ptr = Alignvol(rfd);            /* ignore cast-qual */
1294
1295         /* Now link them together */
1296         for (i = 0; i < sc->nframes; i++) {
1297                 sc->rframes[i]->ie_fd_next =
1298                     MK_16(MEM(sc), sc->rframes[(i + 1) % sc->nframes]);
1299         }
1300
1301         /* Finally, set the EOL bit on the last one. */
1302         sc->rframes[sc->nframes - 1]->ie_fd_last |= IE_FD_LAST;
1303
1304         /*
1305          * Now lay out some buffers for the incoming frames.  Note that we
1306          * set aside a bit of slop in each buffer, to make sure that we have
1307          * enough space to hold a single frame in every buffer.
1308          */
1309         rbd = (volatile void *) ptr;
1310
1311         for (i = 0; i < sc->nrxbufs; i++) {
1312                 sc->rbuffs[i] = rbd;
1313                 bzero((volatile char *)rbd, sizeof *rbd);
1314                 ptr = Alignvol(ptr + sizeof *rbd);
1315                 rbd->ie_rbd_length = IE_RBUF_SIZE;
1316                 rbd->ie_rbd_buffer = MK_24(MEM(sc), ptr);
1317                 sc->cbuffs[i] = (volatile void *) ptr;
1318                 ptr += IE_RBUF_SIZE;
1319                 rbd = (volatile void *) ptr;
1320         }
1321
1322         /* Now link them together */
1323         for (i = 0; i < sc->nrxbufs; i++) {
1324                 sc->rbuffs[i]->ie_rbd_next =
1325                     MK_16(MEM(sc), sc->rbuffs[(i + 1) % sc->nrxbufs]);
1326         }
1327
1328         /* Tag EOF on the last one */
1329         sc->rbuffs[sc->nrxbufs - 1]->ie_rbd_length |= IE_RBD_LAST;
1330
1331         /*
1332          * We use the head and tail pointers on receive to keep track of the
1333          * order in which RFDs and RBDs are used.
1334          */
1335         sc->rfhead = 0;
1336         sc->rftail = sc->nframes - 1;
1337         sc->rbhead = 0;
1338         sc->rbtail = sc->nrxbufs - 1;
1339
1340         sc->scb->ie_recv_list = MK_16(MEM(sc), sc->rframes[0]);
1341         sc->rframes[0]->ie_fd_buf_desc = MK_16(MEM(sc), sc->rbuffs[0]);
1342
1343         ptr = Alignvol(ptr);
1344         return (ptr);
1345 }
1346
1347 /*
1348  * Run the multicast setup command.
1349  */
1350 static int
1351 mc_setup(struct ie_softc *sc)
1352 {
1353         volatile struct ie_mcast_cmd *cmd = (volatile void *)sc->xmit_cbuffs[0];
1354
1355         cmd->com.ie_cmd_status = 0;
1356         cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1357         cmd->com.ie_cmd_link = 0xffff;
1358
1359         /* ignore cast-qual */
1360         bcopy((v_caddr_t) sc->mcast_addrs, (v_caddr_t) cmd->ie_mcast_addrs,
1361               sc->mcast_count * sizeof *sc->mcast_addrs);
1362
1363         cmd->ie_mcast_bytes = sc->mcast_count * 6;      /* grrr... */
1364
1365         sc->scb->ie_command_list = MK_16(MEM(sc), cmd);
1366         if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL)
1367             || !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1368                 if_printf(sc->ifp, "multicast address setup command failed\n");
1369                 return (0);
1370         }
1371         return (1);
1372 }
1373
1374 /*
1375  * This routine takes the environment generated by check_ie_present()
1376  * and adds to it all the other structures we need to operate the adapter.
1377  * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
1378  * starting the receiver unit, and clearing interrupts.
1379  */
1380 static void
1381 ieinit(xsc)
1382         void *xsc;
1383 {
1384         struct ie_softc *sc = xsc;
1385
1386         IE_LOCK(sc);
1387         ieinit_locked(sc);
1388         IE_UNLOCK(sc);
1389 }
1390
1391 static void
1392 ieinit_locked(struct ie_softc *sc)
1393 {
1394         struct ifnet *ifp = sc->ifp;
1395         volatile struct ie_sys_ctl_block *scb = sc->scb;
1396         caddr_t ptr;
1397         int     i;
1398
1399         ptr = Alignvol((volatile char *) scb + sizeof *scb);
1400
1401         /*
1402          * Send the configure command first.
1403          */
1404         {
1405                 volatile struct ie_config_cmd *cmd = (volatile void *) ptr;
1406
1407                 ie_setup_config(cmd, sc->promisc,
1408                                 sc->hard_type == IE_STARLAN10);
1409                 cmd->com.ie_cmd_status = 0;
1410                 cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
1411                 cmd->com.ie_cmd_link = 0xffff;
1412
1413                 scb->ie_command_list = MK_16(MEM(sc), cmd);
1414
1415                 if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL)
1416                  || !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1417                         if_printf(ifp, "configure command failed\n");
1418                         return;
1419                 }
1420         }
1421         /*
1422          * Now send the Individual Address Setup command.
1423          */
1424         {
1425                 volatile struct ie_iasetup_cmd *cmd = (volatile void *) ptr;
1426
1427                 cmd->com.ie_cmd_status = 0;
1428                 cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
1429                 cmd->com.ie_cmd_link = 0xffff;
1430
1431                 bcopy((volatile char *)IF_LLADDR(ifp),
1432                       (volatile char *)&cmd->ie_address, sizeof cmd->ie_address);
1433                 scb->ie_command_list = MK_16(MEM(sc), cmd);
1434                 if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL)
1435                     || !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1436                         if_printf(ifp, "individual address "
1437                                "setup command failed\n");
1438                         return;
1439                 }
1440         }
1441
1442         /*
1443          * Now run the time-domain reflectometer.
1444          */
1445         run_tdr(sc, (volatile void *) ptr);
1446
1447         /*
1448          * Acknowledge any interrupts we have generated thus far.
1449          */
1450         ie_ack(sc, IE_ST_WHENCE);
1451
1452         /*
1453          * Set up the RFA.
1454          */
1455         ptr = setup_rfa(sc, ptr);
1456
1457         /*
1458          * Finally, the transmit command and buffer are the last little bit
1459          * of work.
1460          */
1461
1462         /* transmit command buffers */
1463         for (i = 0; i < sc->ntxbufs; i++) {
1464                 sc->xmit_cmds[i] = (volatile void *) ptr;
1465                 ptr += sizeof *sc->xmit_cmds[i];
1466                 ptr = Alignvol(ptr);
1467                 sc->xmit_buffs[i] = (volatile void *)ptr;
1468                 ptr += sizeof *sc->xmit_buffs[i];
1469                 ptr = Alignvol(ptr);
1470         }
1471
1472         /* transmit buffers */
1473         for (i = 0; i < sc->ntxbufs - 1; i++) {
1474                 sc->xmit_cbuffs[i] = (volatile void *)ptr;
1475                 ptr += IE_BUF_LEN;
1476                 ptr = Alignvol(ptr);
1477         }
1478         sc->xmit_cbuffs[sc->ntxbufs - 1] = (volatile void *) ptr;
1479
1480         for (i = 1; i < sc->ntxbufs; i++) {
1481                 bzero((v_caddr_t) sc->xmit_cmds[i], sizeof *sc->xmit_cmds[i]);
1482                 bzero((v_caddr_t) sc->xmit_buffs[i], sizeof *sc->xmit_buffs[i]);
1483         }
1484
1485         /*
1486          * This must be coordinated with iestart() and ietint().
1487          */
1488         sc->xmit_cmds[0]->ie_xmit_status = IE_STAT_COMPL;
1489
1490         /* take the ee16 out of loopback */
1491         if (sc->hard_type == IE_EE16) {
1492                 u_int8_t bart_config;
1493
1494                 bart_config = inb(PORT(sc) + IEE16_CONFIG);
1495                 bart_config &= ~IEE16_BART_LOOPBACK;
1496                 /* inb doesn't get bit! */
1497                 bart_config |= IEE16_BART_MCS16_TEST;
1498                 outb(PORT(sc) + IEE16_CONFIG, bart_config);
1499                 ee16_interrupt_enable(sc);
1500                 ee16_chan_attn(sc);
1501         }
1502         ifp->if_drv_flags |= IFF_DRV_RUNNING;   /* tell higher levels
1503                                                          * we're here */
1504         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1505
1506         start_receiver(sc);
1507
1508         return;
1509 }
1510
1511 static void
1512 ie_stop(struct ie_softc *sc)
1513 {
1514         struct ifnet *ifp = sc->ifp;
1515
1516         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1517         command_and_wait(sc, IE_RU_DISABLE, 0, 0);
1518 }
1519
1520 static int
1521 ieioctl(struct ifnet *ifp, u_long command, caddr_t data)
1522 {
1523         int     error = 0;
1524         struct   ie_softc *sc = ifp->if_softc;
1525
1526         switch (command) {
1527         case SIOCSIFFLAGS:
1528                 /*
1529                  * Note that this device doesn't have an "all multicast"
1530                  * mode, so we must turn on promiscuous mode and do the
1531                  * filtering manually.
1532                  */
1533                 IE_LOCK(sc);
1534                 if ((ifp->if_flags & IFF_UP) == 0 &&
1535                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1536                         ie_stop(sc);
1537                 } else if ((ifp->if_flags & IFF_UP) &&
1538                            (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1539                         sc->promisc =
1540                             ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1541                         ieinit_locked(sc);
1542                 } else if (sc->promisc ^
1543                            (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1544                         sc->promisc =
1545                             ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1546                         ieinit_locked(sc);
1547                 }
1548                 IE_UNLOCK(sc);
1549                 break;
1550
1551         case SIOCADDMULTI:
1552         case SIOCDELMULTI:
1553                 /*
1554                  * Update multicast listeners
1555                  */
1556                 /* reset multicast filtering */
1557                 IE_LOCK(sc);
1558                 ie_mc_reset(sc);
1559                 IE_UNLOCK(sc);
1560                 error = 0;
1561                 break;
1562
1563         default:
1564                 error = ether_ioctl(ifp, command, data);
1565                 break;
1566         }
1567
1568         return (error);
1569 }
1570
1571 static void
1572 ie_mc_reset(struct ie_softc *sc)
1573 {
1574         struct ifmultiaddr *ifma;
1575
1576         /*
1577          * Step through the list of addresses.
1578          */
1579         sc->mcast_count = 0;
1580         if_maddr_rlock(sc->ifp);
1581         TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) {
1582                 if (ifma->ifma_addr->sa_family != AF_LINK)
1583                         continue;
1584
1585                 /* XXX - this is broken... */
1586                 if (sc->mcast_count >= MAXMCAST) {
1587                         sc->ifp->if_flags |= IFF_ALLMULTI;
1588                         if (sc->ifp->if_flags & IFF_UP)
1589                                 ieinit_locked(sc);
1590                         goto setflag;
1591                 }
1592                 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1593                       &(sc->mcast_addrs[sc->mcast_count]), 6);
1594                 sc->mcast_count++;
1595         }
1596         if_maddr_runlock(sc->ifp);
1597
1598 setflag:
1599         sc->want_mcsetup = 1;
1600 }
1601
1602
1603 #ifdef DEBUG
1604 static void
1605 print_rbd(volatile struct ie_recv_buf_desc * rbd)
1606 {
1607         printf("RBD at %p:\n"
1608                "actual %04x, next %04x, buffer %p\n"
1609                "length %04x, mbz %04x\n",
1610                (volatile void *) rbd,
1611                rbd->ie_rbd_actual, rbd->ie_rbd_next,
1612                (void *) rbd->ie_rbd_buffer,
1613                rbd->ie_rbd_length, rbd->mbz);
1614 }
1615
1616 #endif                          /* DEBUG */
1617
1618 int
1619 ie_alloc_resources (device_t dev)
1620 {
1621         struct ie_softc *       sc;
1622         int                     error;
1623
1624         error = 0;
1625         sc = device_get_softc(dev);
1626
1627         sc->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->io_rid,
1628                                             RF_ACTIVE);
1629         if (!sc->io_res) {
1630                 device_printf(dev, "No I/O space?!\n");
1631                 error = ENOMEM;
1632                 goto bad;
1633         }
1634         sc->io_bt = rman_get_bustag(sc->io_res);
1635         sc->io_bh = rman_get_bushandle(sc->io_res);
1636
1637         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
1638                                              RF_ACTIVE);
1639         if (!sc->mem_res) {
1640                 device_printf(dev, "No Memory!\n");
1641                 error = ENOMEM;
1642                 goto bad;
1643         }
1644         sc->mem_bt = rman_get_bustag(sc->mem_res);
1645         sc->mem_bh = rman_get_bushandle(sc->mem_res);
1646
1647         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1648                                              RF_ACTIVE);
1649         if (!sc->irq_res) {
1650                 device_printf(dev, "No IRQ!\n");
1651                 error = ENOMEM;
1652                 goto bad;
1653         }
1654
1655         sc->port = rman_get_start(sc->io_res);  /* XXX hack */
1656         sc->iomembot = rman_get_virtual(sc->mem_res);
1657         sc->iosize = rman_get_size(sc->mem_res);
1658
1659         return (0);
1660 bad:
1661         return (error);
1662 }
1663
1664 void
1665 ie_release_resources (device_t dev)
1666 {
1667         struct ie_softc *       sc;
1668
1669         sc = device_get_softc(dev);
1670
1671         if (sc->irq_ih)
1672                 bus_teardown_intr(dev, sc->irq_res, sc->irq_ih);
1673         if (sc->rframes)
1674                 free(sc->rframes, M_DEVBUF);
1675         if (sc->io_res)
1676                 bus_release_resource(dev, SYS_RES_IOPORT,
1677                                      sc->io_rid, sc->io_res);
1678         if (sc->irq_res)
1679                 bus_release_resource(dev, SYS_RES_IRQ,
1680                                      sc->irq_rid, sc->irq_res);
1681         if (sc->mem_res)
1682                 bus_release_resource(dev, SYS_RES_MEMORY,
1683                                      sc->mem_rid, sc->mem_res);
1684         if (sc->ifp)
1685                 if_free(sc->ifp);
1686
1687         return;
1688 }
1689
1690 int
1691 ie_detach (device_t dev)
1692 {
1693         struct ie_softc *       sc;
1694         struct ifnet *          ifp;
1695
1696         sc = device_get_softc(dev);
1697         ifp = sc->ifp;
1698
1699         IE_LOCK(sc);
1700         if (sc->hard_type == IE_EE16)
1701                 ee16_shutdown(sc);
1702
1703         ie_stop(sc);
1704         IE_UNLOCK(sc);
1705         ether_ifdetach(ifp);
1706         ie_release_resources(dev);
1707         mtx_destroy(&sc->lock);
1708
1709         return (0);
1710 }