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