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