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