i82586.c revision 1.36 1 /* $NetBSD: i82586.c,v 1.36 2001/03/10 20:04:30 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg and Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1997 Paul Kranenburg.
41 * Copyright (c) 1992, 1993, University of Vermont and State
42 * Agricultural College.
43 * Copyright (c) 1992, 1993, Garrett A. Wollman.
44 *
45 * Portions:
46 * Copyright (c) 1994, 1995, Rafal K. Boni
47 * Copyright (c) 1990, 1991, William F. Jolitz
48 * Copyright (c) 1990, The Regents of the University of California
49 *
50 * All rights reserved.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 * must display the following acknowledgement:
62 * This product includes software developed by the University of Vermont
63 * and State Agricultural College and Garrett A. Wollman, by William F.
64 * Jolitz, and by the University of California, Berkeley, Lawrence
65 * Berkeley Laboratory, and its contributors.
66 * 4. Neither the names of the Universities nor the names of the authors
67 * may be used to endorse or promote products derived from this software
68 * without specific prior written permission.
69 *
70 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
71 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
74 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
76 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
77 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
79 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 * SUCH DAMAGE.
81 */
82
83 /*
84 * Intel 82586 Ethernet chip
85 * Register, bit, and structure definitions.
86 *
87 * Original StarLAN driver written by Garrett Wollman with reference to the
88 * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
89 *
90 * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
91 *
92 * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
93 *
94 * Majorly cleaned up and 3C507 code merged by Charles Hannum.
95 *
96 * Converted to SUN ie driver by Charles D. Cranor,
97 * October 1994, January 1995.
98 * This sun version based on i386 version 1.30.
99 */
100
101 /*
102 * The i82586 is a very painful chip, found in sun3's, sun-4/100's
103 * sun-4/200's, and VME based suns. The byte order is all wrong for a
104 * SUN, making life difficult. Programming this chip is mostly the same,
105 * but certain details differ from system to system. This driver is
106 * written so that different "ie" interfaces can be controled by the same
107 * driver.
108 */
109
110 /*
111 Mode of operation:
112
113 We run the 82586 in a standard Ethernet mode. We keep NFRAMES
114 received frame descriptors around for the receiver to use, and
115 NRXBUF associated receive buffer descriptors, both in a circular
116 list. Whenever a frame is received, we rotate both lists as
117 necessary. (The 586 treats both lists as a simple queue.) We also
118 keep a transmit command around so that packets can be sent off
119 quickly.
120
121 We configure the adapter in AL-LOC = 1 mode, which means that the
122 Ethernet/802.3 MAC header is placed at the beginning of the receive
123 buffer rather than being split off into various fields in the RFD.
124 This also means that we must include this header in the transmit
125 buffer as well.
126
127 By convention, all transmit commands, and only transmit commands,
128 shall have the I (IE_CMD_INTR) bit set in the command. This way,
129 when an interrupt arrives at i82586_intr(), it is immediately possible
130 to tell what precisely caused it. ANY OTHER command-sending
131 routines should run at splnet(), and should post an acknowledgement
132 to every interrupt they generate.
133
134 To save the expense of shipping a command to 82586 every time we
135 want to send a frame, we use a linked list of commands consisting
136 of alternate XMIT and NOP commands. The links of these elements
137 are manipulated (in iexmit()) such that the NOP command loops back
138 to itself whenever the following XMIT command is not yet ready to
139 go. Whenever an XMIT is ready, the preceding NOP link is pointed
140 at it, while its own link field points to the following NOP command.
141 Thus, a single transmit command sets off an interlocked traversal
142 of the xmit command chain, with the host processor in control of
143 the synchronization.
144 */
145
146 #include "bpfilter.h"
147
148 #include <sys/param.h>
149
150 __KERNEL_RCSID(0, "$NetBSD: i82586.c,v 1.36 2001/03/10 20:04:30 jdolecek Exp $");
151
152 #include <sys/systm.h>
153 #include <sys/mbuf.h>
154 #include <sys/socket.h>
155 #include <sys/ioctl.h>
156 #include <sys/errno.h>
157 #include <sys/syslog.h>
158 #include <sys/device.h>
159
160 #include <net/if.h>
161 #include <net/if_dl.h>
162 #include <net/if_types.h>
163 #include <net/if_media.h>
164 #include <net/if_ether.h>
165
166 #if NBPFILTER > 0
167 #include <net/bpf.h>
168 #include <net/bpfdesc.h>
169 #endif
170
171 #include <machine/bus.h>
172
173 #include <dev/ic/i82586reg.h>
174 #include <dev/ic/i82586var.h>
175
176 void i82586_reset __P((struct ie_softc *, int));
177 void i82586_watchdog __P((struct ifnet *));
178 int i82586_init __P((struct ifnet *));
179 int i82586_ioctl __P((struct ifnet *, u_long, caddr_t));
180 void i82586_start __P((struct ifnet *));
181 void i82586_stop __P((struct ifnet *, int));
182
183
184 int i82586_rint __P((struct ie_softc *, int));
185 int i82586_tint __P((struct ie_softc *, int));
186
187 int i82586_mediachange __P((struct ifnet *));
188 void i82586_mediastatus __P((struct ifnet *,
189 struct ifmediareq *));
190
191 static int ie_readframe __P((struct ie_softc *, int));
192 static struct mbuf *ieget __P((struct ie_softc *, int, int));
193 static int i82586_get_rbd_list __P((struct ie_softc *,
194 u_int16_t *, u_int16_t *, int *));
195 static void i82586_release_rbd_list __P((struct ie_softc *,
196 u_int16_t, u_int16_t));
197 static int i82586_drop_frames __P((struct ie_softc *));
198 static int i82586_chk_rx_ring __P((struct ie_softc *));
199
200 static __inline__ void ie_ack __P((struct ie_softc *, u_int));
201 static __inline__ void iexmit __P((struct ie_softc *));
202 static void i82586_start_transceiver
203 __P((struct ie_softc *));
204
205 static void i82586_count_errors __P((struct ie_softc *));
206 static void i82586_rx_errors __P((struct ie_softc *, int, int));
207 static void i82586_setup_bufs __P((struct ie_softc *));
208 static void setup_simple_command __P((struct ie_softc *, int, int));
209 static int ie_cfg_setup __P((struct ie_softc *, int, int, int));
210 static int ie_ia_setup __P((struct ie_softc *, int));
211 static void ie_run_tdr __P((struct ie_softc *, int));
212 static int ie_mc_setup __P((struct ie_softc *, int));
213 static void ie_mc_reset __P((struct ie_softc *));
214 static int i82586_start_cmd __P((struct ie_softc *,
215 int, int, int, int));
216 static int i82586_cmd_wait __P((struct ie_softc *));
217
218 #if I82586_DEBUG
219 void print_rbd __P((struct ie_softc *, int));
220 #endif
221
222
223 /*
224 * Front-ends call this function to attach to the MI driver.
225 *
226 * The front-end has responsibility for managing the ICP and ISCP
227 * structures. Both of these are opaque to us. Also, the front-end
228 * chooses a location for the SCB which is expected to be addressable
229 * (through `sc->scb') as an offset against the shared-memory bus handle.
230 *
231 * The following MD interface function must be setup by the front-end
232 * before calling here:
233 *
234 * hwreset - board dependent reset
235 * hwinit - board dependent initialization
236 * chan_attn - channel attention
237 * intrhook - board dependent interrupt processing
238 * memcopyin - shared memory copy: board to KVA
239 * memcopyout - shared memory copy: KVA to board
240 * ie_bus_read16 - read a sixteen-bit i82586 pointer
241 * ie_bus_write16 - write a sixteen-bit i82586 pointer
242 * ie_bus_write24 - write a twenty-four-bit i82586 pointer
243 *
244 */
245 void
246 i82586_attach(sc, name, etheraddr, media, nmedia, defmedia)
247 struct ie_softc *sc;
248 char *name;
249 u_int8_t *etheraddr;
250 int *media, nmedia, defmedia;
251 {
252 int i;
253 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
254
255 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
256 ifp->if_softc = sc;
257 ifp->if_start = i82586_start;
258 ifp->if_ioctl = i82586_ioctl;
259 ifp->if_init = i82586_init;
260 ifp->if_stop = i82586_stop;
261 ifp->if_watchdog = i82586_watchdog;
262 ifp->if_flags =
263 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
264 IFQ_SET_READY(&ifp->if_snd);
265
266 /* Initialize media goo. */
267 ifmedia_init(&sc->sc_media, 0, i82586_mediachange, i82586_mediastatus);
268 if (media != NULL) {
269 for (i = 0; i < nmedia; i++)
270 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
271 ifmedia_set(&sc->sc_media, defmedia);
272 } else {
273 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
274 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
275 }
276
277 /* Attach the interface. */
278 if_attach(ifp);
279 ether_ifattach(ifp, etheraddr);
280
281 printf(" address %s, type %s\n", ether_sprintf(etheraddr), name);
282 }
283
284
285 /*
286 * Device timeout/watchdog routine.
287 * Entered if the device neglects to generate an interrupt after a
288 * transmit has been started on it.
289 */
290 void
291 i82586_watchdog(ifp)
292 struct ifnet *ifp;
293 {
294 struct ie_softc *sc = ifp->if_softc;
295
296 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
297 ++ifp->if_oerrors;
298
299 i82586_reset(sc, 1);
300 }
301
302 static int
303 i82586_cmd_wait(sc)
304 struct ie_softc *sc;
305 {
306 /* spin on i82586 command acknowledge; wait at most 0.9 (!) seconds */
307 int i, off;
308 u_int16_t cmd;
309
310 for (i = 0; i < 900000; i++) {
311 /* Read the command word */
312 off = IE_SCB_CMD(sc->scb);
313
314 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
315 if ((cmd = sc->ie_bus_read16(sc, off)) == 0)
316 return (0);
317 delay(1);
318 }
319
320 off = IE_SCB_STATUS(sc->scb);
321 printf("i82586_cmd_wait: timo(%ssync): scb status: 0x%x, cmd: 0x%x\n",
322 sc->async_cmd_inprogress?"a":"",
323 sc->ie_bus_read16(sc, off), cmd);
324
325 return (1); /* Timeout */
326 }
327
328 /*
329 * Send a command to the controller and wait for it to either complete
330 * or be accepted, depending on the command. If the command pointer
331 * is null, then pretend that the command is not an action command.
332 * If the command pointer is not null, and the command is an action
333 * command, wait for one of the MASK bits to turn on in the command's
334 * status field.
335 * If ASYNC is set, we just call the chip's attention and return.
336 * We may have to wait for the command's acceptance later though.
337 */
338 static int
339 i82586_start_cmd(sc, cmd, iecmdbuf, mask, async)
340 struct ie_softc *sc;
341 int cmd;
342 int iecmdbuf;
343 int mask;
344 int async;
345 {
346 int i;
347 int off;
348
349 if (sc->async_cmd_inprogress != 0) {
350 /*
351 * If previous command was issued asynchronously, wait
352 * for it now.
353 */
354 if (i82586_cmd_wait(sc) != 0)
355 return (1);
356 sc->async_cmd_inprogress = 0;
357 }
358
359 off = IE_SCB_CMD(sc->scb);
360 sc->ie_bus_write16(sc, off, cmd);
361 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
362 (sc->chan_attn)(sc, CARD_RESET);
363
364 if (async != 0) {
365 sc->async_cmd_inprogress = 1;
366 return (0);
367 }
368
369 if (IE_ACTION_COMMAND(cmd) && iecmdbuf) {
370 int status;
371 /*
372 * Now spin-lock waiting for status. This is not a very nice
373 * thing to do, and can kill performance pretty well...
374 * According to the packet driver, the minimum timeout
375 * should be .369 seconds.
376 */
377 for (i = 0; i < 369000; i++) {
378 /* Read the command status */
379 off = IE_CMD_COMMON_STATUS(iecmdbuf);
380 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
381 status = sc->ie_bus_read16(sc, off);
382 if (status & mask)
383 return (0);
384 delay(1);
385 }
386
387 } else {
388 /*
389 * Otherwise, just wait for the command to be accepted.
390 */
391 return (i82586_cmd_wait(sc));
392 }
393
394 /* Timeout */
395 return (1);
396 }
397
398 /*
399 * Interrupt Acknowledge.
400 */
401 static __inline__ void
402 ie_ack(sc, mask)
403 struct ie_softc *sc;
404 u_int mask; /* in native byte-order */
405 {
406 u_int status;
407
408 IE_BUS_BARRIER(sc, 0, 0, BUS_SPACE_BARRIER_READ);
409 status = sc->ie_bus_read16(sc, IE_SCB_STATUS(sc->scb));
410 i82586_start_cmd(sc, status & mask, 0, 0, 0);
411 if (sc->intrhook)
412 sc->intrhook(sc, INTR_ACK);
413 }
414
415 /*
416 * Transfer accumulated chip error counters to IF.
417 */
418 static __inline void
419 i82586_count_errors(sc)
420 struct ie_softc *sc;
421 {
422 int scb = sc->scb;
423
424 sc->sc_ethercom.ec_if.if_ierrors +=
425 sc->ie_bus_read16(sc, IE_SCB_ERRCRC(scb)) +
426 sc->ie_bus_read16(sc, IE_SCB_ERRALN(scb)) +
427 sc->ie_bus_read16(sc, IE_SCB_ERRRES(scb)) +
428 sc->ie_bus_read16(sc, IE_SCB_ERROVR(scb));
429
430 /* Clear error counters */
431 sc->ie_bus_write16(sc, IE_SCB_ERRCRC(scb), 0);
432 sc->ie_bus_write16(sc, IE_SCB_ERRALN(scb), 0);
433 sc->ie_bus_write16(sc, IE_SCB_ERRRES(scb), 0);
434 sc->ie_bus_write16(sc, IE_SCB_ERROVR(scb), 0);
435 }
436
437 static void
438 i82586_rx_errors(sc, fn, status)
439 struct ie_softc *sc;
440 int fn;
441 int status;
442 {
443 char bits[128];
444
445 log(LOG_ERR, "%s: rx error (frame# %d): %s\n", sc->sc_dev.dv_xname, fn,
446 bitmask_snprintf(status, IE_FD_STATUSBITS, bits, sizeof(bits)));
447 }
448
449 /*
450 * i82586 interrupt entry point.
451 */
452 int
453 i82586_intr(v)
454 void *v;
455 {
456 struct ie_softc *sc = v;
457 u_int status;
458 int off;
459
460 /*
461 * Implementation dependent interrupt handling.
462 */
463 if (sc->intrhook)
464 (sc->intrhook)(sc, INTR_ENTER);
465
466 off = IE_SCB_STATUS(sc->scb);
467 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
468 status = sc->ie_bus_read16(sc, off) & IE_ST_WHENCE;
469
470 if ((status & IE_ST_WHENCE) == 0) {
471 if (sc->intrhook)
472 (sc->intrhook)(sc, INTR_EXIT);
473
474 return (0);
475 }
476
477 loop:
478 /* Ack interrupts FIRST in case we receive more during the ISR. */
479 #if 0
480 ie_ack(sc, status & IE_ST_WHENCE);
481 #endif
482 i82586_start_cmd(sc, status & IE_ST_WHENCE, 0, 0, 1);
483
484 if (status & (IE_ST_FR | IE_ST_RNR))
485 if (i82586_rint(sc, status) != 0)
486 goto reset;
487
488 if (status & IE_ST_CX)
489 if (i82586_tint(sc, status) != 0)
490 goto reset;
491
492 #if I82586_DEBUG
493 if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
494 printf("%s: cna; status=0x%x\n", sc->sc_dev.dv_xname, status);
495 #endif
496 if (sc->intrhook)
497 (sc->intrhook)(sc, INTR_LOOP);
498
499 /*
500 * Interrupt ACK was posted asynchronously; wait for
501 * completion here before reading SCB status again.
502 *
503 * If ACK fails, try to reset the chip, in hopes that
504 * it helps.
505 */
506 if (i82586_cmd_wait(sc) != 0)
507 goto reset;
508
509 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
510 status = sc->ie_bus_read16(sc, off);
511 if ((status & IE_ST_WHENCE) != 0)
512 goto loop;
513
514 out:
515 if (sc->intrhook)
516 (sc->intrhook)(sc, INTR_EXIT);
517 return (1);
518
519 reset:
520 i82586_cmd_wait(sc);
521 i82586_reset(sc, 1);
522 goto out;
523
524 }
525
526 /*
527 * Process a received-frame interrupt.
528 */
529 int
530 i82586_rint(sc, scbstatus)
531 struct ie_softc *sc;
532 int scbstatus;
533 {
534 static int timesthru = 1024;
535 int i, status, off;
536
537 #if I82586_DEBUG
538 if (sc->sc_debug & IED_RINT)
539 printf("%s: rint: status 0x%x\n",
540 sc->sc_dev.dv_xname, scbstatus);
541 #endif
542
543 for (;;) {
544 int drop = 0;
545
546 i = sc->rfhead;
547 off = IE_RFRAME_STATUS(sc->rframes, i);
548 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
549 status = sc->ie_bus_read16(sc, off);
550
551 #if I82586_DEBUG
552 if (sc->sc_debug & IED_RINT)
553 printf("%s: rint: frame(%d) status 0x%x\n",
554 sc->sc_dev.dv_xname, i, status);
555 #endif
556 if ((status & IE_FD_COMPLETE) == 0) {
557 if ((status & IE_FD_OK) != 0) {
558 printf("%s: rint: weird: ",
559 sc->sc_dev.dv_xname);
560 i82586_rx_errors(sc, i, status);
561 break;
562 }
563 if (--timesthru == 0) {
564 /* Account the accumulated errors */
565 i82586_count_errors(sc);
566 timesthru = 1024;
567 }
568 break;
569 } else if ((status & IE_FD_OK) == 0) {
570 /*
571 * If the chip is configured to automatically
572 * discard bad frames, the only reason we can
573 * get here is an "out-of-resource" condition.
574 */
575 i82586_rx_errors(sc, i, status);
576 drop = 1;
577 }
578
579 #if I82586_DEBUG
580 if ((status & IE_FD_BUSY) != 0)
581 printf("%s: rint: frame(%d) busy; status=0x%x\n",
582 sc->sc_dev.dv_xname, i, status);
583 #endif
584
585
586 /*
587 * Advance the RFD list, since we're done with
588 * this descriptor.
589 */
590
591 /* Clear frame status */
592 sc->ie_bus_write16(sc, off, 0);
593
594 /* Put fence at this frame (the head) */
595 off = IE_RFRAME_LAST(sc->rframes, i);
596 sc->ie_bus_write16(sc, off, IE_FD_EOL|IE_FD_SUSP);
597
598 /* and clear RBD field */
599 off = IE_RFRAME_BUFDESC(sc->rframes, i);
600 sc->ie_bus_write16(sc, off, 0xffff);
601
602 /* Remove fence from current tail */
603 off = IE_RFRAME_LAST(sc->rframes, sc->rftail);
604 sc->ie_bus_write16(sc, off, 0);
605
606 if (++sc->rftail == sc->nframes)
607 sc->rftail = 0;
608 if (++sc->rfhead == sc->nframes)
609 sc->rfhead = 0;
610
611 /* Pull the frame off the board */
612 if (drop) {
613 i82586_drop_frames(sc);
614 if ((status & IE_FD_RNR) != 0)
615 sc->rnr_expect = 1;
616 sc->sc_ethercom.ec_if.if_ierrors++;
617 } else if (ie_readframe(sc, i) != 0)
618 return (1);
619 }
620
621 if ((scbstatus & IE_ST_RNR) != 0) {
622
623 /*
624 * Receiver went "Not Ready". We try to figure out
625 * whether this was an expected event based on past
626 * frame status values.
627 */
628
629 if ((scbstatus & IE_RUS_SUSPEND) != 0) {
630 /*
631 * We use the "suspend on last frame" flag.
632 * Send a RU RESUME command in response, since
633 * we should have dealt with all completed frames
634 * by now.
635 */
636 printf("RINT: SUSPENDED; scbstatus=0x%x\n",
637 scbstatus);
638 if (i82586_start_cmd(sc, IE_RUC_RESUME, 0, 0, 0) == 0)
639 return (0);
640 printf("%s: RU RESUME command timed out\n",
641 sc->sc_dev.dv_xname);
642 return (1); /* Ask for a reset */
643 }
644
645 if (sc->rnr_expect != 0) {
646 /*
647 * The RNR condition was announced in the previously
648 * completed frame. Assume the receive ring is Ok,
649 * so restart the receiver without further delay.
650 */
651 i82586_start_transceiver(sc);
652 sc->rnr_expect = 0;
653 return (0);
654
655 } else if ((scbstatus & IE_RUS_NOSPACE) != 0) {
656 /*
657 * We saw no previous IF_FD_RNR flag.
658 * We check our ring invariants and, if ok,
659 * just restart the receiver at the current
660 * point in the ring.
661 */
662 if (i82586_chk_rx_ring(sc) != 0)
663 return (1);
664
665 i82586_start_transceiver(sc);
666 sc->sc_ethercom.ec_if.if_ierrors++;
667 return (0);
668 } else
669 printf("%s: receiver not ready; scbstatus=0x%x\n",
670 sc->sc_dev.dv_xname, scbstatus);
671
672 sc->sc_ethercom.ec_if.if_ierrors++;
673 return (1); /* Ask for a reset */
674 }
675
676 return (0);
677 }
678
679 /*
680 * Process a command-complete interrupt. These are only generated by the
681 * transmission of frames. This routine is deceptively simple, since most
682 * of the real work is done by i82586_start().
683 */
684 int
685 i82586_tint(sc, scbstatus)
686 struct ie_softc *sc;
687 int scbstatus;
688 {
689 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
690 int status;
691
692 ifp->if_timer = 0;
693 ifp->if_flags &= ~IFF_OACTIVE;
694
695 #if I82586_DEBUG
696 if (sc->xmit_busy <= 0) {
697 printf("i82586_tint: WEIRD: xmit_busy=%d, xctail=%d, xchead=%d\n",
698 sc->xmit_busy, sc->xctail, sc->xchead);
699 return (0);
700 }
701 #endif
702
703 status = sc->ie_bus_read16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds,
704 sc->xctail));
705
706 #if I82586_DEBUG
707 if (sc->sc_debug & IED_TINT)
708 printf("%s: tint: SCB status 0x%x; xmit status 0x%x\n",
709 sc->sc_dev.dv_xname, scbstatus, status);
710 #endif
711
712 if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY)) {
713 printf("i82586_tint: command still busy; status=0x%x; tail=%d\n",
714 status, sc->xctail);
715 printf("iestatus = 0x%x\n", scbstatus);
716 }
717
718 if (status & IE_STAT_OK) {
719 ifp->if_opackets++;
720 ifp->if_collisions += (status & IE_XS_MAXCOLL);
721 } else {
722 ifp->if_oerrors++;
723 /*
724 * Check SQE and DEFERRED?
725 * What if more than one bit is set?
726 */
727 if (status & IE_STAT_ABORT)
728 printf("%s: send aborted\n", sc->sc_dev.dv_xname);
729 else if (status & IE_XS_NOCARRIER)
730 printf("%s: no carrier\n", sc->sc_dev.dv_xname);
731 else if (status & IE_XS_LOSTCTS)
732 printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
733 else if (status & IE_XS_UNDERRUN)
734 printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
735 else if (status & IE_XS_EXCMAX) {
736 printf("%s: too many collisions\n",
737 sc->sc_dev.dv_xname);
738 sc->sc_ethercom.ec_if.if_collisions += 16;
739 }
740 }
741
742 /*
743 * If multicast addresses were added or deleted while transmitting,
744 * ie_mc_reset() set the want_mcsetup flag indicating that we
745 * should do it.
746 */
747 if (sc->want_mcsetup) {
748 ie_mc_setup(sc, IE_XBUF_ADDR(sc, sc->xctail));
749 sc->want_mcsetup = 0;
750 }
751
752 /* Done with the buffer. */
753 sc->xmit_busy--;
754 sc->xctail = (sc->xctail + 1) % NTXBUF;
755
756 /* Start the next packet, if any, transmitting. */
757 if (sc->xmit_busy > 0)
758 iexmit(sc);
759
760 i82586_start(ifp);
761 return (0);
762 }
763
764 /*
765 * Get a range of receive buffer descriptors that represent one packet.
766 */
767 static int
768 i82586_get_rbd_list(sc, start, end, pktlen)
769 struct ie_softc *sc;
770 u_int16_t *start;
771 u_int16_t *end;
772 int *pktlen;
773 {
774 int off, rbbase = sc->rbds;
775 int rbindex, count = 0;
776 int plen = 0;
777 int rbdstatus;
778
779 *start = rbindex = sc->rbhead;
780
781 do {
782 off = IE_RBD_STATUS(rbbase, rbindex);
783 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
784 rbdstatus = sc->ie_bus_read16(sc, off);
785 if ((rbdstatus & IE_RBD_USED) == 0) {
786 /*
787 * This means we are somehow out of sync. So, we
788 * reset the adapter.
789 */
790 #if I82586_DEBUG
791 print_rbd(sc, rbindex);
792 #endif
793 log(LOG_ERR,
794 "%s: receive descriptors out of sync at %d\n",
795 sc->sc_dev.dv_xname, rbindex);
796 return (0);
797 }
798 plen += (rbdstatus & IE_RBD_CNTMASK);
799
800 if (++rbindex == sc->nrxbuf)
801 rbindex = 0;
802
803 ++count;
804 } while ((rbdstatus & IE_RBD_LAST) == 0);
805 *end = rbindex;
806 *pktlen = plen;
807 return (count);
808 }
809
810
811 /*
812 * Release a range of receive buffer descriptors after we've copied the packet.
813 */
814 static void
815 i82586_release_rbd_list(sc, start, end)
816 struct ie_softc *sc;
817 u_int16_t start;
818 u_int16_t end;
819 {
820 int off, rbbase = sc->rbds;
821 int rbindex = start;
822
823 do {
824 /* Clear buffer status */
825 off = IE_RBD_STATUS(rbbase, rbindex);
826 sc->ie_bus_write16(sc, off, 0);
827 if (++rbindex == sc->nrxbuf)
828 rbindex = 0;
829 } while (rbindex != end);
830
831 /* Mark EOL at new tail */
832 rbindex = ((rbindex == 0) ? sc->nrxbuf : rbindex) - 1;
833 off = IE_RBD_BUFLEN(rbbase, rbindex);
834 sc->ie_bus_write16(sc, off, IE_RBUF_SIZE|IE_RBD_EOL);
835
836 /* Remove EOL from current tail */
837 off = IE_RBD_BUFLEN(rbbase, sc->rbtail);
838 sc->ie_bus_write16(sc, off, IE_RBUF_SIZE);
839
840 /* New head & tail pointer */
841 /* hmm, why have both? head is always (tail + 1) % NRXBUF */
842 sc->rbhead = end;
843 sc->rbtail = rbindex;
844 }
845
846 /*
847 * Drop the packet at the head of the RX buffer ring.
848 * Called if the frame descriptor reports an error on this packet.
849 * Returns 1 if the buffer descriptor ring appears to be corrupt;
850 * and 0 otherwise.
851 */
852 static int
853 i82586_drop_frames(sc)
854 struct ie_softc *sc;
855 {
856 u_int16_t bstart, bend;
857 int pktlen;
858
859 if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0)
860 return (1);
861 i82586_release_rbd_list(sc, bstart, bend);
862 return (0);
863 }
864
865 /*
866 * Check the RX frame & buffer descriptor lists for our invariants,
867 * i.e.: EOL bit set iff. it is pointed at by the r*tail pointer.
868 *
869 * Called when the receive unit has stopped unexpectedly.
870 * Returns 1 if an inconsistency is detected; 0 otherwise.
871 *
872 * The Receive Unit is expected to be NOT RUNNING.
873 */
874 static int
875 i82586_chk_rx_ring(sc)
876 struct ie_softc *sc;
877 {
878 int n, off, val;
879
880 for (n = 0; n < sc->nrxbuf; n++) {
881 off = IE_RBD_BUFLEN(sc->rbds, n);
882 val = sc->ie_bus_read16(sc, off);
883 if ((n == sc->rbtail) ^ ((val & IE_RBD_EOL) != 0)) {
884 /* `rbtail' and EOL flag out of sync */
885 log(LOG_ERR,
886 "%s: rx buffer descriptors out of sync at %d\n",
887 sc->sc_dev.dv_xname, n);
888 return (1);
889 }
890
891 /* Take the opportunity to clear the status fields here ? */
892 }
893
894 for (n = 0; n < sc->nframes; n++) {
895 off = IE_RFRAME_LAST(sc->rframes, n);
896 val = sc->ie_bus_read16(sc, off);
897 if ((n == sc->rftail) ^ ((val & (IE_FD_EOL|IE_FD_SUSP)) != 0)) {
898 /* `rftail' and EOL flag out of sync */
899 log(LOG_ERR,
900 "%s: rx frame list out of sync at %d\n",
901 sc->sc_dev.dv_xname, n);
902 return (1);
903 }
904 }
905
906 return (0);
907 }
908
909 /*
910 * Read data off the interface, and turn it into an mbuf chain.
911 *
912 * This code is DRAMATICALLY different from the previous version; this
913 * version tries to allocate the entire mbuf chain up front, given the
914 * length of the data available. This enables us to allocate mbuf
915 * clusters in many situations where before we would have had a long
916 * chain of partially-full mbufs. This should help to speed up the
917 * operation considerably. (Provided that it works, of course.)
918 */
919 static __inline struct mbuf *
920 ieget(sc, head, totlen)
921 struct ie_softc *sc;
922 int head;
923 int totlen;
924 {
925 struct mbuf *m, *m0, *newm;
926 int len, resid;
927 int thisrboff, thismboff;
928 struct ether_header eh;
929
930 /*
931 * Snarf the Ethernet header.
932 */
933 (sc->memcopyin)(sc, &eh, IE_RBUF_ADDR(sc, head),
934 sizeof(struct ether_header));
935
936 resid = totlen;
937
938 MGETHDR(m0, M_DONTWAIT, MT_DATA);
939 if (m0 == 0)
940 return (0);
941 m0->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
942 m0->m_pkthdr.len = totlen;
943 len = MHLEN;
944 m = m0;
945
946 /*
947 * This loop goes through and allocates mbufs for all the data we will
948 * be copying in. It does not actually do the copying yet.
949 */
950 while (totlen > 0) {
951 if (totlen >= MINCLSIZE) {
952 MCLGET(m, M_DONTWAIT);
953 if ((m->m_flags & M_EXT) == 0)
954 goto bad;
955 len = MCLBYTES;
956 }
957
958 if (m == m0) {
959 caddr_t newdata = (caddr_t)
960 ALIGN(m->m_data + sizeof(struct ether_header)) -
961 sizeof(struct ether_header);
962 len -= newdata - m->m_data;
963 m->m_data = newdata;
964 }
965
966 m->m_len = len = min(totlen, len);
967
968 totlen -= len;
969 if (totlen > 0) {
970 MGET(newm, M_DONTWAIT, MT_DATA);
971 if (newm == 0)
972 goto bad;
973 len = MLEN;
974 m = m->m_next = newm;
975 }
976 }
977
978 m = m0;
979 thismboff = 0;
980
981 /*
982 * Copy the Ethernet header into the mbuf chain.
983 */
984 memcpy(mtod(m, caddr_t), &eh, sizeof(struct ether_header));
985 thismboff = sizeof(struct ether_header);
986 thisrboff = sizeof(struct ether_header);
987 resid -= sizeof(struct ether_header);
988
989 /*
990 * Now we take the mbuf chain (hopefully only one mbuf most of the
991 * time) and stuff the data into it. There are no possible failures
992 * at or after this point.
993 */
994 while (resid > 0) {
995 int thisrblen = IE_RBUF_SIZE - thisrboff,
996 thismblen = m->m_len - thismboff;
997 len = min(thisrblen, thismblen);
998
999 (sc->memcopyin)(sc, mtod(m, caddr_t) + thismboff,
1000 IE_RBUF_ADDR(sc,head) + thisrboff,
1001 (u_int)len);
1002 resid -= len;
1003
1004 if (len == thismblen) {
1005 m = m->m_next;
1006 thismboff = 0;
1007 } else
1008 thismboff += len;
1009
1010 if (len == thisrblen) {
1011 if (++head == sc->nrxbuf)
1012 head = 0;
1013 thisrboff = 0;
1014 } else
1015 thisrboff += len;
1016 }
1017
1018 /*
1019 * Unless something changed strangely while we were doing the copy,
1020 * we have now copied everything in from the shared memory.
1021 * This means that we are done.
1022 */
1023 return (m0);
1024
1025 bad:
1026 m_freem(m0);
1027 return (0);
1028 }
1029
1030 /*
1031 * Read frame NUM from unit UNIT (pre-cached as IE).
1032 *
1033 * This routine reads the RFD at NUM, and copies in the buffers from the list
1034 * of RBD, then rotates the RBD list so that the receiver doesn't start
1035 * complaining. Trailers are DROPPED---there's no point in wasting time
1036 * on confusing code to deal with them. Hopefully, this machine will
1037 * never ARP for trailers anyway.
1038 */
1039 static int
1040 ie_readframe(sc, num)
1041 struct ie_softc *sc;
1042 int num; /* frame number to read */
1043 {
1044 struct mbuf *m;
1045 u_int16_t bstart, bend;
1046 int pktlen;
1047
1048 if (i82586_get_rbd_list(sc, &bstart, &bend, &pktlen) == 0) {
1049 sc->sc_ethercom.ec_if.if_ierrors++;
1050 return (1);
1051 }
1052
1053 m = ieget(sc, bstart, pktlen);
1054 i82586_release_rbd_list(sc, bstart, bend);
1055
1056 if (m == 0) {
1057 sc->sc_ethercom.ec_if.if_ierrors++;
1058 return (0);
1059 }
1060
1061 #if I82586_DEBUG
1062 if (sc->sc_debug & IED_READFRAME) {
1063 struct ether_header *eh = mtod(m, struct ether_header *);
1064
1065 printf("%s: frame from ether %s type 0x%x len %d\n",
1066 sc->sc_dev.dv_xname,
1067 ether_sprintf(eh->ether_shost),
1068 (u_int)ntohs(eh->ether_type),
1069 pktlen);
1070 }
1071 #endif
1072
1073 #if NBPFILTER > 0
1074 /* Check for a BPF filter; if so, hand it up. */
1075 if (sc->sc_ethercom.ec_if.if_bpf != 0)
1076 /* Pass it up. */
1077 bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m);
1078 #endif /* NBPFILTER > 0 */
1079
1080 /*
1081 * Finally pass this packet up to higher layers.
1082 */
1083 (*sc->sc_ethercom.ec_if.if_input)(&sc->sc_ethercom.ec_if, m);
1084 sc->sc_ethercom.ec_if.if_ipackets++;
1085 return (0);
1086 }
1087
1088
1089 /*
1090 * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
1091 * command to the chip to be executed.
1092 */
1093 static __inline__ void
1094 iexmit(sc)
1095 struct ie_softc *sc;
1096 {
1097 int off;
1098 int cur, prev;
1099
1100 cur = sc->xctail;
1101
1102 #if I82586_DEBUG
1103 if (sc->sc_debug & IED_XMIT)
1104 printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname, cur);
1105 #endif
1106
1107 /*
1108 * Setup the transmit command.
1109 */
1110 sc->ie_bus_write16(sc, IE_CMD_XMIT_DESC(sc->xmit_cmds, cur),
1111 IE_XBD_ADDR(sc->xbds, cur));
1112
1113 sc->ie_bus_write16(sc, IE_CMD_XMIT_STATUS(sc->xmit_cmds, cur), 0);
1114
1115 if (sc->do_xmitnopchain) {
1116 /*
1117 * Gate this XMIT command to the following NOP
1118 */
1119 sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds, cur),
1120 IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
1121 sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
1122 IE_CMD_XMIT | IE_CMD_INTR);
1123
1124 /*
1125 * Loopback at following NOP
1126 */
1127 sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, cur), 0);
1128 sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, cur),
1129 IE_CMD_NOP_ADDR(sc->nop_cmds, cur));
1130
1131 /*
1132 * Gate preceding NOP to this XMIT command
1133 */
1134 prev = (cur + NTXBUF - 1) % NTXBUF;
1135 sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, prev), 0);
1136 sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, prev),
1137 IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
1138
1139 off = IE_SCB_STATUS(sc->scb);
1140 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
1141 if ((sc->ie_bus_read16(sc, off) & IE_CUS_ACTIVE) == 0) {
1142 printf("iexmit: CU not active\n");
1143 i82586_start_transceiver(sc);
1144 }
1145 } else {
1146 sc->ie_bus_write16(sc, IE_CMD_XMIT_LINK(sc->xmit_cmds,cur),
1147 0xffff);
1148
1149 sc->ie_bus_write16(sc, IE_CMD_XMIT_CMD(sc->xmit_cmds, cur),
1150 IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
1151
1152 off = IE_SCB_CMDLST(sc->scb);
1153 sc->ie_bus_write16(sc, off, IE_CMD_XMIT_ADDR(sc->xmit_cmds, cur));
1154 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
1155
1156 if (i82586_start_cmd(sc, IE_CUC_START, 0, 0, 1))
1157 printf("%s: iexmit: start xmit command timed out\n",
1158 sc->sc_dev.dv_xname);
1159 }
1160
1161 sc->sc_ethercom.ec_if.if_timer = 5;
1162 }
1163
1164
1165 /*
1166 * Start transmission on an interface.
1167 */
1168 void
1169 i82586_start(ifp)
1170 struct ifnet *ifp;
1171 {
1172 struct ie_softc *sc = ifp->if_softc;
1173 struct mbuf *m0, *m;
1174 int buffer, head, xbase;
1175 u_short len;
1176 int s;
1177
1178 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1179 return;
1180
1181 for (;;) {
1182 if (sc->xmit_busy == NTXBUF) {
1183 ifp->if_flags |= IFF_OACTIVE;
1184 break;
1185 }
1186
1187 head = sc->xchead;
1188 xbase = sc->xbds;
1189
1190 IFQ_DEQUEUE(&ifp->if_snd, m0);
1191 if (m0 == 0)
1192 break;
1193
1194 /* We need to use m->m_pkthdr.len, so require the header */
1195 if ((m0->m_flags & M_PKTHDR) == 0)
1196 panic("i82586_start: no header mbuf");
1197
1198 #if NBPFILTER > 0
1199 /* Tap off here if there is a BPF listener. */
1200 if (ifp->if_bpf)
1201 bpf_mtap(ifp->if_bpf, m0);
1202 #endif
1203
1204 #if I82586_DEBUG
1205 if (sc->sc_debug & IED_ENQ)
1206 printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
1207 sc->xchead);
1208 #endif
1209
1210 if (m0->m_pkthdr.len > IE_TBUF_SIZE)
1211 printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
1212
1213 buffer = IE_XBUF_ADDR(sc, head);
1214 for (m = m0; m != 0; m = m->m_next) {
1215 (sc->memcopyout)(sc, mtod(m,caddr_t), buffer, m->m_len);
1216 buffer += m->m_len;
1217 }
1218
1219 len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
1220 m_freem(m0);
1221
1222 /*
1223 * Setup the transmit buffer descriptor here, while we
1224 * know the packet's length.
1225 */
1226 sc->ie_bus_write16(sc, IE_XBD_FLAGS(xbase, head),
1227 len | IE_TBD_EOL);
1228 sc->ie_bus_write16(sc, IE_XBD_NEXT(xbase, head), 0xffff);
1229 sc->ie_bus_write24(sc, IE_XBD_BUF(xbase, head),
1230 IE_XBUF_ADDR(sc, head));
1231
1232 if (++head == NTXBUF)
1233 head = 0;
1234 sc->xchead = head;
1235
1236 s = splnet();
1237 /* Start the first packet transmitting. */
1238 if (sc->xmit_busy == 0)
1239 iexmit(sc);
1240
1241 sc->xmit_busy++;
1242 splx(s);
1243 }
1244 }
1245
1246 /*
1247 * Probe IE's ram setup [ Move all this into MD front-end!? ]
1248 * Use only if SCP and ISCP represent offsets into shared ram space.
1249 */
1250 int
1251 i82586_proberam(sc)
1252 struct ie_softc *sc;
1253 {
1254 int result, off;
1255
1256 /* Put in 16-bit mode */
1257 off = IE_SCP_BUS_USE(sc->scp);
1258 sc->ie_bus_write16(sc, off, 0);
1259 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
1260
1261 /* Set the ISCP `busy' bit */
1262 off = IE_ISCP_BUSY(sc->iscp);
1263 sc->ie_bus_write16(sc, off, 1);
1264 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_WRITE);
1265
1266 if (sc->hwreset)
1267 (sc->hwreset)(sc, CHIP_PROBE);
1268
1269 (sc->chan_attn) (sc, CHIP_PROBE);
1270
1271 delay(100); /* wait a while... */
1272
1273 /* Read back the ISCP `busy' bit; it should be clear by now */
1274 off = IE_ISCP_BUSY(sc->iscp);
1275 IE_BUS_BARRIER(sc, off, 2, BUS_SPACE_BARRIER_READ);
1276 result = sc->ie_bus_read16(sc, off) == 0;
1277
1278 /* Acknowledge any interrupts we may have caused. */
1279 ie_ack(sc, IE_ST_WHENCE);
1280
1281 return (result);
1282 }
1283
1284 void
1285 i82586_reset(sc, hard)
1286 struct ie_softc *sc;
1287 int hard;
1288 {
1289 int s = splnet();
1290
1291 if (hard)
1292 printf("%s: reset\n", sc->sc_dev.dv_xname);
1293
1294 /* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
1295 sc->sc_ethercom.ec_if.if_timer = 0;
1296 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1297
1298 /*
1299 * Stop i82586 dead in its tracks.
1300 */
1301 if (i82586_start_cmd(sc, IE_RUC_ABORT | IE_CUC_ABORT, 0, 0, 0))
1302 printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
1303
1304 /*
1305 * This can really slow down the i82586_reset() on some cards, but it's
1306 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
1307 * lockups.
1308 */
1309 if (hard && sc->hwreset)
1310 (sc->hwreset)(sc, CARD_RESET);
1311
1312 delay(100);
1313 ie_ack(sc, IE_ST_WHENCE);
1314
1315 if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0) {
1316 int retries=0; /* XXX - find out why init sometimes fails */
1317 while (retries++ < 2)
1318 if (i82586_init(&sc->sc_ethercom.ec_if) == 0)
1319 break;
1320 }
1321
1322 splx(s);
1323 }
1324
1325
1326 static void
1327 setup_simple_command(sc, cmd, cmdbuf)
1328 struct ie_softc *sc;
1329 int cmd;
1330 int cmdbuf;
1331 {
1332 /* Setup a simple command */
1333 sc->ie_bus_write16(sc, IE_CMD_COMMON_STATUS(cmdbuf), 0);
1334 sc->ie_bus_write16(sc, IE_CMD_COMMON_CMD(cmdbuf), cmd | IE_CMD_LAST);
1335 sc->ie_bus_write16(sc, IE_CMD_COMMON_LINK(cmdbuf), 0xffff);
1336
1337 /* Assign the command buffer to the SCB command list */
1338 sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb), cmdbuf);
1339 }
1340
1341 /*
1342 * Run the time-domain reflectometer.
1343 */
1344 static void
1345 ie_run_tdr(sc, cmd)
1346 struct ie_softc *sc;
1347 int cmd;
1348 {
1349 int result;
1350
1351 setup_simple_command(sc, IE_CMD_TDR, cmd);
1352 sc->ie_bus_write16(sc, IE_CMD_TDR_TIME(cmd), 0);
1353
1354 if (i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0) ||
1355 (sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd)) & IE_STAT_OK) == 0)
1356 result = 0x10000; /* XXX */
1357 else
1358 result = sc->ie_bus_read16(sc, IE_CMD_TDR_TIME(cmd));
1359
1360 /* Squash any pending interrupts */
1361 ie_ack(sc, IE_ST_WHENCE);
1362
1363 if (result & IE_TDR_SUCCESS)
1364 return;
1365
1366 if (result & 0x10000)
1367 printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
1368 else if (result & IE_TDR_XCVR)
1369 printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
1370 else if (result & IE_TDR_OPEN)
1371 printf("%s: TDR detected incorrect termination %d clocks away\n",
1372 sc->sc_dev.dv_xname, result & IE_TDR_TIME);
1373 else if (result & IE_TDR_SHORT)
1374 printf("%s: TDR detected a short circuit %d clocks away\n",
1375 sc->sc_dev.dv_xname, result & IE_TDR_TIME);
1376 else
1377 printf("%s: TDR returned unknown status 0x%x\n",
1378 sc->sc_dev.dv_xname, result);
1379 }
1380
1381
1382 /*
1383 * i82586_setup_bufs: set up the buffers
1384 *
1385 * We have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1386 * this is to be used for the buffers. The chip indexs its control data
1387 * structures with 16 bit offsets, and it indexes actual buffers with
1388 * 24 bit addresses. So we should allocate control buffers first so that
1389 * we don't overflow the 16 bit offset field. The number of transmit
1390 * buffers is fixed at compile time.
1391 *
1392 */
1393 static void
1394 i82586_setup_bufs(sc)
1395 struct ie_softc *sc;
1396 {
1397 int ptr = sc->buf_area; /* memory pool */
1398 int n, r;
1399
1400 /*
1401 * step 0: zero memory and figure out how many recv buffers and
1402 * frames we can have.
1403 */
1404 ptr = (ptr + 3) & ~3; /* set alignment and stick with it */
1405
1406
1407 /*
1408 * step 1: lay out data structures in the shared-memory area
1409 */
1410
1411 /* The no-op commands; used if "nop-chaining" is in effect */
1412 sc->nop_cmds = ptr;
1413 ptr += NTXBUF * IE_CMD_NOP_SZ;
1414
1415 /* The transmit commands */
1416 sc->xmit_cmds = ptr;
1417 ptr += NTXBUF * IE_CMD_XMIT_SZ;
1418
1419 /* The transmit buffers descriptors */
1420 sc->xbds = ptr;
1421 ptr += NTXBUF * IE_XBD_SZ;
1422
1423 /* The transmit buffers */
1424 sc->xbufs = ptr;
1425 ptr += NTXBUF * IE_TBUF_SIZE;
1426
1427 ptr = (ptr + 3) & ~3; /* re-align.. just in case */
1428
1429 /* Compute free space for RECV stuff */
1430 n = sc->buf_area_sz - (ptr - sc->buf_area);
1431
1432 /* Compute size of one RECV frame */
1433 r = IE_RFRAME_SZ + ((IE_RBD_SZ + IE_RBUF_SIZE) * B_PER_F);
1434
1435 sc->nframes = n / r;
1436
1437 if (sc->nframes <= 0)
1438 panic("ie: bogus buffer calc\n");
1439
1440 sc->nrxbuf = sc->nframes * B_PER_F;
1441
1442 /* The receice frame descriptors */
1443 sc->rframes = ptr;
1444 ptr += sc->nframes * IE_RFRAME_SZ;
1445
1446 /* The receive buffer descriptors */
1447 sc->rbds = ptr;
1448 ptr += sc->nrxbuf * IE_RBD_SZ;
1449
1450 /* The receive buffers */
1451 sc->rbufs = ptr;
1452 ptr += sc->nrxbuf * IE_RBUF_SIZE;
1453
1454 #if I82586_DEBUG
1455 printf("%s: %d frames %d bufs\n", sc->sc_dev.dv_xname, sc->nframes,
1456 sc->nrxbuf);
1457 #endif
1458
1459 /*
1460 * step 2: link together the recv frames and set EOL on last one
1461 */
1462 for (n = 0; n < sc->nframes; n++) {
1463 int m = (n == sc->nframes - 1) ? 0 : n + 1;
1464
1465 /* Clear status */
1466 sc->ie_bus_write16(sc, IE_RFRAME_STATUS(sc->rframes,n), 0);
1467
1468 /* RBD link = NULL */
1469 sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,n),
1470 0xffff);
1471
1472 /* Make a circular list */
1473 sc->ie_bus_write16(sc, IE_RFRAME_NEXT(sc->rframes,n),
1474 IE_RFRAME_ADDR(sc->rframes,m));
1475
1476 /* Mark last as EOL */
1477 sc->ie_bus_write16(sc, IE_RFRAME_LAST(sc->rframes,n),
1478 ((m==0)? (IE_FD_EOL|IE_FD_SUSP) : 0));
1479 }
1480
1481 /*
1482 * step 3: link the RBDs and set EOL on last one
1483 */
1484 for (n = 0; n < sc->nrxbuf; n++) {
1485 int m = (n == sc->nrxbuf - 1) ? 0 : n + 1;
1486
1487 /* Clear status */
1488 sc->ie_bus_write16(sc, IE_RBD_STATUS(sc->rbds,n), 0);
1489
1490 /* Make a circular list */
1491 sc->ie_bus_write16(sc, IE_RBD_NEXT(sc->rbds,n),
1492 IE_RBD_ADDR(sc->rbds,m));
1493
1494 /* Link to data buffers */
1495 sc->ie_bus_write24(sc, IE_RBD_BUFADDR(sc->rbds, n),
1496 IE_RBUF_ADDR(sc, n));
1497 sc->ie_bus_write16(sc, IE_RBD_BUFLEN(sc->rbds,n),
1498 IE_RBUF_SIZE | ((m==0)?IE_RBD_EOL:0));
1499 }
1500
1501 /*
1502 * step 4: all xmit no-op commands loopback onto themselves
1503 */
1504 for (n = 0; n < NTXBUF; n++) {
1505 sc->ie_bus_write16(sc, IE_CMD_NOP_STATUS(sc->nop_cmds, n), 0);
1506
1507 sc->ie_bus_write16(sc, IE_CMD_NOP_CMD(sc->nop_cmds, n),
1508 IE_CMD_NOP);
1509
1510 sc->ie_bus_write16(sc, IE_CMD_NOP_LINK(sc->nop_cmds, n),
1511 IE_CMD_NOP_ADDR(sc->nop_cmds, n));
1512 }
1513
1514
1515 /*
1516 * step 6: set the head and tail pointers on receive to keep track of
1517 * the order in which RFDs and RBDs are used.
1518 */
1519
1520 /* Pointers to last packet sent and next available transmit buffer. */
1521 sc->xchead = sc->xctail = 0;
1522
1523 /* Clear transmit-busy flag and set number of free transmit buffers. */
1524 sc->xmit_busy = 0;
1525
1526 /*
1527 * Pointers to first and last receive frame.
1528 * The RFD pointed to by rftail is the only one that has EOL set.
1529 */
1530 sc->rfhead = 0;
1531 sc->rftail = sc->nframes - 1;
1532
1533 /*
1534 * Pointers to first and last receive descriptor buffer.
1535 * The RBD pointed to by rbtail is the only one that has EOL set.
1536 */
1537 sc->rbhead = 0;
1538 sc->rbtail = sc->nrxbuf - 1;
1539
1540 /* link in recv frames * and buffer into the scb. */
1541 #if I82586_DEBUG
1542 printf("%s: reserved %d bytes\n",
1543 sc->sc_dev.dv_xname, ptr - sc->buf_area);
1544 #endif
1545 }
1546
1547 static int
1548 ie_cfg_setup(sc, cmd, promiscuous, manchester)
1549 struct ie_softc *sc;
1550 int cmd;
1551 int promiscuous, manchester;
1552 {
1553 int cmdresult, status;
1554 u_int8_t buf[IE_CMD_CFG_SZ]; /* XXX malloc? */
1555
1556 *IE_CMD_CFG_CNT(buf) = 0x0c;
1557 *IE_CMD_CFG_FIFO(buf) = 8;
1558 *IE_CMD_CFG_SAVEBAD(buf) = 0x40;
1559 *IE_CMD_CFG_ADDRLEN(buf) = 0x2e;
1560 *IE_CMD_CFG_PRIORITY(buf) = 0;
1561 *IE_CMD_CFG_IFS(buf) = 0x60;
1562 *IE_CMD_CFG_SLOT_LOW(buf) = 0;
1563 *IE_CMD_CFG_SLOT_HIGH(buf) = 0xf2;
1564 *IE_CMD_CFG_PROMISC(buf) = !!promiscuous | manchester << 2;
1565 *IE_CMD_CFG_CRSCDT(buf) = 0;
1566 *IE_CMD_CFG_MINLEN(buf) = 64;
1567 *IE_CMD_CFG_JUNK(buf) = 0xff;
1568 sc->memcopyout(sc, buf, cmd, IE_CMD_CFG_SZ);
1569 setup_simple_command(sc, IE_CMD_CONFIG, cmd);
1570 IE_BUS_BARRIER(sc, cmd, IE_CMD_CFG_SZ, BUS_SPACE_BARRIER_WRITE);
1571
1572 cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmd, IE_STAT_COMPL, 0);
1573 status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmd));
1574 if (cmdresult != 0) {
1575 printf("%s: configure command timed out; status %x\n",
1576 sc->sc_dev.dv_xname, status);
1577 return (0);
1578 }
1579 if ((status & IE_STAT_OK) == 0) {
1580 printf("%s: configure command failed; status %x\n",
1581 sc->sc_dev.dv_xname, status);
1582 return (0);
1583 }
1584
1585 /* Squash any pending interrupts */
1586 ie_ack(sc, IE_ST_WHENCE);
1587 return (1);
1588 }
1589
1590 static int
1591 ie_ia_setup(sc, cmdbuf)
1592 struct ie_softc *sc;
1593 int cmdbuf;
1594 {
1595 int cmdresult, status;
1596 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1597
1598 setup_simple_command(sc, IE_CMD_IASETUP, cmdbuf);
1599
1600 (sc->memcopyout)(sc, LLADDR(ifp->if_sadl),
1601 IE_CMD_IAS_EADDR(cmdbuf), ETHER_ADDR_LEN);
1602
1603 cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
1604 status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
1605 if (cmdresult != 0) {
1606 printf("%s: individual address command timed out; status %x\n",
1607 sc->sc_dev.dv_xname, status);
1608 return (0);
1609 }
1610 if ((status & IE_STAT_OK) == 0) {
1611 printf("%s: individual address command failed; status %x\n",
1612 sc->sc_dev.dv_xname, status);
1613 return (0);
1614 }
1615
1616 /* Squash any pending interrupts */
1617 ie_ack(sc, IE_ST_WHENCE);
1618 return (1);
1619 }
1620
1621 /*
1622 * Run the multicast setup command.
1623 * Called at splnet().
1624 */
1625 static int
1626 ie_mc_setup(sc, cmdbuf)
1627 struct ie_softc *sc;
1628 int cmdbuf;
1629 {
1630 int cmdresult, status;
1631
1632 if (sc->mcast_count == 0)
1633 return (1);
1634
1635 setup_simple_command(sc, IE_CMD_MCAST, cmdbuf);
1636
1637 (sc->memcopyout)(sc, (caddr_t)sc->mcast_addrs,
1638 IE_CMD_MCAST_MADDR(cmdbuf),
1639 sc->mcast_count * ETHER_ADDR_LEN);
1640
1641 sc->ie_bus_write16(sc, IE_CMD_MCAST_BYTES(cmdbuf),
1642 sc->mcast_count * ETHER_ADDR_LEN);
1643
1644 /* Start the command */
1645 cmdresult = i82586_start_cmd(sc, IE_CUC_START, cmdbuf, IE_STAT_COMPL, 0);
1646 status = sc->ie_bus_read16(sc, IE_CMD_COMMON_STATUS(cmdbuf));
1647 if (cmdresult != 0) {
1648 printf("%s: multicast setup command timed out; status %x\n",
1649 sc->sc_dev.dv_xname, status);
1650 return (0);
1651 }
1652 if ((status & IE_STAT_OK) == 0) {
1653 printf("%s: multicast setup command failed; status %x\n",
1654 sc->sc_dev.dv_xname, status);
1655 return (0);
1656 }
1657
1658 /* Squash any pending interrupts */
1659 ie_ack(sc, IE_ST_WHENCE);
1660 return (1);
1661 }
1662
1663 /*
1664 * This routine takes the environment generated by check_ie_present() and adds
1665 * to it all the other structures we need to operate the adapter. This
1666 * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
1667 * the receiver unit, and clearing interrupts.
1668 *
1669 * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1670 */
1671 int
1672 i82586_init(ifp)
1673 struct ifnet *ifp;
1674 {
1675 struct ie_softc *sc = ifp->if_softc;
1676 int cmd;
1677
1678 sc->async_cmd_inprogress = 0;
1679
1680 cmd = sc->buf_area;
1681
1682 /*
1683 * Send the configure command first.
1684 */
1685 if (ie_cfg_setup(sc, cmd, sc->promisc, 0) == 0)
1686 return (1);
1687
1688 /*
1689 * Send the Individual Address Setup command.
1690 */
1691 if (ie_ia_setup(sc, cmd) == 0)
1692 return (1);
1693
1694 /*
1695 * Run the time-domain reflectometer.
1696 */
1697 ie_run_tdr(sc, cmd);
1698
1699 /*
1700 * Set the multi-cast filter, if any
1701 */
1702 if (ie_mc_setup(sc, cmd) == 0)
1703 return (1);
1704
1705 /*
1706 * Acknowledge any interrupts we have generated thus far.
1707 */
1708 ie_ack(sc, IE_ST_WHENCE);
1709
1710 /*
1711 * Set up the transmit and recv buffers.
1712 */
1713 i82586_setup_bufs(sc);
1714
1715 if (sc->hwinit)
1716 (sc->hwinit)(sc);
1717
1718 ifp->if_flags |= IFF_RUNNING;
1719 ifp->if_flags &= ~IFF_OACTIVE;
1720
1721 if (NTXBUF < 2)
1722 sc->do_xmitnopchain = 0;
1723
1724 i82586_start_transceiver(sc);
1725 return (0);
1726 }
1727
1728 /*
1729 * Start the RU and possibly the CU unit
1730 */
1731 static void
1732 i82586_start_transceiver(sc)
1733 struct ie_softc *sc;
1734 {
1735
1736 /*
1737 * Start RU at current position in frame & RBD lists.
1738 */
1739 sc->ie_bus_write16(sc, IE_RFRAME_BUFDESC(sc->rframes,sc->rfhead),
1740 IE_RBD_ADDR(sc->rbds, sc->rbhead));
1741
1742 sc->ie_bus_write16(sc, IE_SCB_RCVLST(sc->scb),
1743 IE_RFRAME_ADDR(sc->rframes,sc->rfhead));
1744
1745 if (sc->do_xmitnopchain) {
1746 /* Stop transmit command chain */
1747 if (i82586_start_cmd(sc, IE_CUC_SUSPEND|IE_RUC_SUSPEND, 0, 0, 0))
1748 printf("%s: CU/RU stop command timed out\n",
1749 sc->sc_dev.dv_xname);
1750
1751 /* Start the receiver & transmitter chain */
1752 /* sc->scb->ie_command_list =
1753 IEADDR(sc->nop_cmds[(sc->xctail+NTXBUF-1) % NTXBUF]);*/
1754 sc->ie_bus_write16(sc, IE_SCB_CMDLST(sc->scb),
1755 IE_CMD_NOP_ADDR(
1756 sc->nop_cmds,
1757 (sc->xctail + NTXBUF - 1) % NTXBUF));
1758
1759 if (i82586_start_cmd(sc, IE_CUC_START|IE_RUC_START, 0, 0, 0))
1760 printf("%s: CU/RU command timed out\n",
1761 sc->sc_dev.dv_xname);
1762 } else {
1763 if (i82586_start_cmd(sc, IE_RUC_START, 0, 0, 0))
1764 printf("%s: RU command timed out\n",
1765 sc->sc_dev.dv_xname);
1766 }
1767 }
1768
1769 void
1770 i82586_stop(ifp, disable)
1771 struct ifnet *ifp;
1772 int disable;
1773 {
1774 struct ie_softc *sc = ifp->if_softc;
1775
1776 if (i82586_start_cmd(sc, IE_RUC_SUSPEND | IE_CUC_SUSPEND, 0, 0, 0))
1777 printf("%s: iestop: disable commands timed out\n",
1778 sc->sc_dev.dv_xname);
1779 }
1780
1781 int
1782 i82586_ioctl(ifp, cmd, data)
1783 struct ifnet *ifp;
1784 u_long cmd;
1785 caddr_t data;
1786 {
1787 struct ie_softc *sc = ifp->if_softc;
1788 struct ifreq *ifr = (struct ifreq *)data;
1789 int s, error = 0;
1790
1791 s = splnet();
1792 switch(cmd) {
1793 case SIOCGIFMEDIA:
1794 case SIOCSIFMEDIA:
1795 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1796 break;
1797 default:
1798 error = ether_ioctl(ifp, cmd, data);
1799 if (error == ENETRESET) {
1800 /*
1801 * Multicast list has changed; set the hardware filter
1802 * accordingly.
1803 */
1804 ie_mc_reset(sc);
1805 error = 0;
1806 }
1807 break;
1808 }
1809 #if I82586_DEBUG
1810 if (cmd == SIOCSIFFLAGS)
1811 sc->sc_debug = (ifp->if_flags & IFF_DEBUG) ? IED_ALL : 0;
1812 #endif
1813 splx(s);
1814 return (error);
1815 }
1816
1817 static void
1818 ie_mc_reset(sc)
1819 struct ie_softc *sc;
1820 {
1821 struct ether_multi *enm;
1822 struct ether_multistep step;
1823 int size;
1824
1825 /*
1826 * Step through the list of addresses.
1827 */
1828 again:
1829 size = 0;
1830 sc->mcast_count = 0;
1831 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1832 while (enm) {
1833 size += 6;
1834 if (sc->mcast_count >= IE_MAXMCAST ||
1835 bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
1836 sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
1837 i82586_ioctl(&sc->sc_ethercom.ec_if,
1838 SIOCSIFFLAGS, (void *)0);
1839 return;
1840 }
1841 ETHER_NEXT_MULTI(step, enm);
1842 }
1843
1844 if (size > sc->mcast_addrs_size) {
1845 /* Need to allocate more space */
1846 if (sc->mcast_addrs_size)
1847 free(sc->mcast_addrs, M_IPMADDR);
1848 sc->mcast_addrs = (char *)
1849 malloc(size, M_IPMADDR, M_WAITOK);
1850 sc->mcast_addrs_size = size;
1851 }
1852
1853 /*
1854 * We've got the space; now copy the addresses
1855 */
1856 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1857 while (enm) {
1858 if (sc->mcast_count >= IE_MAXMCAST)
1859 goto again; /* Just in case */
1860
1861 bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
1862 sc->mcast_count++;
1863 ETHER_NEXT_MULTI(step, enm);
1864 }
1865 sc->want_mcsetup = 1;
1866 }
1867
1868 /*
1869 * Media change callback.
1870 */
1871 int
1872 i82586_mediachange(ifp)
1873 struct ifnet *ifp;
1874 {
1875 struct ie_softc *sc = ifp->if_softc;
1876
1877 if (sc->sc_mediachange)
1878 return ((*sc->sc_mediachange)(sc));
1879 return (0);
1880 }
1881
1882 /*
1883 * Media status callback.
1884 */
1885 void
1886 i82586_mediastatus(ifp, ifmr)
1887 struct ifnet *ifp;
1888 struct ifmediareq *ifmr;
1889 {
1890 struct ie_softc *sc = ifp->if_softc;
1891
1892 if (sc->sc_mediastatus)
1893 (*sc->sc_mediastatus)(sc, ifmr);
1894 }
1895
1896 #if I82586_DEBUG
1897 void
1898 print_rbd(sc, n)
1899 struct ie_softc *sc;
1900 int n;
1901 {
1902
1903 printf("RBD at %08x:\n status %04x, next %04x, buffer %lx\n"
1904 "length/EOL %04x\n", IE_RBD_ADDR(sc->rbds,n),
1905 sc->ie_bus_read16(sc, IE_RBD_STATUS(sc->rbds,n)),
1906 sc->ie_bus_read16(sc, IE_RBD_NEXT(sc->rbds,n)),
1907 (u_long)0,/*bus_space_read_4(sc->bt, sc->bh, IE_RBD_BUFADDR(sc->rbds,n)),-* XXX */
1908 sc->ie_bus_read16(sc, IE_RBD_BUFLEN(sc->rbds,n)));
1909 }
1910 #endif
1911