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