i82586.c revision 1.8 1 /* $NetBSD: i82586.c,v 1.8 1998/01/06 04:55:52 perry Exp $ */
2
3 /*-
4 * Copyright (c) 1997 Paul Kranenburg.
5 * Copyright (c) 1993, 1994, 1995 Charles Hannum.
6 * Copyright (c) 1992, 1993, University of Vermont and State
7 * Agricultural College.
8 * Copyright (c) 1992, 1993, Garrett A. Wollman.
9 *
10 * Portions:
11 * Copyright (c) 1994, 1995, Rafal K. Boni
12 * Copyright (c) 1990, 1991, William F. Jolitz
13 * Copyright (c) 1990, The Regents of the University of California
14 *
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by Charles Hannum, by the
28 * University of Vermont and State Agricultural College and Garrett A.
29 * Wollman, by William F. Jolitz, and by the University of California,
30 * Berkeley, Lawrence Berkeley Laboratory, and its contributors.
31 * 4. Neither the names of the Universities nor the names of the authors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * Intel 82586 Ethernet chip
50 * Register, bit, and structure definitions.
51 *
52 * Original StarLAN driver written by Garrett Wollman with reference to the
53 * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
54 *
55 * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
56 *
57 * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
58 *
59 * Majorly cleaned up and 3C507 code merged by Charles Hannum.
60 *
61 * Converted to SUN ie driver by Charles D. Cranor,
62 * October 1994, January 1995.
63 * This sun version based on i386 version 1.30.
64 */
65
66 /*
67 * The i82586 is a very painful chip, found in sun3's, sun-4/100's
68 * sun-4/200's, and VME based suns. The byte order is all wrong for a
69 * SUN, making life difficult. Programming this chip is mostly the same,
70 * but certain details differ from system to system. This driver is
71 * written so that different "ie" interfaces can be controled by the same
72 * driver.
73 */
74
75 /*
76 Mode of operation:
77
78 We run the 82586 in a standard Ethernet mode. We keep NFRAMES
79 received frame descriptors around for the receiver to use, and
80 NRXBUF associated receive buffer descriptors, both in a circular
81 list. Whenever a frame is received, we rotate both lists as
82 necessary. (The 586 treats both lists as a simple queue.) We also
83 keep a transmit command around so that packets can be sent off
84 quickly.
85
86 We configure the adapter in AL-LOC = 1 mode, which means that the
87 Ethernet/802.3 MAC header is placed at the beginning of the receive
88 buffer rather than being split off into various fields in the RFD.
89 This also means that we must include this header in the transmit
90 buffer as well.
91
92 By convention, all transmit commands, and only transmit commands,
93 shall have the I (IE_CMD_INTR) bit set in the command. This way,
94 when an interrupt arrives at ieintr(), it is immediately possible
95 to tell what precisely caused it. ANY OTHER command-sending
96 routines should run at splnet(), and should post an acknowledgement
97 to every interrupt they generate.
98
99 To save the expense of shipping a command to 82586 every time we
100 want to send a frame, we use a linked list of commands consisting
101 of alternate XMIT and NOP commands. The links of these elements
102 are manipulated (in iexmit()) such that the NOP command loops back
103 to itself whenever the following XMIT command is not yet ready to
104 go. Whenever an XMIT is ready, the preceding NOP link is pointed
105 at it, while its own link field points to the following NOP command.
106 Thus, a single transmit command sets off an interlocked traversal
107 of the xmit command chain, with the host processor in control of
108 the synchronization.
109 */
110
111 #include "bpfilter.h"
112
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/mbuf.h>
116 #include <sys/buf.h>
117 #include <sys/protosw.h>
118 #include <sys/socket.h>
119 #include <sys/ioctl.h>
120 #include <sys/errno.h>
121 #include <sys/syslog.h>
122 #include <sys/device.h>
123
124 #include <net/if.h>
125 #include <net/if_dl.h>
126 #include <net/if_types.h>
127 #include <net/if_media.h>
128 #include <net/if_ether.h>
129
130 #if NBPFILTER > 0
131 #include <net/bpf.h>
132 #include <net/bpfdesc.h>
133 #endif
134
135 #ifdef INET
136 #include <netinet/in.h>
137 #include <netinet/in_systm.h>
138 #include <netinet/in_var.h>
139 #include <netinet/ip.h>
140 #include <netinet/if_inarp.h>
141 #endif
142
143 #ifdef NS
144 #include <netns/ns.h>
145 #include <netns/ns_if.h>
146 #endif
147
148 #include <machine/bus.h>
149
150 #include <dev/ic/i82586reg.h>
151 #include <dev/ic/i82586var.h>
152
153 void i82586_watchdog __P((struct ifnet *));
154 int i82586_init __P((struct ie_softc *));
155 int i82586_ioctl __P((struct ifnet *, u_long, caddr_t));
156 void i82586_start __P((struct ifnet *));
157 int i82586_setupram __P((struct ie_softc *));
158
159 void i82586_rint __P((struct ie_softc *));
160 void i82586_tint __P((struct ie_softc *));
161
162 int i82586_mediachange __P((struct ifnet *));
163 void i82586_mediastatus __P((struct ifnet *,
164 struct ifmediareq *));
165
166 static void ie_readframe __P((struct ie_softc *, int));
167 static void ie_drop_packet_buffer __P((struct ie_softc *));
168 static int command_and_wait __P((struct ie_softc *, int,
169 void volatile *, int));
170 static struct mbuf *ieget __P((struct ie_softc *,
171 struct ether_header *, int *));
172 static void setup_bufs __P((struct ie_softc *));
173 static int mc_setup __P((struct ie_softc *, void *));
174 static void mc_reset __P((struct ie_softc *));
175
176 static __inline__ int ether_equal __P((u_char *, u_char *));
177 static __inline__ void ie_ack __P((struct ie_softc *, u_int));
178 static __inline__ void ie_setup_config __P((volatile struct ie_config_cmd *,
179 int, int));
180 static __inline__ int check_eh __P((struct ie_softc *,
181 struct ether_header *, int *));
182 static __inline__ int ie_buflen __P((struct ie_softc *, int));
183 static __inline__ int ie_packet_len __P((struct ie_softc *));
184 static __inline__ void iexmit __P((struct ie_softc *));
185 static void i82586_start_transceiver
186 __P((struct ie_softc *));
187
188 static void run_tdr __P((struct ie_softc *, struct ie_tdr_cmd *));
189 static void iestop __P((struct ie_softc *));
190
191 #ifdef I82586_DEBUG
192 void print_rbd __P((volatile struct ie_recv_buf_desc *));
193
194 int in_i82586_rint = 0;
195 int in_i82586_tint = 0;
196 int spur_intr = 0;
197
198 #endif
199
200 /*
201 * Address generation macros:
202 * MK_24 = KVA -> 24 bit address in native byte order
203 * MK_16 = KVA -> 16 bit address in INTEL byte order
204 * ST_24 = store a 24 bit address in native byte order to INTEL byte order
205 */
206 #define MK_24(base, ptr) ((caddr_t)((u_long)ptr - (u_long)base))
207
208 #if BYTE_ORDER == BIG_ENDIAN
209 #define XSWAP(y) ( ((y) >> 8) | ((y) << 8) )
210 #define SWAP(x) ({u_short _z=(x); (u_short)XSWAP(_z);})
211
212 #define MK_16(base, ptr) SWAP((u_short)( ((u_long)(ptr)) - ((u_long)(base)) ))
213 #define ST_24(to, from) { \
214 u_long fval = (u_long)(from); \
215 u_char *t = (u_char *)&(to), *f = (u_char *)&fval; \
216 t[0] = f[3]; t[1] = f[2]; t[2] = f[1]; /*t[3] = f[0] ;*/ \
217 }
218 #else
219 #define SWAP(x) x
220 #define MK_16(base, ptr) ((u_short)(u_long)MK_24(base, ptr))
221 #define ST_24(to, from) {to = (from);}
222 #endif
223
224 /*
225 * Here are a few useful functions. We could have done these as macros, but
226 * since we have the inline facility, it makes sense to use that instead.
227 */
228 static __inline__ void
229 ie_setup_config(cmd, promiscuous, manchester)
230 volatile struct ie_config_cmd *cmd;
231 int promiscuous, manchester;
232 {
233
234 cmd->ie_config_count = 0x0c;
235 cmd->ie_fifo = 8;
236 cmd->ie_save_bad = 0x40;
237 cmd->ie_addr_len = 0x2e;
238 cmd->ie_priority = 0;
239 cmd->ie_ifs = 0x60;
240 cmd->ie_slot_low = 0;
241 cmd->ie_slot_high = 0xf2;
242 cmd->ie_promisc = !!promiscuous | manchester << 2;
243 cmd->ie_crs_cdt = 0;
244 cmd->ie_min_len = 64;
245 cmd->ie_junk = 0xff;
246 }
247
248 static __inline__ void
249 ie_ack(sc, mask)
250 struct ie_softc *sc;
251 u_int mask; /* in native byte-order */
252 {
253 volatile struct ie_sys_ctl_block *scb = sc->scb;
254
255 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
256 command_and_wait(sc, SWAP(scb->ie_status) & mask, 0, 0);
257 }
258
259 void
260 ie_attach(sc, name, etheraddr, media, nmedia, defmedia)
261 struct ie_softc *sc;
262 char *name;
263 u_int8_t *etheraddr;
264 int *media, nmedia, defmedia;
265 {
266 int i;
267 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
268
269 if (i82586_setupram(sc) == 0) { /* XXX - ISA version? */
270 printf(": RAM CONFIG FAILED!\n");
271 /* XXX should reclaim resources? */
272 return;
273 }
274
275 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
276 ifp->if_softc = sc;
277 ifp->if_start = i82586_start;
278 ifp->if_ioctl = i82586_ioctl;
279 ifp->if_watchdog = i82586_watchdog;
280 ifp->if_flags =
281 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
282
283 /* Initialize media goo. */
284 ifmedia_init(&sc->sc_media, 0, i82586_mediachange, i82586_mediastatus);
285 if (media != NULL) {
286 for (i = 0; i < nmedia; i++)
287 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
288 ifmedia_set(&sc->sc_media, defmedia);
289 } else {
290 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
291 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
292 }
293
294 /* Attach the interface. */
295 if_attach(ifp);
296 ether_ifattach(ifp, etheraddr);
297
298 printf(" address %s, type %s\n", ether_sprintf(etheraddr), name);
299
300 #if NBPFILTER > 0
301 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
302 #endif
303 }
304
305
306 /*
307 * Device timeout/watchdog routine. Entered if the device neglects to generate
308 * an interrupt after a transmit has been started on it.
309 */
310 void
311 i82586_watchdog(ifp)
312 struct ifnet *ifp;
313 {
314 struct ie_softc *sc = ifp->if_softc;
315
316 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
317 ++ifp->if_oerrors;
318
319 i82586_reset(sc, 1);
320 }
321
322 /*
323 * What to do upon receipt of an interrupt.
324 */
325 int
326 ieintr(v)
327 void *v;
328 {
329 struct ie_softc *sc = v;
330 u_int status;
331
332 /*
333 * Implementation dependent interrupt handling.
334 */
335 if (sc->intrhook)
336 (sc->intrhook)(sc, INTR_ENTER);
337
338 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
339 status = SWAP(sc->scb->ie_status) & IE_ST_WHENCE;
340
341 if (status == 0) {
342 #ifdef I82586_DEBUG
343 if ((++spur_intr % 25) == 0)
344 printf("%s: ieintr; %d spurious interrupts\n",
345 sc->sc_dev.dv_xname, spur_intr);
346 #endif
347 if (sc->intrhook)
348 (sc->intrhook)(sc, INTR_EXIT);
349
350 return (0);
351 }
352
353 loop:
354 /* Ack interrupts FIRST in case we receive more during the ISR. */
355 ie_ack(sc, status);
356
357 if (status & (IE_ST_FR | IE_ST_RNR)) {
358 #ifdef I82586_DEBUG
359 in_i82586_rint++;
360 if (sc->sc_debug & IED_RINT)
361 printf("%s: rint\n", sc->sc_dev.dv_xname);
362 #endif
363 i82586_rint(sc);
364 #ifdef I82586_DEBUG
365 in_i82586_rint--;
366 #endif
367 }
368
369 if (status & IE_ST_CX) {
370 #ifdef I82586_DEBUG
371 in_i82586_tint++;
372 if (sc->sc_debug & IED_TINT)
373 printf("%s: tint\n", sc->sc_dev.dv_xname);
374 #endif
375 i82586_tint(sc);
376 #ifdef I82586_DEBUG
377 in_i82586_tint--;
378 #endif
379 }
380
381 if (status & IE_ST_RNR) {
382 printf("%s: receiver not ready; status=0x%x\n",
383 sc->sc_dev.dv_xname, status);
384 sc->sc_ethercom.ec_if.if_ierrors++;
385
386 i82586_reset(sc, 1);
387
388 if (sc->intrhook)
389 (sc->intrhook)(sc, INTR_EXIT);
390
391 return (1);
392 }
393
394 #ifdef I82586_DEBUG
395 if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
396 printf("%s: cna; status=0x%x\n", sc->sc_dev.dv_xname, status);
397 #endif
398 if (sc->intrhook)
399 (sc->intrhook)(sc, INTR_LOOP);
400
401 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
402 status = SWAP(sc->scb->ie_status) & IE_ST_WHENCE;
403 if (status != 0)
404 goto loop;
405
406 if (sc->intrhook)
407 (sc->intrhook)(sc, INTR_EXIT);
408
409 return (1);
410 }
411
412 /*
413 * Process a received-frame interrupt.
414 */
415 void
416 i82586_rint(sc)
417 struct ie_softc *sc;
418 {
419 volatile struct ie_sys_ctl_block *scb = sc->scb;
420 int i, status;
421 static int timesthru = 1024;
422
423 i = sc->rfhead;
424 for (;;) {
425 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
426 status = SWAP(sc->rframes[i]->ie_fd_status);
427
428 if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
429 if (--timesthru == 0) {
430 sc->sc_ethercom.ec_if.if_ierrors +=
431 SWAP(scb->ie_err_crc) +
432 SWAP(scb->ie_err_align) +
433 SWAP(scb->ie_err_resource) +
434 SWAP(scb->ie_err_overrun);
435 scb->ie_err_crc = scb->ie_err_align =
436 scb->ie_err_resource = scb->ie_err_overrun =
437 SWAP(0);
438 timesthru = 1024;
439 }
440 ie_readframe(sc, i);
441 } else {
442 if ((status & IE_FD_RNR) != 0 &&
443 (SWAP(scb->ie_status) & IE_RU_READY) == 0) {
444 i82586_start_transceiver(sc);
445 }
446 break;
447 }
448 i = (i + 1) % sc->nframes;
449 }
450 }
451
452 /*
453 * Process a command-complete interrupt. These are only generated by the
454 * transmission of frames. This routine is deceptively simple, since most of
455 * the real work is done by i82586_start().
456 */
457 void
458 i82586_tint(sc)
459 struct ie_softc *sc;
460 {
461 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
462 int status;
463
464 ifp->if_timer = 0;
465 ifp->if_flags &= ~IFF_OACTIVE;
466
467 #ifdef I82586_DEBUG
468 if (sc->xmit_busy <= 0) {
469 printf("i82586_tint: WEIRD: xmit_busy = %d, xctail = %d, xchead=%d\n",
470 sc->xmit_busy, sc->xctail, sc->xchead);
471 return;
472 }
473 #endif
474
475 status = SWAP(sc->xmit_cmds[sc->xctail]->ie_xmit_status);
476
477 if ((status & IE_STAT_COMPL) == 0 || (status & IE_STAT_BUSY)) {
478 printf("i82586_tint: command still busy; status=0x%x; tail=%d\n",
479 status, sc->xctail);
480 printf("iestatus = 0x%x\n", SWAP(sc->scb->ie_status));
481 }
482
483 if (status & IE_STAT_OK) {
484 ifp->if_opackets++;
485 ifp->if_collisions += (status & IE_XS_MAXCOLL);
486 } else {
487 ifp->if_oerrors++;
488 /*
489 * Check SQE and DEFERRED?
490 * What if more than one bit is set?
491 */
492 if (status & IE_STAT_ABORT)
493 printf("%s: send aborted\n", sc->sc_dev.dv_xname);
494 else if (status & IE_XS_NOCARRIER)
495 printf("%s: no carrier\n", sc->sc_dev.dv_xname);
496 else if (status & IE_XS_LOSTCTS)
497 printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
498 else if (status & IE_XS_UNDERRUN)
499 printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
500 else if (status & IE_XS_EXCMAX) {
501 printf("%s: too many collisions\n",
502 sc->sc_dev.dv_xname);
503 sc->sc_ethercom.ec_if.if_collisions += 16;
504 }
505 }
506
507 /*
508 * If multicast addresses were added or deleted while transmitting,
509 * mc_reset() set the want_mcsetup flag indicating that we should do
510 * it.
511 */
512 if (sc->want_mcsetup) {
513 mc_setup(sc, (caddr_t)sc->xmit_cbuffs[sc->xctail]);
514 sc->want_mcsetup = 0;
515 }
516
517 /* Done with the buffer. */
518 sc->xmit_busy--;
519 sc->xctail = (sc->xctail + 1) % NTXBUF;
520
521 /* Start the next packet, if any, transmitting. */
522 if (sc->xmit_busy > 0)
523 iexmit(sc);
524
525 i82586_start(ifp);
526 }
527
528 /*
529 * Compare two Ether/802 addresses for equality, inlined and unrolled for
530 * speed.
531 */
532 static __inline__ int
533 ether_equal(one, two)
534 u_char *one, *two;
535 {
536
537 if (one[5] != two[5] || one[4] != two[4] || one[3] != two[3] ||
538 one[2] != two[2] || one[1] != two[1] || one[0] != two[0])
539 return 0;
540 return 1;
541 }
542
543 /*
544 * Check for a valid address. to_bpf is filled in with one of the following:
545 * 0 -> BPF doesn't get this packet
546 * 1 -> BPF does get this packet
547 * 2 -> BPF does get this packet, but we don't
548 * Return value is true if the packet is for us, and false otherwise.
549 *
550 * This routine is a mess, but it's also critical that it be as fast
551 * as possible. It could be made cleaner if we can assume that the
552 * only client which will fiddle with IFF_PROMISC is BPF. This is
553 * probably a good assumption, but we do not make it here. (Yet.)
554 */
555 static __inline__ int
556 check_eh(sc, eh, to_bpf)
557 struct ie_softc *sc;
558 struct ether_header *eh;
559 int *to_bpf;
560 {
561 struct ifnet *ifp;
562 int i;
563
564 ifp = &sc->sc_ethercom.ec_if;
565
566 switch(sc->promisc) {
567 case IFF_ALLMULTI:
568 /*
569 * Receiving all multicasts, but no unicasts except those
570 * destined for us.
571 */
572 #if NBPFILTER > 0
573 /* BPF gets this packet if anybody cares */
574 *to_bpf = (ifp->if_bpf != 0);
575 #endif
576 if (eh->ether_dhost[0] & 1)
577 return 1;
578 if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
579 return 1;
580 return 0;
581
582 case IFF_PROMISC:
583 /*
584 * Receiving all packets. These need to be passed on to BPF.
585 */
586 #if NBPFILTER > 0
587 *to_bpf = (ifp->if_bpf != 0);
588 #endif
589 /* If for us, accept and hand up to BPF */
590 if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
591 return 1;
592
593 #if NBPFILTER > 0
594 if (*to_bpf)
595 *to_bpf = 2; /* we don't need to see it */
596 #endif
597
598 /*
599 * Not a multicast, so BPF wants to see it but we don't.
600 */
601 if ((eh->ether_dhost[0] & 1) == 0)
602 return 1;
603
604 /*
605 * If it's one of our multicast groups, accept it and pass it
606 * up.
607 */
608 for (i = 0; i < sc->mcast_count; i++) {
609 if (ether_equal(eh->ether_dhost,
610 (u_char *)&sc->mcast_addrs[i])) {
611 #if NBPFILTER > 0
612 if (*to_bpf)
613 *to_bpf = 1;
614 #endif
615 return 1;
616 }
617 }
618 return 1;
619
620 case IFF_ALLMULTI | IFF_PROMISC:
621 /*
622 * Acting as a multicast router, and BPF running at the same
623 * time. Whew! (Hope this is a fast machine...)
624 */
625 #if NBPFILTER > 0
626 *to_bpf = (ifp->if_bpf != 0);
627 #endif
628 /* We want to see multicasts. */
629 if (eh->ether_dhost[0] & 1)
630 return 1;
631
632 /* We want to see our own packets */
633 if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
634 return 1;
635
636 /* Anything else goes to BPF but nothing else. */
637 #if NBPFILTER > 0
638 if (*to_bpf)
639 *to_bpf = 2;
640 #endif
641 return 1;
642
643 default:
644 /*
645 * Only accept unicast packets destined for us, or multicasts
646 * for groups that we belong to. For now, we assume that the
647 * '586 will only return packets that we asked it for. This
648 * isn't strictly true (it uses hashing for the multicast
649 * filter), but it will do in this case, and we want to get
650 * out of here as quickly as possible.
651 */
652 #if NBPFILTER > 0
653 *to_bpf = (ifp->if_bpf != 0);
654 #endif
655 return 1;
656 }
657 return 0;
658 }
659
660 /*
661 * We want to isolate the bits that have meaning... This assumes that
662 * IE_RBUF_SIZE is an even power of two. If somehow the act_len exceeds
663 * the size of the buffer, then we are screwed anyway.
664 */
665 static __inline__ int
666 ie_buflen(sc, head)
667 struct ie_softc *sc;
668 int head;
669 {
670
671 return (SWAP(sc->rbuffs[head]->ie_rbd_actual)
672 & (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
673 }
674
675
676 static __inline__ int
677 ie_packet_len(sc)
678 struct ie_softc *sc;
679 {
680 int i;
681 int head = sc->rbhead;
682 int acc = 0;
683 int oldhead = head;
684
685 do {
686 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
687 i = SWAP(sc->rbuffs[head]->ie_rbd_actual);
688 if ((i & IE_RBD_USED) == 0) {
689 #ifdef I82586_DEBUG
690 print_rbd(sc->rbuffs[head]);
691 #endif
692 log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
693 sc->sc_dev.dv_xname, sc->rbhead);
694 i82586_reset(sc, 1);
695 return -1;
696 }
697
698 i = (i & IE_RBD_LAST) != 0;
699
700 acc += ie_buflen(sc, head);
701 head = (head + 1) % sc->nrxbuf;
702 if (oldhead == head) {
703 printf("ie: packet len: looping: acc = %d (head=%d)\n",
704 acc, head);
705 i82586_reset(sc, 1);
706 return -1;
707 }
708 } while (!i);
709
710 return acc;
711 }
712
713 /*
714 * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
715 * command to the chip to be executed.
716 */
717 static __inline__ void
718 iexmit(sc)
719 struct ie_softc *sc;
720 {
721 int cur, prev;
722
723 cur = sc->xctail;
724
725 #ifdef I82586_DEBUG
726 if (sc->sc_debug & IED_XMIT)
727 printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname, cur);
728 #endif
729
730 sc->xmit_buffs[cur]->ie_xmit_flags |= SWAP(IE_XMIT_LAST);
731 sc->xmit_buffs[cur]->ie_xmit_next = SWAP(0xffff);
732 ST_24(sc->xmit_buffs[cur]->ie_xmit_buf,
733 MK_24(sc->sc_iobase, sc->xmit_cbuffs[cur]));
734
735 sc->xmit_cmds[cur]->ie_xmit_desc =
736 MK_16(sc->sc_maddr, sc->xmit_buffs[cur]);
737
738 sc->xmit_cmds[cur]->ie_xmit_status = SWAP(0);
739
740 if (sc->do_xmitnopchain) {
741 /* Gate this XMIT to following NOP */
742 sc->xmit_cmds[cur]->com.ie_cmd_link =
743 MK_16(sc->sc_maddr, sc->nop_cmds[cur]);
744 sc->xmit_cmds[cur]->com.ie_cmd_cmd =
745 SWAP(IE_CMD_XMIT | IE_CMD_INTR);
746
747 /* Loopback at following NOP */
748 sc->nop_cmds[cur]->ie_cmd_status = SWAP(0);
749 sc->nop_cmds[cur]->ie_cmd_link =
750 MK_16(sc->sc_maddr, sc->nop_cmds[cur]);
751
752 /* Gate preceding NOP to this XMIT command */
753 prev = (cur + NTXBUF - 1) % NTXBUF;
754 sc->nop_cmds[prev]->ie_cmd_status = SWAP(0);
755 sc->nop_cmds[prev]->ie_cmd_link =
756 MK_16(sc->sc_maddr, sc->xmit_cmds[cur]);
757 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_WRITE);
758 if ((SWAP(sc->scb->ie_status) & IE_CU_ACTIVE) == 0) {
759 printf("iexmit: CU not active\n");
760 i82586_start_transceiver(sc);
761 }
762 } else {
763 sc->xmit_cmds[cur]->com.ie_cmd_link = SWAP(0xffff);
764 sc->xmit_cmds[cur]->com.ie_cmd_cmd =
765 SWAP(IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST);
766
767 sc->scb->ie_command_list =
768 MK_16(sc->sc_maddr, sc->xmit_cmds[cur]);
769
770 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_WRITE);
771 if (command_and_wait(sc, IE_CU_START, 0, 0))
772 printf("%s: iexmit: start xmit command timed out\n",
773 sc->sc_dev.dv_xname);
774 }
775
776 sc->sc_ethercom.ec_if.if_timer = 5;
777 }
778
779 /*
780 * Read data off the interface, and turn it into an mbuf chain.
781 *
782 * This code is DRAMATICALLY different from the previous version; this
783 * version tries to allocate the entire mbuf chain up front, given the
784 * length of the data available. This enables us to allocate mbuf
785 * clusters in many situations where before we would have had a long
786 * chain of partially-full mbufs. This should help to speed up the
787 * operation considerably. (Provided that it works, of course.)
788 */
789 struct mbuf *
790 ieget(sc, ehp, to_bpf)
791 struct ie_softc *sc;
792 struct ether_header *ehp;
793 int *to_bpf;
794 {
795 struct mbuf *top, **mp, *m;
796 int len, totlen, resid;
797 int thisrboff, thismboff;
798 int head;
799
800 totlen = ie_packet_len(sc);
801 if (totlen <= 0)
802 return 0;
803
804 head = sc->rbhead;
805
806 /*
807 * Snarf the Ethernet header.
808 */
809 (sc->memcopy)((caddr_t)sc->cbuffs[head], (caddr_t)ehp, sizeof *ehp);
810
811 /*
812 * As quickly as possible, check if this packet is for us.
813 * If not, don't waste a single cycle copying the rest of the
814 * packet in.
815 * This is only a consideration when FILTER is defined; i.e., when
816 * we are either running BPF or doing multicasting.
817 */
818 if (!check_eh(sc, ehp, to_bpf)) {
819 /* just this case, it's not an error */
820 sc->sc_ethercom.ec_if.if_ierrors--;
821 return 0;
822 }
823
824 resid = totlen -= (thisrboff = sizeof *ehp);
825
826 MGETHDR(m, M_DONTWAIT, MT_DATA);
827 if (m == 0)
828 return 0;
829 m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
830 m->m_pkthdr.len = totlen;
831 len = MHLEN;
832 top = 0;
833 mp = ⊤
834
835 /*
836 * This loop goes through and allocates mbufs for all the data we will
837 * be copying in. It does not actually do the copying yet.
838 */
839 while (totlen > 0) {
840 if (top) {
841 MGET(m, M_DONTWAIT, MT_DATA);
842 if (m == 0) {
843 m_freem(top);
844 return 0;
845 }
846 len = MLEN;
847 }
848 if (totlen >= MINCLSIZE) {
849 MCLGET(m, M_DONTWAIT);
850 if ((m->m_flags & M_EXT) == 0) {
851 m_freem(top);
852 return 0;
853 }
854 len = MCLBYTES;
855 }
856 m->m_len = len = min(totlen, len);
857 totlen -= len;
858 *mp = m;
859 mp = &m->m_next;
860 }
861
862 m = top;
863 thismboff = 0;
864
865 /*
866 * Now we take the mbuf chain (hopefully only one mbuf most of the
867 * time) and stuff the data into it. There are no possible failures at
868 * or after this point.
869 */
870 while (resid > 0) {
871 int thisrblen = ie_buflen(sc, head) - thisrboff,
872 thismblen = m->m_len - thismboff;
873 len = min(thisrblen, thismblen);
874
875 (sc->memcopy)((caddr_t)(sc->cbuffs[head] + thisrboff),
876 mtod(m, caddr_t) + thismboff, (u_int)len);
877 resid -= len;
878
879 if (len == thismblen) {
880 m = m->m_next;
881 thismboff = 0;
882 } else
883 thismboff += len;
884
885 if (len == thisrblen) {
886 head = (head + 1) % sc->nrxbuf;
887 thisrboff = 0;
888 } else
889 thisrboff += len;
890 }
891
892 /*
893 * Unless something changed strangely while we were doing the copy, we
894 * have now copied everything in from the shared memory.
895 * This means that we are done.
896 */
897 return top;
898 }
899
900 /*
901 * Read frame NUM from unit UNIT (pre-cached as IE).
902 *
903 * This routine reads the RFD at NUM, and copies in the buffers from the list
904 * of RBD, then rotates the RBD and RFD lists so that the receiver doesn't
905 * start complaining. Trailers are DROPPED---there's no point in wasting time
906 * on confusing code to deal with them. Hopefully, this machine will never ARP
907 * for trailers anyway.
908 */
909 static void
910 ie_readframe(sc, num)
911 struct ie_softc *sc;
912 int num; /* frame number to read */
913 {
914 int status;
915 struct mbuf *m = 0;
916 struct ether_header eh;
917 #if NBPFILTER > 0
918 int bpf_gets_it = 0;
919 #endif
920
921 status = SWAP(sc->rframes[num]->ie_fd_status);
922
923 /* Immediately advance the RFD list, since we have copied ours now. */
924 sc->rframes[num]->ie_fd_status = SWAP(0);
925 sc->rframes[num]->ie_fd_last |= SWAP(IE_FD_LAST);
926 sc->rframes[sc->rftail]->ie_fd_last &= ~SWAP(IE_FD_LAST);
927 sc->rftail = (sc->rftail + 1) % sc->nframes;
928 sc->rfhead = (sc->rfhead + 1) % sc->nframes;
929
930 if (status & IE_FD_OK) {
931 #if NBPFILTER > 0
932 m = ieget(sc, &eh, &bpf_gets_it);
933 #else
934 m = ieget(sc, &eh, 0);
935 #endif
936 ie_drop_packet_buffer(sc);
937 }
938 if (m == 0) {
939 sc->sc_ethercom.ec_if.if_ierrors++;
940 return;
941 }
942
943 #ifdef I82586_DEBUG
944 if (sc->sc_debug & IED_READFRAME)
945 printf("%s: frame from ether %s type 0x%x\n",
946 sc->sc_dev.dv_xname,
947 ether_sprintf(eh.ether_shost), (u_int)eh.ether_type);
948 #endif
949
950 #if NBPFILTER > 0
951 /*
952 * Check for a BPF filter; if so, hand it up.
953 * Note that we have to stick an extra mbuf up front, because bpf_mtap
954 * expects to have the ether header at the front.
955 * It doesn't matter that this results in an ill-formatted mbuf chain,
956 * since BPF just looks at the data. (It doesn't try to free the mbuf,
957 * tho' it will make a copy for tcpdump.)
958 */
959 if (bpf_gets_it) {
960 struct mbuf m0;
961 m0.m_len = sizeof eh;
962 m0.m_data = (caddr_t)&eh;
963 m0.m_next = m;
964
965 /* Pass it up. */
966 bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, &m0);
967
968 /*
969 * A signal passed up from the filtering code indicating that
970 * the packet is intended for BPF but not for the protocol
971 * machinery. We can save a few cycles by not handing it off
972 * to them.
973 */
974 if (bpf_gets_it == 2) {
975 m_freem(m);
976 return;
977 }
978 }
979 #endif /* NBPFILTER > 0 */
980
981 /*
982 * In here there used to be code to check destination addresses upon
983 * receipt of a packet. We have deleted that code, and replaced it
984 * with code to check the address much earlier in the cycle, before
985 * copying the data in; this saves us valuable cycles when operating
986 * as a multicast router or when using BPF.
987 */
988
989 /*
990 * Finally pass this packet up to higher layers.
991 */
992 ether_input(&sc->sc_ethercom.ec_if, &eh, m);
993 sc->sc_ethercom.ec_if.if_ipackets++;
994 }
995
996 static void
997 ie_drop_packet_buffer(sc)
998 struct ie_softc *sc;
999 {
1000 int i;
1001
1002 do {
1003 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_READ);
1004 i = SWAP(sc->rbuffs[sc->rbhead]->ie_rbd_actual);
1005 if ((i & IE_RBD_USED) == 0) {
1006 /*
1007 * This means we are somehow out of sync. So, we
1008 * reset the adapter.
1009 */
1010 #ifdef I82586_DEBUG
1011 print_rbd(sc->rbuffs[sc->rbhead]);
1012 #endif
1013 log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
1014 sc->sc_dev.dv_xname, sc->rbhead);
1015 i82586_reset(sc, 1);
1016 return;
1017 }
1018
1019 i = (i & IE_RBD_LAST) != 0;
1020
1021 sc->rbuffs[sc->rbhead]->ie_rbd_length |= SWAP(IE_RBD_LAST);
1022 sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
1023 sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
1024 sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~SWAP(IE_RBD_LAST);
1025 sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
1026 } while (!i);
1027 }
1028
1029
1030 /*
1031 * Start transmission on an interface.
1032 */
1033 void
1034 i82586_start(ifp)
1035 struct ifnet *ifp;
1036 {
1037 struct ie_softc *sc = ifp->if_softc;
1038 struct mbuf *m0, *m;
1039 u_char *buffer;
1040 u_short len;
1041 int s;
1042
1043 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1044 return;
1045
1046 for (;;) {
1047 if (sc->xmit_busy == NTXBUF) {
1048 ifp->if_flags |= IFF_OACTIVE;
1049 break;
1050 }
1051
1052 IF_DEQUEUE(&ifp->if_snd, m0);
1053 if (m0 == 0)
1054 break;
1055
1056 /* We need to use m->m_pkthdr.len, so require the header */
1057 if ((m0->m_flags & M_PKTHDR) == 0)
1058 panic("i82586_start: no header mbuf");
1059
1060 #if NBPFILTER > 0
1061 /* Tap off here if there is a BPF listener. */
1062 if (ifp->if_bpf)
1063 bpf_mtap(ifp->if_bpf, m0);
1064 #endif
1065
1066 #ifdef I82586_DEBUG
1067 if (sc->sc_debug & IED_ENQ)
1068 printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
1069 sc->xchead);
1070 #endif
1071
1072 if (m0->m_pkthdr.len > IE_TBUF_SIZE)
1073 printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
1074
1075 buffer = sc->xmit_cbuffs[sc->xchead];
1076 for (m = m0; m != 0; m = m->m_next) {
1077 (sc->memcopy)(mtod(m, caddr_t), buffer, m->m_len);
1078 buffer += m->m_len;
1079 }
1080
1081 len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
1082 m_freem(m0);
1083
1084 sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
1085
1086 sc->xchead = (sc->xchead + 1) % NTXBUF;
1087
1088 s = splnet();
1089 /* Start the first packet transmitting. */
1090 if (sc->xmit_busy == 0)
1091 iexmit(sc);
1092
1093 sc->xmit_busy++;
1094 splx(s);
1095 }
1096 }
1097
1098 /*
1099 * set up IE's ram space
1100 */
1101 int
1102 i82586_setupram(sc)
1103 struct ie_softc *sc;
1104 {
1105 volatile struct ie_sys_conf_ptr *scp;
1106 volatile struct ie_int_sys_conf_ptr *iscp;
1107 volatile struct ie_sys_ctl_block *scb;
1108 int s;
1109
1110 s = splnet();
1111
1112 scp = sc->scp;
1113 (sc->memzero)((char *) scp, sizeof *scp);
1114
1115 iscp = sc->iscp;
1116 (sc->memzero)((char *) iscp, sizeof *iscp);
1117
1118 scb = sc->scb;
1119 (sc->memzero)((char *) scb, sizeof *scb);
1120
1121 scp->ie_bus_use = 0; /* 16-bit */
1122 ST_24(scp->ie_iscp_ptr, MK_24(sc->sc_iobase, iscp));
1123
1124 iscp->ie_busy = 1; /* ie_busy == char */
1125 iscp->ie_scb_offset = MK_16(sc->sc_maddr, scb);
1126 ST_24(iscp->ie_base, MK_24(sc->sc_iobase, sc->sc_maddr));
1127
1128 if (sc->hwreset)
1129 (sc->hwreset)(sc, CHIP_PROBE);
1130
1131 (sc->chan_attn) (sc);
1132
1133 delay(100); /* wait a while... */
1134
1135 if (iscp->ie_busy) {
1136 printf("%s: ISCP still busy in setupram!\n", sc->sc_dev.dv_xname);
1137 splx(s);
1138 return 0;
1139 }
1140 /*
1141 * Acknowledge any interrupts we may have caused...
1142 */
1143 ie_ack(sc, IE_ST_WHENCE);
1144 splx(s);
1145
1146 return 1;
1147 }
1148
1149 void
1150 i82586_reset(sc, verbose)
1151 struct ie_softc *sc;
1152 int verbose;
1153 {
1154 int s = splnet();
1155
1156 if (verbose)
1157 printf("%s: reset\n", sc->sc_dev.dv_xname);
1158
1159 /* Clear OACTIVE in case we're called from watchdog (frozen xmit). */
1160 sc->sc_ethercom.ec_if.if_timer = 0;
1161 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1162
1163 iestop(sc);
1164
1165 /*
1166 * Stop i82586 dead in its tracks.
1167 */
1168 if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1169 printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
1170
1171 if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1172 printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
1173
1174 /*
1175 * This can really slow down the i82586_reset() on some cards, but it's
1176 * necessary to unwedge other ones (eg, the Sun VME ones) from certain
1177 * lockups.
1178 */
1179 if (sc->hwreset)
1180 (sc->hwreset)(sc, CARD_RESET);
1181
1182 ie_ack(sc, IE_ST_WHENCE);
1183
1184 #ifdef notdef
1185 if (!check_ie_present(sc, sc->sc_maddr, sc->sc_msize))
1186 panic("ie disappeared!\n");
1187 #endif
1188
1189 if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0)
1190 i82586_init(sc);
1191
1192 splx(s);
1193 }
1194
1195 /*
1196 * Send a command to the controller and wait for it to either complete
1197 * or be accepted, depending on the command. If the command pointer
1198 * is null, then pretend that the command is not an action command.
1199 * If the command pointer is not null, and the command is an action
1200 * command, wait for
1201 * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1202 * to become true.
1203 */
1204 static int
1205 command_and_wait(sc, cmd, pcmd, mask)
1206 struct ie_softc *sc;
1207 int cmd; /* native byte-order */
1208 volatile void *pcmd;
1209 int mask; /* native byte-order */
1210 {
1211 volatile struct ie_cmd_common *cc = pcmd;
1212 volatile struct ie_sys_ctl_block *scb = sc->scb;
1213 int i;
1214
1215 scb->ie_command = (u_short)SWAP(cmd);
1216 bus_space_barrier(sc->bt, sc->bh, 0, 0, BUS_SPACE_BARRIER_WRITE);
1217 (sc->chan_attn)(sc);
1218
1219 if (IE_ACTION_COMMAND(cmd) && pcmd) {
1220 /*
1221 * According to the packet driver, the minimum timeout should
1222 * be .369 seconds, which we round up to .4.
1223 */
1224
1225 /*
1226 * Now spin-lock waiting for status. This is not a very nice
1227 * thing to do, but I haven't figured out how, or indeed if, we
1228 * can put the process waiting for action to sleep. (We may
1229 * be getting called through some other timeout running in the
1230 * kernel.)
1231 */
1232 for (i = 0; i < 369000; i++) {
1233 bus_space_barrier(sc->bt, sc->bh, 0, 0,
1234 BUS_SPACE_BARRIER_READ);
1235 if ((SWAP(cc->ie_cmd_status) & mask))
1236 return (0);
1237 delay(1);
1238 }
1239
1240 } else {
1241 /*
1242 * Otherwise, just wait for the command to be accepted.
1243 */
1244
1245 /* XXX spin lock; wait at most 0.9 seconds */
1246 for (i = 0; i < 900000; i++) {
1247 bus_space_barrier(sc->bt, sc->bh, 0, 0,
1248 BUS_SPACE_BARRIER_READ);
1249 if (scb->ie_command == 0)
1250 return (0);
1251 delay(1);
1252 }
1253 }
1254
1255 /* Timeout */
1256 return (1);
1257 }
1258
1259 /*
1260 * Run the time-domain reflectometer.
1261 */
1262 static void
1263 run_tdr(sc, cmd)
1264 struct ie_softc *sc;
1265 struct ie_tdr_cmd *cmd;
1266 {
1267 int result;
1268
1269 cmd->com.ie_cmd_status = SWAP(0);
1270 cmd->com.ie_cmd_cmd = SWAP(IE_CMD_TDR | IE_CMD_LAST);
1271 cmd->com.ie_cmd_link = SWAP(0xffff);
1272
1273 sc->scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
1274 cmd->ie_tdr_time = SWAP(0);
1275
1276 if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1277 (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0)
1278 result = 0x10000; /* XXX */
1279 else
1280 result = SWAP(cmd->ie_tdr_time);
1281
1282 ie_ack(sc, IE_ST_WHENCE);
1283
1284 if (result & IE_TDR_SUCCESS)
1285 return;
1286
1287 if (result & 0x10000)
1288 printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
1289 else if (result & IE_TDR_XCVR)
1290 printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
1291 else if (result & IE_TDR_OPEN)
1292 printf("%s: TDR detected an open %d clocks away\n",
1293 sc->sc_dev.dv_xname, result & IE_TDR_TIME);
1294 else if (result & IE_TDR_SHORT)
1295 printf("%s: TDR detected a short %d clocks away\n",
1296 sc->sc_dev.dv_xname, result & IE_TDR_TIME);
1297 else
1298 printf("%s: TDR returned unknown status 0x%x\n",
1299 sc->sc_dev.dv_xname, result);
1300 }
1301
1302 #ifdef notdef
1303 /* ALIGN works on 8 byte boundaries.... but 4 byte boundaries are ok for sun */
1304 #define _ALLOC(p, n) (bzero(p, n), p += n, p - n)
1305 #define ALLOC(p, n) _ALLOC(p, ALIGN(n)) /* XXX convert to this? */
1306 #endif
1307
1308 /*
1309 * setup_bufs: set up the buffers
1310 *
1311 * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1312 * this is to be used for the buffers. the chip indexs its control data
1313 * structures with 16 bit offsets, and it indexes actual buffers with
1314 * 24 bit addresses. so we should allocate control buffers first so that
1315 * we don't overflow the 16 bit offset field. The number of transmit
1316 * buffers is fixed at compile time.
1317 *
1318 * note: this function was written to be easy to understand, rather than
1319 * highly efficient (it isn't in the critical path).
1320 */
1321 static void
1322 setup_bufs(sc)
1323 struct ie_softc *sc;
1324 {
1325 caddr_t ptr = sc->buf_area; /* memory pool */
1326 int n, r;
1327
1328 /*
1329 * step 0: zero memory and figure out how many recv buffers and
1330 * frames we can have.
1331 */
1332 (sc->memzero)(ptr, sc->buf_area_sz);
1333 ptr = (sc->align)(ptr); /* set alignment and stick with it */
1334
1335 n = (int)(sc->align)((caddr_t) sizeof(struct ie_cmd_common)) +
1336 (int)(sc->align)((caddr_t) sizeof(struct ie_xmit_cmd)) +
1337 (int)(sc->align)((caddr_t) sizeof(struct ie_xmit_buf)) + IE_TBUF_SIZE;
1338 n *= NTXBUF; /* n = total size of xmit area */
1339
1340 n = sc->buf_area_sz - n;/* n = free space for recv stuff */
1341
1342 r = (int)(sc->align)((caddr_t) sizeof(struct ie_recv_frame_desc)) +
1343 (((int)(sc->align)((caddr_t) sizeof(struct ie_recv_buf_desc)) +
1344 IE_RBUF_SIZE) * B_PER_F);
1345
1346 /* r = size of one R frame */
1347
1348 sc->nframes = n / r;
1349 if (sc->nframes <= 0)
1350 panic("ie: bogus buffer calc\n");
1351 if (sc->nframes > MAXFRAMES)
1352 sc->nframes = MAXFRAMES;
1353
1354 sc->nrxbuf = sc->nframes * B_PER_F;
1355
1356 #ifdef I82586_DEBUG
1357 printf("%s: %d frames %d bufs\n", sc->sc_dev.dv_xname, sc->nframes,
1358 sc->nrxbuf);
1359 #endif
1360
1361 /*
1362 * step 1a: lay out and zero frame data structures for transmit and recv
1363 */
1364 for (n = 0; n < NTXBUF; n++) {
1365 sc->nop_cmds[n] = (volatile struct ie_cmd_common *) ptr;
1366 ptr = (sc->align)(ptr + sizeof(struct ie_cmd_common));
1367 }
1368 for (n = 0; n < NTXBUF; n++) {
1369 sc->xmit_cmds[n] = (volatile struct ie_xmit_cmd *) ptr;
1370 ptr = (sc->align)(ptr + sizeof(struct ie_xmit_cmd));
1371 }
1372
1373 for (n = 0; n < sc->nframes; n++) {
1374 sc->rframes[n] = (volatile struct ie_recv_frame_desc *) ptr;
1375 ptr = (sc->align)(ptr + sizeof(struct ie_recv_frame_desc));
1376 }
1377
1378 /*
1379 * step 1b: link together the recv frames and set EOL on last one
1380 */
1381 for (n = 0; n < sc->nframes; n++) {
1382 sc->rframes[n]->ie_fd_next =
1383 MK_16(sc->sc_maddr, sc->rframes[(n + 1) % sc->nframes]);
1384 }
1385 sc->rframes[sc->nframes - 1]->ie_fd_last |= SWAP(IE_FD_LAST);
1386
1387 /*
1388 * step 1c: link the xmit no-op frames to themselves
1389 */
1390 for (n = 0; n < NTXBUF; n++) {
1391 sc->nop_cmds[n]->ie_cmd_status = SWAP(0);
1392 sc->nop_cmds[n]->ie_cmd_cmd = SWAP(IE_CMD_NOP);
1393 sc->nop_cmds[n]->ie_cmd_link =
1394 MK_16(sc->sc_maddr, sc->nop_cmds[n]);
1395 }
1396
1397 /*
1398 * step 2a: lay out and zero frame buffer structures for xmit and recv
1399 */
1400 for (n = 0; n < NTXBUF; n++) {
1401 sc->xmit_buffs[n] = (volatile struct ie_xmit_buf *) ptr;
1402 ptr = (sc->align)(ptr + sizeof(struct ie_xmit_buf));
1403 }
1404
1405 for (n = 0; n < sc->nrxbuf; n++) {
1406 sc->rbuffs[n] = (volatile struct ie_recv_buf_desc *) ptr;
1407 ptr = (sc->align)(ptr + sizeof(struct ie_recv_buf_desc));
1408 }
1409
1410 /*
1411 * step 2b: link together recv bufs and set EOL on last one
1412 */
1413 for (n = 0; n < sc->nrxbuf; n++) {
1414 sc->rbuffs[n]->ie_rbd_next =
1415 MK_16(sc->sc_maddr, sc->rbuffs[(n + 1) % sc->nrxbuf]);
1416 }
1417 sc->rbuffs[sc->nrxbuf - 1]->ie_rbd_length |= SWAP(IE_RBD_LAST);
1418
1419 /*
1420 * step 3: allocate the actual data buffers for xmit and recv
1421 * recv buffer gets linked into recv_buf_desc list here
1422 */
1423 for (n = 0; n < NTXBUF; n++) {
1424 sc->xmit_cbuffs[n] = (u_char *) ptr;
1425 ptr = (sc->align)(ptr + IE_TBUF_SIZE);
1426 }
1427
1428 /* Pointers to last packet sent and next available transmit buffer. */
1429 sc->xchead = sc->xctail = 0;
1430
1431 /* Clear transmit-busy flag and set number of free transmit buffers. */
1432 sc->xmit_busy = 0;
1433
1434 for (n = 0; n < sc->nrxbuf; n++) {
1435 sc->cbuffs[n] = (char *) ptr; /* XXX why char vs uchar? */
1436 sc->rbuffs[n]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
1437 ST_24(sc->rbuffs[n]->ie_rbd_buffer, MK_24(sc->sc_iobase, ptr));
1438 ptr = (sc->align)(ptr + IE_RBUF_SIZE);
1439 }
1440
1441 /*
1442 * step 4: set the head and tail pointers on receive to keep track of
1443 * the order in which RFDs and RBDs are used. link in recv frames
1444 * and buffer into the scb.
1445 */
1446
1447 sc->rfhead = 0;
1448 sc->rftail = sc->nframes - 1;
1449 sc->rbhead = 0;
1450 sc->rbtail = sc->nrxbuf - 1;
1451
1452 #ifdef I82586_DEBUG
1453 printf("%s: reserved %d bytes\n", sc->sc_dev.dv_xname, ptr - sc->buf_area);
1454 #endif
1455 }
1456
1457 /*
1458 * Run the multicast setup command.
1459 * Called at splnet().
1460 */
1461 static int
1462 mc_setup(sc, ptr)
1463 struct ie_softc *sc;
1464 void *ptr;
1465 {
1466 volatile struct ie_mcast_cmd *cmd = ptr;
1467
1468 cmd->com.ie_cmd_status = SWAP(0);
1469 cmd->com.ie_cmd_cmd = SWAP(IE_CMD_MCAST | IE_CMD_LAST);
1470 cmd->com.ie_cmd_link = SWAP(0xffff);
1471
1472 (sc->memcopy)((caddr_t)sc->mcast_addrs, (caddr_t)cmd->ie_mcast_addrs,
1473 sc->mcast_count * sizeof *sc->mcast_addrs);
1474
1475 cmd->ie_mcast_bytes =
1476 SWAP(sc->mcast_count * ETHER_ADDR_LEN); /* grrr... */
1477
1478 sc->scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
1479 if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1480 (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
1481 printf("%s: multicast address setup command failed\n",
1482 sc->sc_dev.dv_xname);
1483 return 0;
1484 }
1485
1486 i82586_start_transceiver(sc);
1487 return 1;
1488 }
1489
1490 /*
1491 * This routine takes the environment generated by check_ie_present() and adds
1492 * to it all the other structures we need to operate the adapter. This
1493 * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
1494 * the receiver unit, and clearing interrupts.
1495 *
1496 * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1497 */
1498 int
1499 i82586_init(sc)
1500 struct ie_softc *sc;
1501 {
1502 volatile struct ie_sys_ctl_block *scb = sc->scb;
1503 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1504 void *ptr;
1505
1506 ptr = sc->buf_area;
1507
1508 /*
1509 * Send the configure command first.
1510 */
1511 {
1512 volatile struct ie_config_cmd *cmd = ptr;
1513
1514 scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
1515 cmd->com.ie_cmd_status = SWAP(0);
1516 cmd->com.ie_cmd_cmd = SWAP(IE_CMD_CONFIG | IE_CMD_LAST);
1517 cmd->com.ie_cmd_link = SWAP(0xffff);
1518
1519 ie_setup_config(cmd, sc->promisc, 0);
1520
1521 if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1522 (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
1523 printf("%s: configure command failed\n",
1524 sc->sc_dev.dv_xname);
1525 return 0;
1526 }
1527 }
1528
1529 /*
1530 * Now send the Individual Address Setup command.
1531 */
1532 {
1533 volatile struct ie_iasetup_cmd *cmd = ptr;
1534
1535 scb->ie_command_list = MK_16(sc->sc_maddr, cmd);
1536 cmd->com.ie_cmd_status = SWAP(0);
1537 cmd->com.ie_cmd_cmd = SWAP(IE_CMD_IASETUP | IE_CMD_LAST);
1538 cmd->com.ie_cmd_link = SWAP(0xffff);
1539
1540 (sc->memcopy)(LLADDR(ifp->if_sadl),
1541 (caddr_t)&cmd->ie_address, sizeof cmd->ie_address);
1542
1543 if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1544 (SWAP(cmd->com.ie_cmd_status) & IE_STAT_OK) == 0) {
1545 printf("%s: individual address setup command failed\n",
1546 sc->sc_dev.dv_xname);
1547 return 0;
1548 }
1549 }
1550
1551 /*
1552 * Now run the time-domain reflectometer.
1553 */
1554 run_tdr(sc, ptr);
1555
1556 /*
1557 * Acknowledge any interrupts we have generated thus far.
1558 */
1559 ie_ack(sc, IE_ST_WHENCE);
1560
1561 /*
1562 * Set up the transmit and recv buffers.
1563 */
1564 setup_bufs(sc);
1565
1566 ie_ack(sc, IE_ST_WHENCE);
1567
1568 if (sc->hwinit)
1569 (sc->hwinit)(sc);
1570
1571 ifp->if_flags |= IFF_RUNNING;
1572 ifp->if_flags &= ~IFF_OACTIVE;
1573
1574 if (NTXBUF < 2)
1575 sc->do_xmitnopchain = 0;
1576
1577 i82586_start_transceiver(sc);
1578 return 0;
1579 }
1580
1581 static void
1582 i82586_start_transceiver(sc)
1583 struct ie_softc *sc;
1584 {
1585 sc->rframes[0]->ie_fd_buf_desc = MK_16(sc->sc_maddr, sc->rbuffs[0]);
1586 sc->scb->ie_recv_list = MK_16(sc->sc_maddr, sc->rframes[0]);
1587
1588 if (sc->do_xmitnopchain) {
1589 /* Stop transmit command chain */
1590 if (command_and_wait(sc, IE_CU_STOP|IE_RU_DISABLE, 0, 0))
1591 printf("%s: CU/RU stop command timed out\n",
1592 sc->sc_dev.dv_xname);
1593
1594 /* Start the receiver & transmitter chain */
1595 sc->scb->ie_command_list =
1596 MK_16(sc->sc_maddr,
1597 sc->nop_cmds[(sc->xctail + NTXBUF - 1) % NTXBUF]);
1598 if (command_and_wait(sc, IE_CU_START|IE_RU_START, 0, 0))
1599 printf("%s: CU/RU command timed out\n",
1600 sc->sc_dev.dv_xname);
1601 } else {
1602 if (command_and_wait(sc, IE_RU_START, 0, 0))
1603 printf("%s: RU command timed out\n",
1604 sc->sc_dev.dv_xname);
1605 }
1606 }
1607
1608 static void
1609 iestop(sc)
1610 struct ie_softc *sc;
1611 {
1612
1613 if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1614 printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
1615 }
1616
1617 int
1618 i82586_ioctl(ifp, cmd, data)
1619 register struct ifnet *ifp;
1620 u_long cmd;
1621 caddr_t data;
1622 {
1623 struct ie_softc *sc = ifp->if_softc;
1624 struct ifaddr *ifa = (struct ifaddr *)data;
1625 struct ifreq *ifr = (struct ifreq *)data;
1626 int s, error = 0;
1627
1628 s = splnet();
1629
1630 switch(cmd) {
1631
1632 case SIOCSIFADDR:
1633 ifp->if_flags |= IFF_UP;
1634
1635 switch(ifa->ifa_addr->sa_family) {
1636 #ifdef INET
1637 case AF_INET:
1638 i82586_init(sc);
1639 arp_ifinit(ifp, ifa);
1640 break;
1641 #endif
1642 #ifdef NS
1643 /* XXX - This code is probably wrong. */
1644 case AF_NS:
1645 {
1646 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1647
1648 if (ns_nullhost(*ina))
1649 ina->x_host =
1650 *(union ns_host *)LLADDR(ifp->if_sadl);
1651 else
1652 bcopy(ina->x_host.c_host,
1653 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1654 /* Set new address. */
1655 i82586_init(sc);
1656 break;
1657 }
1658 #endif /* NS */
1659 default:
1660 i82586_init(sc);
1661 break;
1662 }
1663 break;
1664
1665 case SIOCSIFFLAGS:
1666 sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1667 if ((ifp->if_flags & IFF_UP) == 0 &&
1668 (ifp->if_flags & IFF_RUNNING) != 0) {
1669 /*
1670 * If interface is marked down and it is running, then
1671 * stop it.
1672 */
1673 iestop(sc);
1674 ifp->if_flags &= ~IFF_RUNNING;
1675 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1676 (ifp->if_flags & IFF_RUNNING) == 0) {
1677 /*
1678 * If interface is marked up and it is stopped, then
1679 * start it.
1680 */
1681 i82586_init(sc);
1682 } else {
1683 /*
1684 * Reset the interface to pick up changes in any other
1685 * flags that affect hardware registers.
1686 */
1687 iestop(sc);
1688 i82586_init(sc);
1689 }
1690 #ifdef I82586_DEBUG
1691 if (ifp->if_flags & IFF_DEBUG)
1692 sc->sc_debug = IED_ALL;
1693 else
1694 sc->sc_debug = 0;
1695 #endif
1696 break;
1697
1698 case SIOCADDMULTI:
1699 case SIOCDELMULTI:
1700 error = (cmd == SIOCADDMULTI) ?
1701 ether_addmulti(ifr, &sc->sc_ethercom):
1702 ether_delmulti(ifr, &sc->sc_ethercom);
1703
1704 if (error == ENETRESET) {
1705 /*
1706 * Multicast list has changed; set the hardware filter
1707 * accordingly.
1708 */
1709 mc_reset(sc);
1710 error = 0;
1711 }
1712 break;
1713
1714 case SIOCGIFMEDIA:
1715 case SIOCSIFMEDIA:
1716 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1717 break;
1718
1719 default:
1720 error = EINVAL;
1721 }
1722 splx(s);
1723 return error;
1724 }
1725
1726 static void
1727 mc_reset(sc)
1728 struct ie_softc *sc;
1729 {
1730 struct ether_multi *enm;
1731 struct ether_multistep step;
1732
1733 /*
1734 * Step through the list of addresses.
1735 */
1736 sc->mcast_count = 0;
1737 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1738 while (enm) {
1739 if (sc->mcast_count >= MAXMCAST ||
1740 bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
1741 sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
1742 i82586_ioctl(&sc->sc_ethercom.ec_if,
1743 SIOCSIFFLAGS, (void *)0);
1744 goto setflag;
1745 }
1746
1747 bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
1748 sc->mcast_count++;
1749 ETHER_NEXT_MULTI(step, enm);
1750 }
1751 setflag:
1752 sc->want_mcsetup = 1;
1753 }
1754
1755 /*
1756 * Media change callback.
1757 */
1758 int
1759 i82586_mediachange(ifp)
1760 struct ifnet *ifp;
1761 {
1762 struct ie_softc *sc = ifp->if_softc;
1763
1764 if (sc->sc_mediachange)
1765 return ((*sc->sc_mediachange)(sc));
1766 return (EINVAL);
1767 }
1768
1769 /*
1770 * Media status callback.
1771 */
1772 void
1773 i82586_mediastatus(ifp, ifmr)
1774 struct ifnet *ifp;
1775 struct ifmediareq *ifmr;
1776 {
1777 struct ie_softc *sc = ifp->if_softc;
1778
1779 if (sc->sc_mediastatus)
1780 (*sc->sc_mediastatus)(sc, ifmr);
1781 }
1782
1783 #ifdef I82586_DEBUG
1784 void
1785 print_rbd(rbd)
1786 volatile struct ie_recv_buf_desc *rbd;
1787 {
1788 u_long bufval;
1789
1790 bcopy((char *)&rbd->ie_rbd_buffer, &bufval, 4); /*XXX*/
1791
1792 printf("RBD at %08lx:\nactual %04x, next %04x, buffer %lx\n"
1793 "length %04x, mbz %04x\n", (u_long)rbd,
1794 SWAP(rbd->ie_rbd_actual),
1795 SWAP(rbd->ie_rbd_next),
1796 bufval,
1797 SWAP(rbd->ie_rbd_length),
1798 rbd->mbz);
1799 }
1800 #endif
1801