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