seeq8005.c revision 1.11 1 1.11 bjh21 /* $NetBSD: seeq8005.c,v 1.11 2001/03/24 20:38:41 bjh21 Exp $ */
2 1.1 bjh21
3 1.1 bjh21 /*
4 1.1 bjh21 * Copyright (c) 2000 Ben Harris
5 1.11 bjh21 * Copyright (c) 1995-1998 Mark Brinicombe
6 1.1 bjh21 * All rights reserved.
7 1.1 bjh21 *
8 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
9 1.1 bjh21 * modification, are permitted provided that the following conditions
10 1.1 bjh21 * are met:
11 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
12 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
13 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
15 1.1 bjh21 * documentation and/or other materials provided with the distribution.
16 1.1 bjh21 * 3. All advertising materials mentioning features or use of this software
17 1.1 bjh21 * must display the following acknowledgement:
18 1.11 bjh21 * This product includes software developed by Mark Brinicombe
19 1.11 bjh21 * for the NetBSD Project.
20 1.1 bjh21 * 4. The name of the company nor the name of the author may be used to
21 1.1 bjh21 * endorse or promote products derived from this software without specific
22 1.1 bjh21 * prior written permission.
23 1.1 bjh21 *
24 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 1.1 bjh21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 1.1 bjh21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 bjh21 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 1.1 bjh21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 1.1 bjh21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 1.1 bjh21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 bjh21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 bjh21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 bjh21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 bjh21 * SUCH DAMAGE.
35 1.1 bjh21 */
36 1.1 bjh21 /*
37 1.2 bjh21 * seeq8005.c - SEEQ 8005 device driver
38 1.2 bjh21 */
39 1.2 bjh21 /*
40 1.2 bjh21 * This driver currently supports the following chip:
41 1.2 bjh21 * SEEQ 8005 Advanced Ethernet Data Link Controller
42 1.2 bjh21 */
43 1.2 bjh21 /*
44 1.11 bjh21 * More information on the 8004 and 8005 AEDLC controllers can be found in
45 1.11 bjh21 * the SEEQ Technology Inc 1992 Data Comm Devices data book.
46 1.11 bjh21 *
47 1.11 bjh21 * This data book may no longer be available as these are rather old chips
48 1.11 bjh21 * (1991 - 1993)
49 1.11 bjh21 */
50 1.11 bjh21 /*
51 1.2 bjh21 * This driver is based on the arm32 ea(4) driver, hence the names of many
52 1.2 bjh21 * of the functions.
53 1.1 bjh21 */
54 1.1 bjh21 /*
55 1.1 bjh21 * Bugs/possible improvements:
56 1.1 bjh21 * - Does not currently support DMA
57 1.1 bjh21 * - Does not transmit multiple packets in one go
58 1.1 bjh21 * - Does not support 8-bit busses
59 1.1 bjh21 */
60 1.1 bjh21
61 1.1 bjh21 #include "opt_inet.h"
62 1.1 bjh21 #include "opt_ns.h"
63 1.1 bjh21
64 1.1 bjh21 #include <sys/types.h>
65 1.1 bjh21 #include <sys/param.h>
66 1.1 bjh21
67 1.11 bjh21 __RCSID("$NetBSD: seeq8005.c,v 1.11 2001/03/24 20:38:41 bjh21 Exp $");
68 1.1 bjh21
69 1.1 bjh21 #include <sys/systm.h>
70 1.1 bjh21 #include <sys/endian.h>
71 1.1 bjh21 #include <sys/errno.h>
72 1.1 bjh21 #include <sys/ioctl.h>
73 1.1 bjh21 #include <sys/mbuf.h>
74 1.1 bjh21 #include <sys/socket.h>
75 1.1 bjh21 #include <sys/syslog.h>
76 1.1 bjh21 #include <sys/device.h>
77 1.1 bjh21
78 1.1 bjh21 #include <net/if.h>
79 1.1 bjh21 #include <net/if_dl.h>
80 1.1 bjh21 #include <net/if_types.h>
81 1.1 bjh21 #include <net/if_ether.h>
82 1.11 bjh21 #include <net/if_media.h>
83 1.1 bjh21
84 1.1 bjh21 #ifdef INET
85 1.1 bjh21 #include <netinet/in.h>
86 1.1 bjh21 #include <netinet/in_systm.h>
87 1.1 bjh21 #include <netinet/in_var.h>
88 1.1 bjh21 #include <netinet/ip.h>
89 1.1 bjh21 #include <netinet/if_inarp.h>
90 1.1 bjh21 #endif
91 1.1 bjh21
92 1.1 bjh21 #ifdef NS
93 1.1 bjh21 #include <netns/ns.h>
94 1.1 bjh21 #include <netns/ns_if.h>
95 1.1 bjh21 #endif
96 1.1 bjh21
97 1.1 bjh21 #include "bpfilter.h"
98 1.1 bjh21 #if NBPFILTER > 0
99 1.1 bjh21 #include <net/bpf.h>
100 1.1 bjh21 #include <net/bpfdesc.h>
101 1.1 bjh21 #endif
102 1.1 bjh21
103 1.1 bjh21 #include <machine/bus.h>
104 1.1 bjh21 #include <machine/intr.h>
105 1.1 bjh21
106 1.1 bjh21 #include <dev/ic/seeq8005reg.h>
107 1.1 bjh21 #include <dev/ic/seeq8005var.h>
108 1.1 bjh21
109 1.10 bjh21 /*#define SEEQ_DEBUG*/
110 1.1 bjh21
111 1.1 bjh21 /* for debugging convenience */
112 1.10 bjh21 #ifdef SEEQ_DEBUG
113 1.11 bjh21 #define SEEQ_DEBUG_MISC 1
114 1.11 bjh21 #define SEEQ_DEBUG_TX 2
115 1.11 bjh21 #define SEEQ_DEBUG_RX 4
116 1.11 bjh21 #define SEEQ_DEBUG_PKT 8
117 1.11 bjh21 #define SEEQ_DEBUG_TXINT 16
118 1.11 bjh21 #define SEEQ_DEBUG_RXINT 32
119 1.11 bjh21 int seeq_debug = 0;
120 1.11 bjh21 #define DPRINTF(f, x) { if (seeq_debug & (f)) printf x; }
121 1.1 bjh21 #else
122 1.11 bjh21 #define DPRINTF(f, x)
123 1.1 bjh21 #endif
124 1.11 bjh21 #define dprintf(x) DPRINTF(SEEQ_DEBUG_MISC, x)
125 1.11 bjh21
126 1.11 bjh21 #define SEEQ_TX_BUFFER_SIZE 0x600 /* (> MAX_ETHER_LEN) */
127 1.1 bjh21
128 1.1 bjh21 /*
129 1.1 bjh21 * prototypes
130 1.1 bjh21 */
131 1.1 bjh21
132 1.5 bjh21 static int ea_init(struct ifnet *);
133 1.1 bjh21 static int ea_ioctl(struct ifnet *, u_long, caddr_t);
134 1.1 bjh21 static void ea_start(struct ifnet *);
135 1.1 bjh21 static void ea_watchdog(struct ifnet *);
136 1.1 bjh21 static void ea_chipreset(struct seeq8005_softc *);
137 1.1 bjh21 static void ea_ramtest(struct seeq8005_softc *);
138 1.1 bjh21 static int ea_stoptx(struct seeq8005_softc *);
139 1.1 bjh21 static int ea_stoprx(struct seeq8005_softc *);
140 1.5 bjh21 static void ea_stop(struct ifnet *, int);
141 1.1 bjh21 static void ea_await_fifo_empty(struct seeq8005_softc *);
142 1.1 bjh21 static void ea_await_fifo_full(struct seeq8005_softc *);
143 1.11 bjh21 static void ea_writebuf(struct seeq8005_softc *, u_char *, int, size_t);
144 1.11 bjh21 static void ea_readbuf(struct seeq8005_softc *, u_char *, int, size_t);
145 1.3 bjh21 static void ea_select_buffer(struct seeq8005_softc *, int);
146 1.5 bjh21 static void ea_set_address(struct seeq8005_softc *, int, const u_int8_t *);
147 1.11 bjh21 static void ea_read(struct seeq8005_softc *, int, int);
148 1.11 bjh21 static struct mbuf *ea_get(struct seeq8005_softc *, int, int, struct ifnet *);
149 1.11 bjh21 static void ea_getpackets(struct seeq8005_softc *);
150 1.1 bjh21 static void eatxpacket(struct seeq8005_softc *);
151 1.5 bjh21 static void ea_mc_reset(struct seeq8005_softc *);
152 1.11 bjh21 static void ea_mc_reset_8004(struct seeq8005_softc *);
153 1.11 bjh21 static void ea_mc_reset_8005(struct seeq8005_softc *);
154 1.11 bjh21 static int ea_mediachange(struct ifnet *);
155 1.11 bjh21 static void ea_mediastatus(struct ifnet *, struct ifmediareq *);
156 1.1 bjh21
157 1.1 bjh21
158 1.1 bjh21 /*
159 1.1 bjh21 * Attach chip.
160 1.1 bjh21 */
161 1.1 bjh21
162 1.1 bjh21 void
163 1.11 bjh21 seeq8005_attach(struct seeq8005_softc *sc, const u_int8_t *myaddr, int *media,
164 1.11 bjh21 int nmedia, int defmedia)
165 1.1 bjh21 {
166 1.1 bjh21 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
167 1.2 bjh21 u_int id;
168 1.2 bjh21
169 1.11 bjh21 KASSERT(myaddr != NULL);
170 1.2 bjh21 printf(" address %s", ether_sprintf(myaddr));
171 1.2 bjh21
172 1.3 bjh21 /* Stop the board. */
173 1.3 bjh21
174 1.3 bjh21 ea_chipreset(sc);
175 1.3 bjh21
176 1.2 bjh21 /* Get the product ID */
177 1.1 bjh21
178 1.10 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_PRODUCTID);
179 1.10 bjh21 id = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN);
180 1.2 bjh21
181 1.11 bjh21 switch (id & SEEQ_PRODUCTID_MASK) {
182 1.11 bjh21 case SEEQ_PRODUCTID_8004:
183 1.11 bjh21 sc->sc_variant = SEEQ_8004;
184 1.11 bjh21 break;
185 1.11 bjh21 default: /* XXX */
186 1.11 bjh21 sc->sc_variant = SEEQ_8005;
187 1.11 bjh21 break;
188 1.11 bjh21 }
189 1.11 bjh21
190 1.11 bjh21 switch (sc->sc_variant) {
191 1.11 bjh21 case SEEQ_8004:
192 1.11 bjh21 printf(", SEEQ80C04 rev %x\n",
193 1.11 bjh21 id & SEEQ_PRODUCTID_REV_MASK);
194 1.11 bjh21 break;
195 1.11 bjh21 case SEEQ_8005:
196 1.11 bjh21 if (id != 0xff)
197 1.11 bjh21 printf(", SEEQ8005 rev %x\n", id);
198 1.11 bjh21 else
199 1.11 bjh21 printf(", SEEQ8005\n");
200 1.11 bjh21 break;
201 1.11 bjh21 default:
202 1.11 bjh21 printf(", Unknown ethernet controller\n");
203 1.11 bjh21 return;
204 1.11 bjh21 }
205 1.11 bjh21
206 1.11 bjh21 /* Both the 8004 and 8005 are designed for 64K Buffer memory */
207 1.11 bjh21 sc->sc_buffersize = SEEQ_MAX_BUFFER_SIZE;
208 1.11 bjh21
209 1.11 bjh21 /*
210 1.11 bjh21 * Set up tx and rx buffers.
211 1.11 bjh21 *
212 1.11 bjh21 * We use approximately a third of the packet memory for TX
213 1.11 bjh21 * buffers and the rest for RX buffers
214 1.11 bjh21 */
215 1.11 bjh21 sc->sc_tx_bufs = sc->sc_buffersize / SEEQ_TX_BUFFER_SIZE / 3;
216 1.11 bjh21 sc->sc_tx_bufsize = sc->sc_tx_bufs * SEEQ_TX_BUFFER_SIZE;
217 1.11 bjh21 sc->sc_rx_bufsize = sc->sc_buffersize - sc->sc_tx_bufsize;
218 1.11 bjh21 sc->sc_enabled = 0;
219 1.11 bjh21
220 1.11 bjh21 /* Test the RAM */
221 1.11 bjh21 ea_ramtest(sc);
222 1.11 bjh21
223 1.11 bjh21 printf("%s: %dKB packet memory, txbuf=%dKB (%d buffers), rxbuf=%dKB",
224 1.11 bjh21 sc->sc_dev.dv_xname, sc->sc_buffersize >> 10,
225 1.11 bjh21 sc->sc_tx_bufsize >> 10, sc->sc_tx_bufs, sc->sc_rx_bufsize >> 10);
226 1.1 bjh21
227 1.1 bjh21 /* Initialise ifnet structure. */
228 1.1 bjh21
229 1.1 bjh21 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
230 1.1 bjh21 ifp->if_softc = sc;
231 1.1 bjh21 ifp->if_start = ea_start;
232 1.1 bjh21 ifp->if_ioctl = ea_ioctl;
233 1.5 bjh21 ifp->if_init = ea_init;
234 1.5 bjh21 ifp->if_stop = ea_stop;
235 1.1 bjh21 ifp->if_watchdog = ea_watchdog;
236 1.5 bjh21 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
237 1.11 bjh21 if (sc->sc_variant == SEEQ_8004)
238 1.11 bjh21 ifp->if_flags |= IFF_SIMPLEX;
239 1.7 thorpej IFQ_SET_READY(&ifp->if_snd);
240 1.1 bjh21
241 1.11 bjh21 /* Initialize media goo. */
242 1.11 bjh21 ifmedia_init(&sc->sc_media, 0, ea_mediachange, ea_mediastatus);
243 1.11 bjh21 if (media != NULL) {
244 1.11 bjh21 int i;
245 1.11 bjh21
246 1.11 bjh21 for (i = 0; i < nmedia; i++)
247 1.11 bjh21 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
248 1.11 bjh21 ifmedia_set(&sc->sc_media, defmedia);
249 1.11 bjh21 } else {
250 1.11 bjh21 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
251 1.11 bjh21 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
252 1.11 bjh21 }
253 1.11 bjh21
254 1.1 bjh21 /* Now we can attach the interface. */
255 1.1 bjh21
256 1.1 bjh21 if_attach(ifp);
257 1.1 bjh21 ether_ifattach(ifp, myaddr);
258 1.1 bjh21
259 1.11 bjh21 printf("\n");
260 1.11 bjh21 }
261 1.11 bjh21
262 1.11 bjh21 /*
263 1.11 bjh21 * Media change callback.
264 1.11 bjh21 */
265 1.11 bjh21 static int
266 1.11 bjh21 ea_mediachange(struct ifnet *ifp)
267 1.11 bjh21 {
268 1.11 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
269 1.8 bjh21
270 1.11 bjh21 if (sc->sc_mediachange)
271 1.11 bjh21 return ((*sc->sc_mediachange)(sc));
272 1.11 bjh21 return (EINVAL);
273 1.1 bjh21 }
274 1.1 bjh21
275 1.11 bjh21 /*
276 1.11 bjh21 * Media status callback.
277 1.11 bjh21 */
278 1.11 bjh21 static void
279 1.11 bjh21 ea_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
280 1.11 bjh21 {
281 1.11 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
282 1.11 bjh21
283 1.11 bjh21 if (sc->sc_enabled == 0) {
284 1.11 bjh21 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
285 1.11 bjh21 ifmr->ifm_status = 0;
286 1.11 bjh21 return;
287 1.11 bjh21 }
288 1.11 bjh21
289 1.11 bjh21 if (sc->sc_mediastatus)
290 1.11 bjh21 (*sc->sc_mediastatus)(sc, ifmr);
291 1.11 bjh21 }
292 1.1 bjh21
293 1.1 bjh21 /*
294 1.1 bjh21 * Test the RAM on the ethernet card.
295 1.1 bjh21 */
296 1.1 bjh21
297 1.1 bjh21 void
298 1.1 bjh21 ea_ramtest(struct seeq8005_softc *sc)
299 1.1 bjh21 {
300 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
301 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
302 1.1 bjh21 int loop;
303 1.1 bjh21 u_int sum = 0;
304 1.1 bjh21
305 1.1 bjh21 /* dprintf(("ea_ramtest()\n"));*/
306 1.1 bjh21
307 1.1 bjh21 /*
308 1.1 bjh21 * Test the buffer memory on the board.
309 1.1 bjh21 * Write simple pattens to it and read them back.
310 1.1 bjh21 */
311 1.1 bjh21
312 1.1 bjh21 /* Set up the whole buffer RAM for writing */
313 1.1 bjh21
314 1.10 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_TX_EAP);
315 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (SEEQ_MAX_BUFFER_SIZE >> 8) - 1);
316 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
317 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_RX_PTR, SEEQ_MAX_BUFFER_SIZE - 2);
318 1.1 bjh21
319 1.10 bjh21 #define SEEQ_RAMTEST_LOOP(value) \
320 1.3 bjh21 do { \
321 1.3 bjh21 /* Set the write start address and write a pattern */ \
322 1.3 bjh21 ea_writebuf(sc, NULL, 0x0000, 0); \
323 1.10 bjh21 for (loop = 0; loop < SEEQ_MAX_BUFFER_SIZE; loop += 2) \
324 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (value)); \
325 1.3 bjh21 \
326 1.3 bjh21 /* Set the read start address and verify the pattern */ \
327 1.3 bjh21 ea_readbuf(sc, NULL, 0x0000, 0); \
328 1.10 bjh21 for (loop = 0; loop < SEEQ_MAX_BUFFER_SIZE; loop += 2) \
329 1.10 bjh21 if (bus_space_read_2(iot, ioh, SEEQ_BUFWIN) != (value)) \
330 1.3 bjh21 ++sum; \
331 1.3 bjh21 if (sum != 0) \
332 1.3 bjh21 dprintf(("sum=%d\n", sum)); \
333 1.3 bjh21 } while (/*CONSTCOND*/0)
334 1.3 bjh21
335 1.10 bjh21 SEEQ_RAMTEST_LOOP(loop);
336 1.10 bjh21 SEEQ_RAMTEST_LOOP(loop ^ (SEEQ_MAX_BUFFER_SIZE - 1));
337 1.10 bjh21 SEEQ_RAMTEST_LOOP(0xaa55);
338 1.10 bjh21 SEEQ_RAMTEST_LOOP(0x55aa);
339 1.1 bjh21
340 1.1 bjh21 /* Report */
341 1.1 bjh21
342 1.2 bjh21 if (sum > 0)
343 1.2 bjh21 printf("%s: buffer RAM failed self test, %d faults\n",
344 1.2 bjh21 sc->sc_dev.dv_xname, sum);
345 1.1 bjh21 }
346 1.1 bjh21
347 1.1 bjh21
348 1.1 bjh21 /*
349 1.1 bjh21 * Stop the tx interface.
350 1.1 bjh21 *
351 1.1 bjh21 * Returns 0 if the tx was already stopped or 1 if it was active
352 1.1 bjh21 */
353 1.1 bjh21
354 1.1 bjh21 static int
355 1.1 bjh21 ea_stoptx(struct seeq8005_softc *sc)
356 1.1 bjh21 {
357 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
358 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
359 1.1 bjh21 int timeout;
360 1.1 bjh21 int status;
361 1.1 bjh21
362 1.11 bjh21 DPRINTF(SEEQ_DEBUG_TX, ("seeq_stoptx()\n"));
363 1.11 bjh21
364 1.11 bjh21 sc->sc_enabled = 0;
365 1.1 bjh21
366 1.10 bjh21 status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
367 1.10 bjh21 if (!(status & SEEQ_STATUS_TX_ON))
368 1.1 bjh21 return 0;
369 1.1 bjh21
370 1.1 bjh21 /* Stop any tx and wait for confirmation */
371 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
372 1.10 bjh21 sc->sc_command | SEEQ_CMD_TX_OFF);
373 1.1 bjh21
374 1.1 bjh21 timeout = 20000;
375 1.1 bjh21 do {
376 1.10 bjh21 status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
377 1.11 bjh21 delay(1);
378 1.10 bjh21 } while ((status & SEEQ_STATUS_TX_ON) && --timeout > 0);
379 1.11 bjh21 if (timeout == 0)
380 1.11 bjh21 log(LOG_ERR, "%s: timeout waiting for tx termination\n",
381 1.11 bjh21 sc->sc_dev.dv_xname);
382 1.1 bjh21
383 1.1 bjh21 /* Clear any pending tx interrupt */
384 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
385 1.10 bjh21 sc->sc_command | SEEQ_CMD_TX_INTACK);
386 1.1 bjh21 return 1;
387 1.1 bjh21 }
388 1.1 bjh21
389 1.1 bjh21
390 1.1 bjh21 /*
391 1.1 bjh21 * Stop the rx interface.
392 1.1 bjh21 *
393 1.1 bjh21 * Returns 0 if the tx was already stopped or 1 if it was active
394 1.1 bjh21 */
395 1.1 bjh21
396 1.1 bjh21 static int
397 1.1 bjh21 ea_stoprx(struct seeq8005_softc *sc)
398 1.1 bjh21 {
399 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
400 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
401 1.1 bjh21 int timeout;
402 1.1 bjh21 int status;
403 1.1 bjh21
404 1.11 bjh21 DPRINTF(SEEQ_DEBUG_RX, ("seeq_stoprx()\n"));
405 1.1 bjh21
406 1.10 bjh21 status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
407 1.10 bjh21 if (!(status & SEEQ_STATUS_RX_ON))
408 1.1 bjh21 return 0;
409 1.1 bjh21
410 1.1 bjh21 /* Stop any rx and wait for confirmation */
411 1.1 bjh21
412 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
413 1.10 bjh21 sc->sc_command | SEEQ_CMD_RX_OFF);
414 1.1 bjh21
415 1.1 bjh21 timeout = 20000;
416 1.1 bjh21 do {
417 1.10 bjh21 status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
418 1.10 bjh21 } while ((status & SEEQ_STATUS_RX_ON) && --timeout > 0);
419 1.1 bjh21 if (timeout == 0)
420 1.11 bjh21 log(LOG_ERR, "%s: timeout waiting for rx termination\n",
421 1.11 bjh21 sc->sc_dev.dv_xname);
422 1.1 bjh21
423 1.1 bjh21 /* Clear any pending rx interrupt */
424 1.1 bjh21
425 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
426 1.10 bjh21 sc->sc_command | SEEQ_CMD_RX_INTACK);
427 1.1 bjh21 return 1;
428 1.1 bjh21 }
429 1.1 bjh21
430 1.1 bjh21
431 1.1 bjh21 /*
432 1.1 bjh21 * Stop interface.
433 1.1 bjh21 * Stop all IO and shut the interface down
434 1.1 bjh21 */
435 1.1 bjh21
436 1.1 bjh21 static void
437 1.5 bjh21 ea_stop(struct ifnet *ifp, int disable)
438 1.1 bjh21 {
439 1.5 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
440 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
441 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
442 1.1 bjh21
443 1.11 bjh21 DPRINTF(SEEQ_DEBUG_MISC, ("seeq_stop()\n"));
444 1.1 bjh21
445 1.1 bjh21 /* Stop all IO */
446 1.1 bjh21 ea_stoptx(sc);
447 1.1 bjh21 ea_stoprx(sc);
448 1.1 bjh21
449 1.1 bjh21 /* Disable rx and tx interrupts */
450 1.10 bjh21 sc->sc_command &= (SEEQ_CMD_RX_INTEN | SEEQ_CMD_TX_INTEN);
451 1.1 bjh21
452 1.1 bjh21 /* Clear any pending interrupts */
453 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
454 1.10 bjh21 sc->sc_command | SEEQ_CMD_RX_INTACK |
455 1.10 bjh21 SEEQ_CMD_TX_INTACK | SEEQ_CMD_DMA_INTACK |
456 1.10 bjh21 SEEQ_CMD_BW_INTACK);
457 1.11 bjh21
458 1.11 bjh21 if (sc->sc_variant == SEEQ_8004) {
459 1.11 bjh21 /* Put the chip to sleep */
460 1.11 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_CONFIG3);
461 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN,
462 1.11 bjh21 sc->sc_config3 | SEEQ_CFG3_SLEEP);
463 1.11 bjh21 }
464 1.1 bjh21
465 1.1 bjh21 /* Cancel any watchdog timer */
466 1.1 bjh21 sc->sc_ethercom.ec_if.if_timer = 0;
467 1.1 bjh21 }
468 1.1 bjh21
469 1.1 bjh21
470 1.1 bjh21 /*
471 1.1 bjh21 * Reset the chip
472 1.1 bjh21 * Following this the software registers are reset
473 1.1 bjh21 */
474 1.1 bjh21
475 1.1 bjh21 static void
476 1.1 bjh21 ea_chipreset(struct seeq8005_softc *sc)
477 1.1 bjh21 {
478 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
479 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
480 1.1 bjh21
481 1.11 bjh21 DPRINTF(SEEQ_DEBUG_MISC, ("seeq_chipreset()\n"));
482 1.1 bjh21
483 1.1 bjh21 /* Reset the controller. Min of 4us delay here */
484 1.1 bjh21
485 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, SEEQ_CFG2_RESET);
486 1.3 bjh21 delay(4);
487 1.1 bjh21
488 1.1 bjh21 sc->sc_command = 0;
489 1.1 bjh21 sc->sc_config1 = 0;
490 1.1 bjh21 sc->sc_config2 = 0;
491 1.11 bjh21 sc->sc_config3 = 0;
492 1.1 bjh21 }
493 1.1 bjh21
494 1.1 bjh21
495 1.1 bjh21 /*
496 1.1 bjh21 * If the DMA FIFO's in write mode, wait for it to empty. Needed when
497 1.1 bjh21 * switching the FIFO from write to read. We also use it when changing
498 1.1 bjh21 * the address for writes.
499 1.1 bjh21 */
500 1.1 bjh21 static void
501 1.1 bjh21 ea_await_fifo_empty(struct seeq8005_softc *sc)
502 1.1 bjh21 {
503 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
504 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
505 1.1 bjh21 int timeout;
506 1.1 bjh21
507 1.1 bjh21 timeout = 20000;
508 1.10 bjh21 if ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
509 1.10 bjh21 SEEQ_STATUS_FIFO_DIR) != 0)
510 1.1 bjh21 return; /* FIFO is reading anyway. */
511 1.10 bjh21 while ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
512 1.10 bjh21 SEEQ_STATUS_FIFO_EMPTY) == 0 &&
513 1.1 bjh21 --timeout > 0)
514 1.1 bjh21 continue;
515 1.1 bjh21 }
516 1.1 bjh21
517 1.1 bjh21 /*
518 1.1 bjh21 * Wait for the DMA FIFO to fill before reading from it.
519 1.1 bjh21 */
520 1.1 bjh21 static void
521 1.1 bjh21 ea_await_fifo_full(struct seeq8005_softc *sc)
522 1.1 bjh21 {
523 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
524 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
525 1.1 bjh21 int timeout;
526 1.1 bjh21
527 1.1 bjh21 timeout = 20000;
528 1.10 bjh21 while ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
529 1.10 bjh21 SEEQ_STATUS_FIFO_FULL) == 0 &&
530 1.1 bjh21 --timeout > 0)
531 1.1 bjh21 continue;
532 1.1 bjh21 }
533 1.1 bjh21
534 1.1 bjh21 /*
535 1.1 bjh21 * write to the buffer memory on the interface
536 1.1 bjh21 *
537 1.1 bjh21 * The buffer address is set to ADDR.
538 1.1 bjh21 * If len != 0 then data is copied from the address starting at buf
539 1.1 bjh21 * to the interface buffer.
540 1.1 bjh21 * BUF must be usable as a u_int16_t *.
541 1.1 bjh21 * If LEN is odd, it must be safe to overwrite one extra byte.
542 1.1 bjh21 */
543 1.1 bjh21
544 1.1 bjh21 static void
545 1.11 bjh21 ea_writebuf(struct seeq8005_softc *sc, u_char *buf, int addr, size_t len)
546 1.1 bjh21 {
547 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
548 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
549 1.1 bjh21
550 1.1 bjh21 dprintf(("writebuf: st=%04x\n",
551 1.10 bjh21 bus_space_read_2(iot, ioh, SEEQ_STATUS)));
552 1.1 bjh21
553 1.1 bjh21 #ifdef DIAGNOSTIC
554 1.1 bjh21 if (__predict_false(!ALIGNED_POINTER(buf, u_int16_t)))
555 1.1 bjh21 panic("%s: unaligned writebuf", sc->sc_dev.dv_xname);
556 1.1 bjh21 #endif
557 1.10 bjh21 if (__predict_false(addr >= SEEQ_MAX_BUFFER_SIZE))
558 1.1 bjh21 panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
559 1.1 bjh21
560 1.1 bjh21 /* Assume that copying too much is safe. */
561 1.1 bjh21 if (len % 2 != 0)
562 1.1 bjh21 len++;
563 1.1 bjh21
564 1.11 bjh21 if (addr != -1) {
565 1.11 bjh21 ea_await_fifo_empty(sc);
566 1.1 bjh21
567 1.11 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_LOCAL_MEM);
568 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
569 1.11 bjh21 sc->sc_command | SEEQ_CMD_FIFO_WRITE);
570 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr);
571 1.11 bjh21 }
572 1.1 bjh21
573 1.1 bjh21 if (len > 0)
574 1.10 bjh21 bus_space_write_multi_2(iot, ioh, SEEQ_BUFWIN,
575 1.1 bjh21 (u_int16_t *)buf, len / 2);
576 1.1 bjh21 /* Leave FIFO to empty in the background */
577 1.1 bjh21 }
578 1.1 bjh21
579 1.1 bjh21
580 1.1 bjh21 /*
581 1.1 bjh21 * read from the buffer memory on the interface
582 1.1 bjh21 *
583 1.1 bjh21 * The buffer address is set to ADDR.
584 1.1 bjh21 * If len != 0 then data is copied from the interface buffer to the
585 1.1 bjh21 * address starting at buf.
586 1.1 bjh21 * BUF must be usable as a u_int16_t *.
587 1.1 bjh21 * If LEN is odd, it must be safe to overwrite one extra byte.
588 1.1 bjh21 */
589 1.1 bjh21
590 1.1 bjh21 static void
591 1.11 bjh21 ea_readbuf(struct seeq8005_softc *sc, u_char *buf, int addr, size_t len)
592 1.1 bjh21 {
593 1.1 bjh21
594 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
595 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
596 1.1 bjh21
597 1.1 bjh21 dprintf(("readbuf: st=%04x addr=%04x len=%d\n",
598 1.10 bjh21 bus_space_read_2(iot, ioh, SEEQ_STATUS), addr, len));
599 1.1 bjh21
600 1.1 bjh21 #ifdef DIAGNOSTIC
601 1.1 bjh21 if (!ALIGNED_POINTER(buf, u_int16_t))
602 1.1 bjh21 panic("%s: unaligned readbuf", sc->sc_dev.dv_xname);
603 1.1 bjh21 #endif
604 1.10 bjh21 if (addr >= SEEQ_MAX_BUFFER_SIZE)
605 1.1 bjh21 panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
606 1.1 bjh21
607 1.1 bjh21 /* Assume that copying too much is safe. */
608 1.1 bjh21 if (len % 2 != 0)
609 1.1 bjh21 len++;
610 1.1 bjh21
611 1.11 bjh21 if (addr != -1) {
612 1.11 bjh21 ea_await_fifo_empty(sc);
613 1.1 bjh21
614 1.11 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_LOCAL_MEM);
615 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr);
616 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
617 1.11 bjh21 sc->sc_command | SEEQ_CMD_FIFO_READ);
618 1.1 bjh21
619 1.11 bjh21 ea_await_fifo_full(sc);
620 1.11 bjh21 }
621 1.1 bjh21
622 1.1 bjh21 if (len > 0)
623 1.10 bjh21 bus_space_read_multi_2(iot, ioh, SEEQ_BUFWIN,
624 1.1 bjh21 (u_int16_t *)buf, len / 2);
625 1.1 bjh21 }
626 1.1 bjh21
627 1.3 bjh21 static void
628 1.3 bjh21 ea_select_buffer(struct seeq8005_softc *sc, int bufcode)
629 1.3 bjh21 {
630 1.3 bjh21
631 1.10 bjh21 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_CONFIG1,
632 1.3 bjh21 sc->sc_config1 | bufcode);
633 1.3 bjh21 }
634 1.1 bjh21
635 1.5 bjh21 /* Must be called at splnet */
636 1.5 bjh21 static void
637 1.5 bjh21 ea_set_address(struct seeq8005_softc *sc, int which, u_int8_t const *ea)
638 1.5 bjh21 {
639 1.5 bjh21 int i;
640 1.5 bjh21
641 1.10 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_STATION_ADDR0 + which);
642 1.5 bjh21 for (i = 0; i < ETHER_ADDR_LEN; ++i)
643 1.10 bjh21 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN,
644 1.5 bjh21 ea[i]);
645 1.5 bjh21 }
646 1.5 bjh21
647 1.1 bjh21 /*
648 1.1 bjh21 * Initialize interface.
649 1.1 bjh21 *
650 1.1 bjh21 * This should leave the interface in a state for packet reception and
651 1.1 bjh21 * transmission.
652 1.1 bjh21 */
653 1.1 bjh21
654 1.1 bjh21 static int
655 1.5 bjh21 ea_init(struct ifnet *ifp)
656 1.1 bjh21 {
657 1.5 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
658 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
659 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
660 1.5 bjh21 int s;
661 1.1 bjh21
662 1.1 bjh21 dprintf(("ea_init()\n"));
663 1.1 bjh21
664 1.1 bjh21 s = splnet();
665 1.1 bjh21
666 1.1 bjh21 /* First, reset the board. */
667 1.1 bjh21
668 1.3 bjh21 ea_chipreset(sc);
669 1.3 bjh21
670 1.3 bjh21 /* Set up defaults for the registers */
671 1.3 bjh21
672 1.11 bjh21 sc->sc_command = 0;
673 1.11 bjh21 sc->sc_config1 = 0;
674 1.3 bjh21 #if BYTE_ORDER == BIG_ENDIAN
675 1.11 bjh21 sc->sc_config2 = SEEQ_CFG2_BYTESWAP;
676 1.3 bjh21 #else
677 1.3 bjh21 sc->sc_config2 = 0;
678 1.3 bjh21 #endif
679 1.11 bjh21 sc->sc_config3 = 0;
680 1.1 bjh21
681 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND, sc->sc_command);
682 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG1, sc->sc_config1);
683 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
684 1.11 bjh21 if (sc->sc_variant == SEEQ_8004) {
685 1.11 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_CONFIG3);
686 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, sc->sc_config3);
687 1.11 bjh21 }
688 1.11 bjh21
689 1.11 bjh21 /* Write the station address - the receiver must be off */
690 1.11 bjh21 ea_set_address(sc, 0, LLADDR(ifp->if_sadl));
691 1.3 bjh21
692 1.3 bjh21 /* Split board memory into Rx and Tx. */
693 1.10 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_TX_EAP);
694 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (sc->sc_tx_bufsize>> 8) - 1);
695 1.3 bjh21
696 1.11 bjh21 if (sc->sc_variant == SEEQ_8004)
697 1.11 bjh21 sc->sc_config2 |= SEEQ_CFG2_RX_TX_DISABLE;
698 1.1 bjh21
699 1.1 bjh21 /* Configure rx. */
700 1.1 bjh21 if (ifp->if_flags & IFF_PROMISC)
701 1.10 bjh21 sc->sc_config1 = SEEQ_CFG1_PROMISCUOUS;
702 1.9 bjh21 else if (ifp->if_flags & IFF_ALLMULTI)
703 1.10 bjh21 sc->sc_config1 = SEEQ_CFG1_MULTICAST;
704 1.1 bjh21 else
705 1.10 bjh21 sc->sc_config1 = SEEQ_CFG1_BROADCAST;
706 1.10 bjh21 sc->sc_config1 |= SEEQ_CFG1_STATION_ADDR0;
707 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG1, sc->sc_config1);
708 1.3 bjh21
709 1.3 bjh21 /* Setup the Rx pointers */
710 1.11 bjh21 sc->sc_rx_ptr = sc->sc_tx_bufsize;
711 1.3 bjh21
712 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_RX_PTR, sc->sc_rx_ptr);
713 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_RX_END, sc->sc_rx_ptr >> 8);
714 1.3 bjh21
715 1.3 bjh21
716 1.3 bjh21 /* Place a NULL header at the beginning of the receive area */
717 1.3 bjh21 ea_writebuf(sc, NULL, sc->sc_rx_ptr, 0);
718 1.3 bjh21
719 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
720 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
721 1.1 bjh21
722 1.3 bjh21
723 1.1 bjh21 /* Configure TX. */
724 1.1 bjh21 dprintf(("Configuring tx...\n"));
725 1.1 bjh21
726 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
727 1.1 bjh21
728 1.10 bjh21 sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
729 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
730 1.1 bjh21
731 1.11 bjh21 /* Reset tx buffer pointers */
732 1.11 bjh21 sc->sc_tx_cur = 0;
733 1.11 bjh21 sc->sc_tx_used = 0;
734 1.11 bjh21 sc->sc_tx_next = 0;
735 1.1 bjh21
736 1.1 bjh21 /* Place a NULL header at the beginning of the transmit area */
737 1.1 bjh21 ea_writebuf(sc, NULL, 0x0000, 0);
738 1.1 bjh21
739 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
740 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
741 1.1 bjh21
742 1.10 bjh21 sc->sc_command |= SEEQ_CMD_TX_INTEN;
743 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND, sc->sc_command);
744 1.1 bjh21
745 1.11 bjh21 /* Turn on Rx */
746 1.11 bjh21 sc->sc_command |= SEEQ_CMD_RX_INTEN;
747 1.11 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
748 1.11 bjh21 sc->sc_command | SEEQ_CMD_RX_ON);
749 1.11 bjh21
750 1.3 bjh21 /* TX_ON gets set by ea_txpacket when there's something to transmit. */
751 1.1 bjh21
752 1.1 bjh21
753 1.1 bjh21 /* Set flags appropriately. */
754 1.1 bjh21 ifp->if_flags |= IFF_RUNNING;
755 1.1 bjh21 ifp->if_flags &= ~IFF_OACTIVE;
756 1.11 bjh21 sc->sc_enabled = 1;
757 1.1 bjh21
758 1.1 bjh21 /* And start output. */
759 1.1 bjh21 ea_start(ifp);
760 1.1 bjh21
761 1.1 bjh21 splx(s);
762 1.1 bjh21 return 0;
763 1.1 bjh21 }
764 1.1 bjh21
765 1.1 bjh21 /*
766 1.1 bjh21 * Start output on interface. Get datagrams from the queue and output them,
767 1.1 bjh21 * giving the receiver a chance between datagrams. Call only from splnet or
768 1.1 bjh21 * interrupt level!
769 1.1 bjh21 */
770 1.1 bjh21
771 1.1 bjh21 static void
772 1.1 bjh21 ea_start(struct ifnet *ifp)
773 1.1 bjh21 {
774 1.1 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
775 1.1 bjh21 int s;
776 1.1 bjh21
777 1.1 bjh21 s = splnet();
778 1.10 bjh21 #ifdef SEEQ_TX_DEBUG
779 1.1 bjh21 dprintf(("ea_start()...\n"));
780 1.1 bjh21 #endif
781 1.1 bjh21
782 1.1 bjh21 /* Don't do anything if output is active. */
783 1.1 bjh21
784 1.1 bjh21 if (ifp->if_flags & IFF_OACTIVE)
785 1.1 bjh21 return;
786 1.1 bjh21
787 1.1 bjh21 /* Mark interface as output active */
788 1.1 bjh21
789 1.1 bjh21 ifp->if_flags |= IFF_OACTIVE;
790 1.1 bjh21
791 1.1 bjh21 /* tx packets */
792 1.1 bjh21
793 1.1 bjh21 eatxpacket(sc);
794 1.1 bjh21 splx(s);
795 1.1 bjh21 }
796 1.1 bjh21
797 1.1 bjh21
798 1.1 bjh21 /*
799 1.1 bjh21 * Transfer a packet to the interface buffer and start transmission
800 1.1 bjh21 *
801 1.1 bjh21 * Called at splnet()
802 1.1 bjh21 */
803 1.1 bjh21
804 1.1 bjh21 void
805 1.1 bjh21 eatxpacket(struct seeq8005_softc *sc)
806 1.1 bjh21 {
807 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
808 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
809 1.1 bjh21 struct mbuf *m, *m0;
810 1.1 bjh21 struct ifnet *ifp;
811 1.1 bjh21 int len, nextpacket;
812 1.1 bjh21 u_int8_t hdr[4];
813 1.1 bjh21
814 1.1 bjh21 ifp = &sc->sc_ethercom.ec_if;
815 1.1 bjh21
816 1.1 bjh21 /* Dequeue the next packet. */
817 1.7 thorpej IFQ_DEQUEUE(&ifp->if_snd, m0);
818 1.1 bjh21
819 1.1 bjh21 /* If there's nothing to send, return. */
820 1.1 bjh21 if (!m0) {
821 1.1 bjh21 ifp->if_flags &= ~IFF_OACTIVE;
822 1.10 bjh21 sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
823 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
824 1.10 bjh21 #ifdef SEEQ_TX_DEBUG
825 1.1 bjh21 dprintf(("tx finished\n"));
826 1.1 bjh21 #endif
827 1.1 bjh21 return;
828 1.1 bjh21 }
829 1.1 bjh21
830 1.1 bjh21 #if NBPFILTER > 0
831 1.1 bjh21 /* Give the packet to the bpf, if any. */
832 1.1 bjh21 if (ifp->if_bpf)
833 1.1 bjh21 bpf_mtap(ifp->if_bpf, m0);
834 1.1 bjh21 #endif
835 1.1 bjh21
836 1.10 bjh21 #ifdef SEEQ_TX_DEBUG
837 1.1 bjh21 dprintf(("Tx new packet\n"));
838 1.1 bjh21 #endif
839 1.1 bjh21
840 1.10 bjh21 sc->sc_config2 &= ~SEEQ_CFG2_OUTPUT;
841 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
842 1.1 bjh21
843 1.1 bjh21 /*
844 1.1 bjh21 * Copy the frame to the start of the transmit area on the card,
845 1.1 bjh21 * leaving four bytes for the transmit header.
846 1.1 bjh21 */
847 1.1 bjh21 len = 0;
848 1.1 bjh21 for (m = m0; m; m = m->m_next) {
849 1.1 bjh21 if (m->m_len == 0)
850 1.1 bjh21 continue;
851 1.1 bjh21 ea_writebuf(sc, mtod(m, caddr_t), 4 + len, m->m_len);
852 1.1 bjh21 len += m->m_len;
853 1.1 bjh21 }
854 1.1 bjh21 m_freem(m0);
855 1.1 bjh21
856 1.1 bjh21
857 1.1 bjh21 /* If packet size is odd round up to the next 16 bit boundry */
858 1.1 bjh21 if (len % 2)
859 1.1 bjh21 ++len;
860 1.1 bjh21
861 1.1 bjh21 len = max(len, ETHER_MIN_LEN);
862 1.1 bjh21
863 1.1 bjh21 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN))
864 1.1 bjh21 log(LOG_WARNING, "%s: oversize packet = %d bytes\n",
865 1.1 bjh21 sc->sc_dev.dv_xname, len);
866 1.1 bjh21
867 1.10 bjh21 #if 0 /*def SEEQ_TX_DEBUG*/
868 1.1 bjh21 dprintf(("ea: xfr pkt length=%d...\n", len));
869 1.1 bjh21
870 1.1 bjh21 dprintf(("%s-->", ether_sprintf(sc->sc_pktbuf+6)));
871 1.1 bjh21 dprintf(("%s\n", ether_sprintf(sc->sc_pktbuf)));
872 1.1 bjh21 #endif
873 1.1 bjh21
874 1.10 bjh21 /* dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));*/
875 1.1 bjh21
876 1.1 bjh21 /* Follow it with a NULL packet header */
877 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
878 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
879 1.1 bjh21
880 1.1 bjh21
881 1.1 bjh21 /* Write the packet header */
882 1.1 bjh21
883 1.1 bjh21 nextpacket = len + 4;
884 1.1 bjh21 hdr[0] = (nextpacket >> 8) & 0xff;
885 1.1 bjh21 hdr[1] = nextpacket & 0xff;
886 1.10 bjh21 hdr[2] = SEEQ_PKTCMD_TX | SEEQ_PKTCMD_DATA_FOLLOWS |
887 1.10 bjh21 SEEQ_TXCMD_XMIT_SUCCESS_INT | SEEQ_TXCMD_COLLISION_INT;
888 1.1 bjh21 hdr[3] = 0; /* Status byte -- will be update by hardware. */
889 1.1 bjh21 ea_writebuf(sc, hdr, 0x0000, 4);
890 1.1 bjh21
891 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
892 1.1 bjh21
893 1.10 bjh21 /* dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));*/
894 1.1 bjh21
895 1.10 bjh21 #ifdef SEEQ_PACKET_DEBUG
896 1.1 bjh21 ea_dump_buffer(sc, 0);
897 1.1 bjh21 #endif
898 1.1 bjh21
899 1.1 bjh21
900 1.1 bjh21 /* Now transmit the datagram. */
901 1.10 bjh21 /* dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));*/
902 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
903 1.10 bjh21 sc->sc_command | SEEQ_CMD_TX_ON);
904 1.10 bjh21 #ifdef SEEQ_TX_DEBUG
905 1.10 bjh21 dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));
906 1.1 bjh21 dprintf(("tx: queued\n"));
907 1.1 bjh21 #endif
908 1.1 bjh21 }
909 1.1 bjh21
910 1.1 bjh21
911 1.1 bjh21 /*
912 1.1 bjh21 * Ethernet controller interrupt.
913 1.1 bjh21 */
914 1.1 bjh21
915 1.1 bjh21 int
916 1.1 bjh21 seeq8005intr(void *arg)
917 1.1 bjh21 {
918 1.1 bjh21 struct seeq8005_softc *sc = arg;
919 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
920 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
921 1.1 bjh21 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
922 1.11 bjh21 int status, handled;
923 1.1 bjh21 u_int8_t txhdr[4];
924 1.1 bjh21 u_int txstatus;
925 1.1 bjh21
926 1.1 bjh21 handled = 0;
927 1.1 bjh21 dprintf(("eaintr: "));
928 1.1 bjh21
929 1.1 bjh21
930 1.1 bjh21 /* Get the controller status */
931 1.10 bjh21 status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
932 1.1 bjh21 dprintf(("st=%04x ", status));
933 1.1 bjh21
934 1.1 bjh21
935 1.1 bjh21 /* Tx interrupt ? */
936 1.10 bjh21 if (status & SEEQ_STATUS_TX_INT) {
937 1.1 bjh21 dprintf(("txint "));
938 1.1 bjh21 handled = 1;
939 1.1 bjh21
940 1.1 bjh21 /* Acknowledge the interrupt */
941 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
942 1.10 bjh21 sc->sc_command | SEEQ_CMD_TX_INTACK);
943 1.1 bjh21
944 1.1 bjh21 ea_readbuf(sc, txhdr, 0x0000, 4);
945 1.1 bjh21
946 1.10 bjh21 #ifdef SEEQ_TX_DEBUG
947 1.1 bjh21 dprintf(("txstatus=%02x %02x %02x %02x\n",
948 1.1 bjh21 txhdr[0], txhdr[1], txhdr[2], txhdr[3]));
949 1.1 bjh21 #endif
950 1.1 bjh21 txstatus = txhdr[3];
951 1.1 bjh21
952 1.1 bjh21 /*
953 1.11 bjh21 * If SEEQ_TXSTAT_COLLISION is set then we received at least
954 1.11 bjh21 * one collision. On the 8004 we can find out exactly how many
955 1.11 bjh21 * collisions occurred.
956 1.1 bjh21 *
957 1.11 bjh21 * The SEEQ_PKTSTAT_DONE will be set if the transmission has
958 1.11 bjh21 * completed.
959 1.1 bjh21 *
960 1.11 bjh21 * If SEEQ_TXSTAT_COLLISION16 is set then 16 collisions
961 1.11 bjh21 * occurred and the packet transmission was aborted.
962 1.11 bjh21 * This situation is untested as present.
963 1.11 bjh21 *
964 1.11 bjh21 * The SEEQ_TXSTAT_BABBLE should never be set and is untested
965 1.11 bjh21 * as we should never xmit oversized packets.
966 1.1 bjh21 */
967 1.11 bjh21 if (txstatus & SEEQ_TXSTAT_COLLISION) {
968 1.11 bjh21 switch (sc->sc_variant) {
969 1.11 bjh21 case SEEQ_8004: {
970 1.11 bjh21 int colls;
971 1.11 bjh21
972 1.11 bjh21 /*
973 1.11 bjh21 * The 8004 contains a 4 bit collision count
974 1.11 bjh21 * in the status register.
975 1.11 bjh21 */
976 1.11 bjh21
977 1.11 bjh21 /* This appears to be broken on 80C04.AE */
978 1.11 bjh21 /* ifp->if_collisions +=
979 1.11 bjh21 (txstatus >> SEEQ_TXSTAT_COLLISIONS_SHIFT)
980 1.11 bjh21 & SEEQ_TXSTAT_COLLISION_MASK;*/
981 1.11 bjh21
982 1.11 bjh21 /* Use the TX Collision register */
983 1.11 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_TX_COLLS);
984 1.11 bjh21 colls = bus_space_read_1(iot, ioh,
985 1.11 bjh21 SEEQ_BUFWIN);
986 1.11 bjh21 ifp->if_collisions += colls;
987 1.11 bjh21 break;
988 1.11 bjh21 }
989 1.11 bjh21 case SEEQ_8005:
990 1.11 bjh21 /* We known there was at least 1 collision */
991 1.11 bjh21 ifp->if_collisions++;
992 1.11 bjh21 break;
993 1.11 bjh21 }
994 1.11 bjh21 } else if (txstatus & SEEQ_TXSTAT_COLLISION16) {
995 1.11 bjh21 printf("seeq_intr: col16 %x\n", txstatus);
996 1.11 bjh21 ifp->if_collisions += 16;
997 1.11 bjh21 ifp->if_oerrors++;
998 1.11 bjh21 } else if (txstatus & SEEQ_TXSTAT_BABBLE) {
999 1.1 bjh21 ifp->if_oerrors++;
1000 1.11 bjh21 }
1001 1.1 bjh21
1002 1.11 bjh21 /* Have we completed transmission on the packet ? */
1003 1.10 bjh21 if (txstatus & SEEQ_PKTSTAT_DONE) {
1004 1.11 bjh21 /* Clear watchdog timer. */
1005 1.11 bjh21 ifp->if_timer = 0;
1006 1.11 bjh21 ifp->if_flags &= ~IFF_OACTIVE;
1007 1.11 bjh21
1008 1.11 bjh21 /* Update stats */
1009 1.1 bjh21 ifp->if_opackets++;
1010 1.1 bjh21
1011 1.1 bjh21 /* Tx next packet */
1012 1.1 bjh21
1013 1.1 bjh21 eatxpacket(sc);
1014 1.1 bjh21 }
1015 1.11 bjh21
1016 1.1 bjh21 }
1017 1.1 bjh21
1018 1.1 bjh21
1019 1.1 bjh21 /* Rx interrupt ? */
1020 1.10 bjh21 if (status & SEEQ_STATUS_RX_INT) {
1021 1.1 bjh21 dprintf(("rxint "));
1022 1.1 bjh21 handled = 1;
1023 1.1 bjh21
1024 1.1 bjh21 /* Acknowledge the interrupt */
1025 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
1026 1.10 bjh21 sc->sc_command | SEEQ_CMD_RX_INTACK);
1027 1.1 bjh21
1028 1.1 bjh21 /* Processes the received packets */
1029 1.11 bjh21 ea_getpackets(sc);
1030 1.1 bjh21
1031 1.1 bjh21
1032 1.1 bjh21 #if 0
1033 1.1 bjh21 /* Make sure the receiver is on */
1034 1.10 bjh21 if ((status & SEEQ_STATUS_RX_ON) == 0) {
1035 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
1036 1.10 bjh21 sc->sc_command | SEEQ_CMD_RX_ON);
1037 1.1 bjh21 printf("rxintr: rx is off st=%04x\n",status);
1038 1.1 bjh21 }
1039 1.1 bjh21 #endif
1040 1.1 bjh21 }
1041 1.1 bjh21
1042 1.10 bjh21 #ifdef SEEQ_DEBUG
1043 1.10 bjh21 status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
1044 1.1 bjh21 dprintf(("st=%04x\n", status));
1045 1.1 bjh21 #endif
1046 1.1 bjh21
1047 1.1 bjh21 return handled;
1048 1.1 bjh21 }
1049 1.1 bjh21
1050 1.1 bjh21
1051 1.1 bjh21 void
1052 1.11 bjh21 ea_getpackets(struct seeq8005_softc *sc)
1053 1.1 bjh21 {
1054 1.1 bjh21 bus_space_tag_t iot = sc->sc_iot;
1055 1.1 bjh21 bus_space_handle_t ioh = sc->sc_ioh;
1056 1.1 bjh21 u_int addr;
1057 1.1 bjh21 int len;
1058 1.1 bjh21 int ctrl;
1059 1.1 bjh21 int ptr;
1060 1.1 bjh21 int pack;
1061 1.1 bjh21 int status;
1062 1.1 bjh21 u_int8_t rxhdr[4];
1063 1.1 bjh21 struct ifnet *ifp;
1064 1.1 bjh21
1065 1.1 bjh21 ifp = &sc->sc_ethercom.ec_if;
1066 1.1 bjh21
1067 1.1 bjh21
1068 1.1 bjh21 /* We start from the last rx pointer position */
1069 1.1 bjh21 addr = sc->sc_rx_ptr;
1070 1.10 bjh21 sc->sc_config2 &= ~SEEQ_CFG2_OUTPUT;
1071 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
1072 1.1 bjh21
1073 1.1 bjh21 do {
1074 1.1 bjh21 /* Read rx header */
1075 1.1 bjh21 ea_readbuf(sc, rxhdr, addr, 4);
1076 1.1 bjh21
1077 1.1 bjh21 /* Split the packet header */
1078 1.1 bjh21 ptr = (rxhdr[0] << 8) | rxhdr[1];
1079 1.1 bjh21 ctrl = rxhdr[2];
1080 1.1 bjh21 status = rxhdr[3];
1081 1.1 bjh21
1082 1.10 bjh21 #ifdef SEEQ_RX_DEBUG
1083 1.1 bjh21 dprintf(("addr=%04x ptr=%04x ctrl=%02x status=%02x\n",
1084 1.1 bjh21 addr, ptr, ctrl, status));
1085 1.1 bjh21 #endif
1086 1.1 bjh21
1087 1.1 bjh21
1088 1.1 bjh21 /* Zero packet ptr ? then must be null header so exit */
1089 1.1 bjh21 if (ptr == 0) break;
1090 1.1 bjh21
1091 1.1 bjh21
1092 1.1 bjh21 /* Get packet length */
1093 1.1 bjh21 len = (ptr - addr) - 4;
1094 1.1 bjh21
1095 1.1 bjh21 if (len < 0)
1096 1.11 bjh21 len += sc->sc_rx_bufsize;
1097 1.1 bjh21
1098 1.10 bjh21 #ifdef SEEQ_RX_DEBUG
1099 1.1 bjh21 dprintf(("len=%04x\n", len));
1100 1.1 bjh21 #endif
1101 1.1 bjh21
1102 1.1 bjh21
1103 1.1 bjh21 /* Has the packet rx completed ? if not then exit */
1104 1.10 bjh21 if ((status & SEEQ_PKTSTAT_DONE) == 0)
1105 1.1 bjh21 break;
1106 1.1 bjh21
1107 1.1 bjh21 /*
1108 1.1 bjh21 * Did we have any errors? then note error and go to
1109 1.1 bjh21 * next packet
1110 1.1 bjh21 */
1111 1.11 bjh21 if (__predict_false(status & SEEQ_RXSTAT_ERROR_MASK)) {
1112 1.1 bjh21 ++ifp->if_ierrors;
1113 1.1 bjh21 log(LOG_WARNING,
1114 1.1 bjh21 "%s: rx packet error (%02x) - dropping packet\n",
1115 1.1 bjh21 sc->sc_dev.dv_xname, status & 0x0f);
1116 1.10 bjh21 sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
1117 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2,
1118 1.1 bjh21 sc->sc_config2);
1119 1.5 bjh21 ea_init(ifp);
1120 1.1 bjh21 return;
1121 1.1 bjh21 }
1122 1.1 bjh21
1123 1.1 bjh21 /*
1124 1.1 bjh21 * Is the packet too big ? - this will probably be trapped
1125 1.1 bjh21 * above as a receive error
1126 1.1 bjh21 */
1127 1.1 bjh21 if (__predict_false(len > (ETHER_MAX_LEN - ETHER_CRC_LEN))) {
1128 1.1 bjh21 ++ifp->if_ierrors;
1129 1.1 bjh21 log(LOG_WARNING, "%s: rx packet size error len=%d\n",
1130 1.1 bjh21 sc->sc_dev.dv_xname, len);
1131 1.10 bjh21 sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
1132 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2,
1133 1.1 bjh21 sc->sc_config2);
1134 1.5 bjh21 ea_init(ifp);
1135 1.1 bjh21 return;
1136 1.1 bjh21 }
1137 1.1 bjh21
1138 1.1 bjh21 ifp->if_ipackets++;
1139 1.1 bjh21 /* Pass data up to upper levels. */
1140 1.11 bjh21 ea_read(sc, addr + 4, len);
1141 1.1 bjh21
1142 1.1 bjh21 addr = ptr;
1143 1.1 bjh21 ++pack;
1144 1.1 bjh21 } while (len != 0);
1145 1.1 bjh21
1146 1.10 bjh21 sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
1147 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
1148 1.1 bjh21
1149 1.10 bjh21 #ifdef SEEQ_RX_DEBUG
1150 1.1 bjh21 dprintf(("new rx ptr=%04x\n", addr));
1151 1.1 bjh21 #endif
1152 1.1 bjh21
1153 1.1 bjh21
1154 1.1 bjh21 /* Store new rx pointer */
1155 1.1 bjh21 sc->sc_rx_ptr = addr;
1156 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_RX_END, sc->sc_rx_ptr >> 8);
1157 1.1 bjh21
1158 1.1 bjh21 /* Make sure the receiver is on */
1159 1.10 bjh21 bus_space_write_2(iot, ioh, SEEQ_COMMAND,
1160 1.10 bjh21 sc->sc_command | SEEQ_CMD_RX_ON);
1161 1.1 bjh21
1162 1.1 bjh21 }
1163 1.1 bjh21
1164 1.1 bjh21
1165 1.1 bjh21 /*
1166 1.1 bjh21 * Pass a packet up to the higher levels.
1167 1.1 bjh21 */
1168 1.1 bjh21
1169 1.1 bjh21 static void
1170 1.11 bjh21 ea_read(struct seeq8005_softc *sc, int addr, int len)
1171 1.1 bjh21 {
1172 1.1 bjh21 struct mbuf *m;
1173 1.1 bjh21 struct ifnet *ifp;
1174 1.1 bjh21
1175 1.1 bjh21 ifp = &sc->sc_ethercom.ec_if;
1176 1.1 bjh21
1177 1.1 bjh21 /* Pull packet off interface. */
1178 1.11 bjh21 m = ea_get(sc, addr, len, ifp);
1179 1.1 bjh21 if (m == 0)
1180 1.1 bjh21 return;
1181 1.1 bjh21
1182 1.10 bjh21 #ifdef SEEQ_RX_DEBUG
1183 1.1 bjh21 dprintf(("%s-->", ether_sprintf(eh->ether_shost)));
1184 1.1 bjh21 dprintf(("%s\n", ether_sprintf(eh->ether_dhost)));
1185 1.1 bjh21 #endif
1186 1.1 bjh21
1187 1.1 bjh21 #if NBPFILTER > 0
1188 1.1 bjh21 /*
1189 1.1 bjh21 * Check if there's a BPF listener on this interface.
1190 1.1 bjh21 * If so, hand off the raw packet to bpf.
1191 1.1 bjh21 */
1192 1.4 thorpej if (ifp->if_bpf)
1193 1.1 bjh21 bpf_mtap(ifp->if_bpf, m);
1194 1.1 bjh21 #endif
1195 1.1 bjh21
1196 1.1 bjh21 (*ifp->if_input)(ifp, m);
1197 1.1 bjh21 }
1198 1.1 bjh21
1199 1.1 bjh21 /*
1200 1.1 bjh21 * Pull read data off a interface. Len is length of data, with local net
1201 1.1 bjh21 * header stripped. We copy the data into mbufs. When full cluster sized
1202 1.1 bjh21 * units are present we copy into clusters.
1203 1.1 bjh21 */
1204 1.1 bjh21
1205 1.1 bjh21 struct mbuf *
1206 1.11 bjh21 ea_get(struct seeq8005_softc *sc, int addr, int totlen, struct ifnet *ifp)
1207 1.1 bjh21 {
1208 1.1 bjh21 struct mbuf *top, **mp, *m;
1209 1.1 bjh21 int len;
1210 1.1 bjh21 u_int cp, epkt;
1211 1.1 bjh21
1212 1.1 bjh21 cp = addr;
1213 1.1 bjh21 epkt = cp + totlen;
1214 1.1 bjh21
1215 1.1 bjh21 MGETHDR(m, M_DONTWAIT, MT_DATA);
1216 1.1 bjh21 if (m == 0)
1217 1.1 bjh21 return 0;
1218 1.1 bjh21 m->m_pkthdr.rcvif = ifp;
1219 1.1 bjh21 m->m_pkthdr.len = totlen;
1220 1.1 bjh21 m->m_len = MHLEN;
1221 1.1 bjh21 top = 0;
1222 1.1 bjh21 mp = ⊤
1223 1.1 bjh21
1224 1.1 bjh21 while (totlen > 0) {
1225 1.1 bjh21 if (top) {
1226 1.1 bjh21 MGET(m, M_DONTWAIT, MT_DATA);
1227 1.1 bjh21 if (m == 0) {
1228 1.1 bjh21 m_freem(top);
1229 1.1 bjh21 return 0;
1230 1.1 bjh21 }
1231 1.1 bjh21 m->m_len = MLEN;
1232 1.1 bjh21 }
1233 1.1 bjh21 len = min(totlen, epkt - cp);
1234 1.1 bjh21 if (len >= MINCLSIZE) {
1235 1.1 bjh21 MCLGET(m, M_DONTWAIT);
1236 1.1 bjh21 if (m->m_flags & M_EXT)
1237 1.1 bjh21 m->m_len = len = min(len, MCLBYTES);
1238 1.1 bjh21 else
1239 1.1 bjh21 len = m->m_len;
1240 1.1 bjh21 } else {
1241 1.1 bjh21 /*
1242 1.1 bjh21 * Place initial small packet/header at end of mbuf.
1243 1.1 bjh21 */
1244 1.1 bjh21 if (len < m->m_len) {
1245 1.1 bjh21 if (top == 0 && len + max_linkhdr <= m->m_len)
1246 1.1 bjh21 m->m_data += max_linkhdr;
1247 1.1 bjh21 m->m_len = len;
1248 1.1 bjh21 } else
1249 1.1 bjh21 len = m->m_len;
1250 1.1 bjh21 }
1251 1.1 bjh21 if (top == 0) {
1252 1.1 bjh21 /* Make sure the payload is aligned */
1253 1.1 bjh21 caddr_t newdata = (caddr_t)
1254 1.1 bjh21 ALIGN(m->m_data + sizeof(struct ether_header)) -
1255 1.1 bjh21 sizeof(struct ether_header);
1256 1.1 bjh21 len -= newdata - m->m_data;
1257 1.1 bjh21 m->m_len = len;
1258 1.1 bjh21 m->m_data = newdata;
1259 1.1 bjh21 }
1260 1.1 bjh21 ea_readbuf(sc, mtod(m, u_char *),
1261 1.11 bjh21 cp < SEEQ_MAX_BUFFER_SIZE ? cp : cp - sc->sc_rx_bufsize,
1262 1.11 bjh21 len);
1263 1.1 bjh21 cp += len;
1264 1.1 bjh21 *mp = m;
1265 1.1 bjh21 mp = &m->m_next;
1266 1.1 bjh21 totlen -= len;
1267 1.1 bjh21 if (cp == epkt)
1268 1.1 bjh21 cp = addr;
1269 1.1 bjh21 }
1270 1.1 bjh21
1271 1.1 bjh21 return top;
1272 1.1 bjh21 }
1273 1.1 bjh21
1274 1.1 bjh21 /*
1275 1.3 bjh21 * Process an ioctl request. Mostly boilerplate.
1276 1.1 bjh21 */
1277 1.1 bjh21 static int
1278 1.1 bjh21 ea_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1279 1.1 bjh21 {
1280 1.1 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
1281 1.1 bjh21 int s, error = 0;
1282 1.1 bjh21
1283 1.1 bjh21 s = splnet();
1284 1.1 bjh21 switch (cmd) {
1285 1.1 bjh21
1286 1.5 bjh21 default:
1287 1.5 bjh21 error = ether_ioctl(ifp, cmd, data);
1288 1.5 bjh21 if (error == ENETRESET) {
1289 1.1 bjh21 /*
1290 1.5 bjh21 * Multicast list has changed; set the hardware filter
1291 1.5 bjh21 * accordingly.
1292 1.1 bjh21 */
1293 1.5 bjh21 ea_mc_reset(sc);
1294 1.5 bjh21 error = 0;
1295 1.1 bjh21 }
1296 1.1 bjh21 break;
1297 1.1 bjh21 }
1298 1.1 bjh21
1299 1.1 bjh21 splx(s);
1300 1.1 bjh21 return error;
1301 1.1 bjh21 }
1302 1.1 bjh21
1303 1.5 bjh21 /* Must be called at splnet() */
1304 1.11 bjh21
1305 1.5 bjh21 static void
1306 1.5 bjh21 ea_mc_reset(struct seeq8005_softc *sc)
1307 1.5 bjh21 {
1308 1.11 bjh21
1309 1.11 bjh21 switch (sc->sc_variant) {
1310 1.11 bjh21 case SEEQ_8004:
1311 1.11 bjh21 ea_mc_reset_8004(sc);
1312 1.11 bjh21 return;
1313 1.11 bjh21 case SEEQ_8005:
1314 1.11 bjh21 ea_mc_reset_8005(sc);
1315 1.11 bjh21 return;
1316 1.11 bjh21 }
1317 1.11 bjh21 }
1318 1.11 bjh21
1319 1.11 bjh21 static void
1320 1.11 bjh21 ea_mc_reset_8004(struct seeq8005_softc *sc)
1321 1.11 bjh21 {
1322 1.11 bjh21 struct ethercom *ec = &sc->sc_ethercom;
1323 1.11 bjh21 struct ifnet *ifp = &ec->ec_if;
1324 1.11 bjh21 struct ether_multi *enm;
1325 1.11 bjh21 u_int8_t *cp, c;
1326 1.11 bjh21 u_int32_t crc;
1327 1.11 bjh21 int i, len;
1328 1.11 bjh21 struct ether_multistep step;
1329 1.11 bjh21 u_int8_t af[8];
1330 1.11 bjh21
1331 1.11 bjh21 /*
1332 1.11 bjh21 * Set up multicast address filter by passing all multicast addresses
1333 1.11 bjh21 * through a crc generator, and then using bits 2 - 7 as an index
1334 1.11 bjh21 * into the 64 bit logical address filter. The high order bits
1335 1.11 bjh21 * selects the word, while the rest of the bits select the bit within
1336 1.11 bjh21 * the word.
1337 1.11 bjh21 */
1338 1.11 bjh21
1339 1.11 bjh21 if (ifp->if_flags & IFF_PROMISC) {
1340 1.11 bjh21 ifp->if_flags |= IFF_ALLMULTI;
1341 1.11 bjh21 for (i = 0; i < 8; i++)
1342 1.11 bjh21 af[i] = 0xff;
1343 1.11 bjh21 return;
1344 1.11 bjh21 }
1345 1.11 bjh21 for (i = 0; i < 8; i++)
1346 1.11 bjh21 af[i] = 0;
1347 1.11 bjh21 ETHER_FIRST_MULTI(step, ec, enm);
1348 1.11 bjh21 while (enm != NULL) {
1349 1.11 bjh21 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1350 1.11 bjh21 sizeof(enm->enm_addrlo)) != 0) {
1351 1.11 bjh21 /*
1352 1.11 bjh21 * We must listen to a range of multicast addresses.
1353 1.11 bjh21 * For now, just accept all multicasts, rather than
1354 1.11 bjh21 * trying to set only those filter bits needed to match
1355 1.11 bjh21 * the range. (At this time, the only use of address
1356 1.11 bjh21 * ranges is for IP multicast routing, for which the
1357 1.11 bjh21 * range is big enough to require all bits set.)
1358 1.11 bjh21 */
1359 1.11 bjh21 ifp->if_flags |= IFF_ALLMULTI;
1360 1.11 bjh21 for (i = 0; i < 8; i++)
1361 1.11 bjh21 af[i] = 0xff;
1362 1.11 bjh21 return;
1363 1.11 bjh21 }
1364 1.11 bjh21 cp = enm->enm_addrlo;
1365 1.11 bjh21 crc = 0xffffffff;
1366 1.11 bjh21 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1367 1.11 bjh21 c = *cp++;
1368 1.11 bjh21 for (i = 8; --i >= 0;) {
1369 1.11 bjh21 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1370 1.11 bjh21 crc <<= 1;
1371 1.11 bjh21 crc ^= 0x04c11db6 | 1;
1372 1.11 bjh21 } else
1373 1.11 bjh21 crc <<= 1;
1374 1.11 bjh21 c >>= 1;
1375 1.11 bjh21 }
1376 1.11 bjh21 }
1377 1.11 bjh21 /* Just want the 6 most significant bits. */
1378 1.11 bjh21 crc = (crc >> 2) & 0x3f;
1379 1.11 bjh21
1380 1.11 bjh21 /* Turn on the corresponding bit in the filter. */
1381 1.11 bjh21 af[crc >> 3] |= 1 << (crc & 0x7);
1382 1.11 bjh21
1383 1.11 bjh21 ETHER_NEXT_MULTI(step, enm);
1384 1.11 bjh21 }
1385 1.11 bjh21 ifp->if_flags &= ~IFF_ALLMULTI;
1386 1.11 bjh21
1387 1.11 bjh21 ea_select_buffer(sc, SEEQ_BUFCODE_MULTICAST);
1388 1.11 bjh21 for (i = 0; i < 8; ++i)
1389 1.11 bjh21 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
1390 1.11 bjh21 SEEQ_BUFWIN, af[i]);
1391 1.11 bjh21 }
1392 1.11 bjh21
1393 1.11 bjh21 static void
1394 1.11 bjh21 ea_mc_reset_8005(struct seeq8005_softc *sc)
1395 1.11 bjh21 {
1396 1.5 bjh21 struct ether_multi *enm;
1397 1.5 bjh21 struct ether_multistep step;
1398 1.5 bjh21 int naddr, maxaddrs;
1399 1.5 bjh21
1400 1.5 bjh21 naddr = 0;
1401 1.11 bjh21 maxaddrs = 5;
1402 1.5 bjh21 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1403 1.5 bjh21 while (enm != NULL) {
1404 1.5 bjh21 /* Have we got space? */
1405 1.5 bjh21 if (naddr >= maxaddrs ||
1406 1.5 bjh21 bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
1407 1.5 bjh21 sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
1408 1.5 bjh21 ea_ioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS, NULL);
1409 1.5 bjh21 return;
1410 1.5 bjh21 }
1411 1.11 bjh21 ea_set_address(sc, 1 + naddr, enm->enm_addrlo);
1412 1.11 bjh21 sc->sc_config1 |= SEEQ_CFG1_STATION_ADDR1 << naddr;
1413 1.5 bjh21 naddr++;
1414 1.5 bjh21 ETHER_NEXT_MULTI(step, enm);
1415 1.5 bjh21 }
1416 1.5 bjh21 for (; naddr < maxaddrs; naddr++)
1417 1.11 bjh21 sc->sc_config1 &= ~(SEEQ_CFG1_STATION_ADDR1 << naddr);
1418 1.10 bjh21 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_CONFIG1,
1419 1.5 bjh21 sc->sc_config1);
1420 1.5 bjh21 }
1421 1.5 bjh21
1422 1.1 bjh21 /*
1423 1.1 bjh21 * Device timeout routine.
1424 1.1 bjh21 *
1425 1.1 bjh21 * Ok I am not sure exactly how the device timeout should work....
1426 1.1 bjh21 * Currently what will happens is that that the device timeout is only
1427 1.1 bjh21 * set when a packet it received. This indicates we are on an active
1428 1.1 bjh21 * network and thus we should expect more packets. If non arrive in
1429 1.1 bjh21 * in the timeout period then we reinitialise as we may have jammed.
1430 1.1 bjh21 * We zero the timeout at this point so that we don't end up with
1431 1.1 bjh21 * an endless stream of timeouts if the network goes down.
1432 1.1 bjh21 */
1433 1.1 bjh21
1434 1.1 bjh21 static void
1435 1.1 bjh21 ea_watchdog(struct ifnet *ifp)
1436 1.1 bjh21 {
1437 1.1 bjh21 struct seeq8005_softc *sc = ifp->if_softc;
1438 1.1 bjh21
1439 1.1 bjh21 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1440 1.1 bjh21 ifp->if_oerrors++;
1441 1.1 bjh21 dprintf(("ea_watchdog: "));
1442 1.1 bjh21 dprintf(("st=%04x\n",
1443 1.10 bjh21 bus_space_read_2(sc->sc_iot, sc->sc_ioh, SEEQ_STATUS)));
1444 1.1 bjh21
1445 1.1 bjh21 /* Kick the interface */
1446 1.1 bjh21
1447 1.5 bjh21 ea_init(ifp);
1448 1.1 bjh21
1449 1.10 bjh21 /* ifp->if_timer = SEEQ_TIMEOUT;*/
1450 1.1 bjh21 ifp->if_timer = 0;
1451 1.1 bjh21 }
1452 1.1 bjh21
1453 1.1 bjh21 /* End of if_ea.c */
1454