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