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