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