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