elink3.c revision 1.38 1 /* $NetBSD: elink3.c,v 1.38 1998/03/04 18:10:03 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Jonathan Stone <jonathan (at) NetBSD.org>
5 * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Herb Peyerl.
19 * 4. The name of Herb Peyerl may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "bpfilter.h"
35 #include "rnd.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <sys/errno.h>
43 #include <sys/syslog.h>
44 #include <sys/select.h>
45 #include <sys/device.h>
46 #if NRND > 0
47 #include <sys/rnd.h>
48 #endif
49
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/if_inarp.h>
61 #endif
62
63 #ifdef NS
64 #include <netns/ns.h>
65 #include <netns/ns_if.h>
66 #endif
67
68 #if NBPFILTER > 0
69 #include <net/bpf.h>
70 #include <net/bpfdesc.h>
71 #endif
72
73 #include <machine/cpu.h>
74 #include <machine/bus.h>
75 #include <machine/intr.h>
76
77 #include <dev/ic/elink3var.h>
78 #include <dev/ic/elink3reg.h>
79
80 #define ETHER_MIN_LEN 64
81 #define ETHER_MAX_LEN 1518
82 #define ETHER_ADDR_LEN 6
83
84 #ifdef DEBUG
85 int epdebug = 0;
86 #endif
87
88 /*
89 * Structure to map media-present bits in boards to
90 * ifmedia codes and printable media names. Used for table-driven
91 * ifmedia initialization.
92 */
93 struct ep_media {
94 int epm_eeprom_data; /* bitmask for eeprom config */
95 int epm_conn; /* sc->ep_connectors code for medium */
96 char* epm_name; /* name of medium */
97 int epm_ifmedia; /* ifmedia word for medium */
98 int epm_ifdata;
99 };
100
101 /*
102 * ep_media table for Vortex/Demon/Boomerang:
103 * map from media-present bits in register RESET_OPTIONS+2
104 * to ifmedia "media words" and printable names.
105 *
106 * XXX indexed directly by INTERNAL_CONFIG default_media field,
107 * (i.e., EPMEDIA_ constants) forcing order of entries.
108 * Note that 3 is reserved.
109 */
110 struct ep_media ep_vortex_media[8] = {
111 { EP_PCI_UTP, EPC_UTP, "utp", IFM_ETHER|IFM_10_T,
112 EPMEDIA_10BASE_T },
113 { EP_PCI_AUI, EPC_AUI, "aui", IFM_ETHER|IFM_10_5,
114 EPMEDIA_AUI },
115 { 0, 0, "reserved", IFM_NONE, EPMEDIA_RESV1 },
116 { EP_PCI_BNC, EPC_BNC, "bnc", IFM_ETHER|IFM_10_2,
117 EPMEDIA_10BASE_2 },
118 { EP_PCI_100BASE_TX, EPC_100TX, "100-TX", IFM_ETHER|IFM_100_TX,
119 EPMEDIA_100BASE_TX },
120 { EP_PCI_100BASE_FX, EPC_100FX, "100-FX", IFM_ETHER|IFM_100_FX,
121 EPMEDIA_100BASE_FX },
122 { EP_PCI_100BASE_MII,EPC_MII, "mii", IFM_ETHER|IFM_100_TX,
123 EPMEDIA_MII },
124 { EP_PCI_100BASE_T4, EPC_100T4, "100-T4", IFM_ETHER|IFM_100_T4,
125 EPMEDIA_100BASE_T4 }
126 };
127
128 /*
129 * ep_media table for 3c509/3c509b/3c579/3c589:
130 * map from media-present bits in register CNFG_CNTRL
131 * (window 0, offset ?) to ifmedia "media words" and printable names.
132 */
133 struct ep_media ep_isa_media[3] = {
134 { EP_W0_CC_UTP, EPC_UTP, "utp", IFM_ETHER|IFM_10_T, EPMEDIA_10BASE_T },
135 { EP_W0_CC_AUI, EPC_AUI, "aui", IFM_ETHER|IFM_10_5, EPMEDIA_AUI },
136 { EP_W0_CC_BNC, EPC_BNC, "bnc", IFM_ETHER|IFM_10_2, EPMEDIA_10BASE_2 },
137 };
138
139 /* Map vortex reset_options bits to if_media codes. */
140 const u_int ep_default_to_media[8] = {
141 IFM_ETHER | IFM_10_T,
142 IFM_ETHER | IFM_10_5,
143 0, /* reserved by 3Com */
144 IFM_ETHER | IFM_10_2,
145 IFM_ETHER | IFM_100_TX,
146 IFM_ETHER | IFM_100_FX,
147 IFM_ETHER | IFM_100_TX, /* XXX really MII: need to talk to PHY */
148 IFM_ETHER | IFM_100_T4,
149 };
150
151 void ep_internalconfig __P((struct ep_softc *sc));
152 void ep_vortex_probemedia __P((struct ep_softc *sc));
153 void ep_isa_probemedia __P((struct ep_softc *sc));
154
155 static void eptxstat __P((struct ep_softc *));
156 static int epstatus __P((struct ep_softc *));
157 void epinit __P((struct ep_softc *));
158 int epioctl __P((struct ifnet *, u_long, caddr_t));
159 void epstart __P((struct ifnet *));
160 void epwatchdog __P((struct ifnet *));
161 void epreset __P((struct ep_softc *));
162 static void epshutdown __P((void *));
163 void epread __P((struct ep_softc *));
164 struct mbuf *epget __P((struct ep_softc *, int));
165 void epmbuffill __P((void *));
166 void epmbufempty __P((struct ep_softc *));
167 void epsetfilter __P((struct ep_softc *));
168 void epsetmedia __P((struct ep_softc *, int epmedium));
169
170 int epenable __P((struct ep_softc *));
171 void epdisable __P((struct ep_softc *));
172
173 /* ifmedia callbacks */
174 int ep_media_change __P((struct ifnet *ifp));
175 void ep_media_status __P((struct ifnet *ifp, struct ifmediareq *req));
176
177 static int epbusyeeprom __P((struct ep_softc *));
178 static inline void ep_complete_cmd __P((struct ep_softc *sc,
179 u_int cmd, u_int arg));
180
181
182 /*
183 * Issue a (reset) command, and be sure it has completed.
184 * Used for commands that reset part or all of the board.
185 * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
186 * but older hardware doesn't implement it and we must delay.
187 * It's easiest to just delay always.
188 */
189 static inline void
190 ep_complete_cmd(sc, cmd, arg)
191 struct ep_softc *sc;
192 u_int cmd, arg;
193 {
194 register bus_space_tag_t iot = sc->sc_iot;
195 register bus_space_handle_t ioh = sc->sc_ioh;
196
197 bus_space_write_2(iot, ioh, cmd, arg);
198
199 #ifdef notyet
200 /* if this adapter family has S_COMMAND_IN_PROGRESS, use it */
201 while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
202 ;
203 else
204 #else
205 DELAY(100000); /* need at least 1 ms, but be generous. */
206 #endif
207 }
208
209 /*
210 * Back-end attach and configure.
211 */
212 void
213 epconfig(sc, chipset, enaddr)
214 struct ep_softc *sc;
215 u_short chipset;
216 u_int8_t *enaddr;
217 {
218 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
219 bus_space_tag_t iot = sc->sc_iot;
220 bus_space_handle_t ioh = sc->sc_ioh;
221 u_int16_t i;
222 u_int8_t myla[6];
223
224 sc->ep_chipset = chipset;
225
226 /*
227 * We could have been groveling around in other register
228 * windows in the front-end; make sure we're in window 0
229 * to read the EEPROM.
230 */
231 GO_WINDOW(0);
232
233 if (enaddr == NULL) {
234 /*
235 * Read the station address from the eeprom
236 */
237 for (i = 0; i < 3; i++) {
238 u_int16_t x;
239 if (epbusyeeprom(sc))
240 return; /* XXX why is eeprom busy? */
241 bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
242 READ_EEPROM | i);
243 if (epbusyeeprom(sc))
244 return; /* XXX why is eeprom busy? */
245 x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
246 myla[(i << 1)] = x >> 8;
247 myla[(i << 1) + 1] = x;
248 }
249 enaddr = myla;
250 }
251
252 printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
253 ether_sprintf(enaddr));
254
255 /*
256 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900,3c515?) cards
257 * allow FDDI-sized (4500) byte packets. Commands only take an
258 * 11-bit parameter, and 11 bits isn't enough to hold a full-size
259 * packet length.
260 * Commands to these cards implicitly upshift a packet size
261 * or threshold by 2 bits.
262 * To detect cards with large-packet support, we probe by setting
263 * the transmit threshold register, then change windows and
264 * read back the threshold register directly, and see if the
265 * threshold value was shifted or not.
266 */
267 bus_space_write_2(iot, ioh, EP_COMMAND,
268 SET_TX_AVAIL_THRESH | EP_LARGEWIN_PROBE );
269 GO_WINDOW(5);
270 i = bus_space_read_2(iot, ioh, EP_W5_TX_AVAIL_THRESH);
271 GO_WINDOW(1);
272 switch (i) {
273 case EP_LARGEWIN_PROBE:
274 case (EP_LARGEWIN_PROBE & EP_LARGEWIN_MASK):
275 sc->ep_pktlenshift = 0;
276 break;
277
278 case (EP_LARGEWIN_PROBE << 2):
279 sc->ep_pktlenshift = 2;
280 /* XXX does the 3c515 support Vortex-style RESET_OPTIONS? */
281 break;
282
283 default:
284 printf("%s: wrote 0x%x to TX_AVAIL_THRESH, read back 0x%x. "
285 "Interface disabled\n",
286 sc->sc_dev.dv_xname, EP_LARGEWIN_PROBE, (int) i);
287 return;
288 }
289
290 /*
291 * Ensure Tx-available interrupts are enabled for
292 * start the interface.
293 * XXX should be in epinit()?
294 */
295 bus_space_write_2(iot, ioh, EP_COMMAND,
296 SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
297
298 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
299 ifp->if_softc = sc;
300 ifp->if_start = epstart;
301 ifp->if_ioctl = epioctl;
302 ifp->if_watchdog = epwatchdog;
303 ifp->if_flags =
304 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
305
306 if_attach(ifp);
307 ether_ifattach(ifp, enaddr);
308
309 /*
310 * Finish configuration:
311 * determine chipset if the front-end couldn't do so,
312 * show board details, set media.
313 */
314
315 /* print RAM size */
316 ep_internalconfig(sc);
317 GO_WINDOW(0);
318
319 ifmedia_init(&sc->sc_media, 0, ep_media_change, ep_media_status);
320
321 /*
322 * If we've got an indirect (ISA) board, the chipset is
323 * unknown. If the board has large-packet support, it's a
324 * Vortex/Boomerang, otherwise it's a 3c509. XXX use eeprom
325 * capability word instead?
326 */
327
328 if (sc->ep_chipset == EP_CHIPSET_UNKNOWN && sc->ep_pktlenshift) {
329 printf("warning: unknown chipset, possibly 3c515?\n");
330 #ifdef notyet
331 sc->sc_chipset = EP_CHIPSET_VORTEX;
332 #endif /* notyet */
333 }
334
335 /*
336 * Ascertain which media types are present and inform ifmedia.
337 */
338 switch (sc->ep_chipset) {
339 /* on a direct bus, the attach routine can tell, but check anyway. */
340 case EP_CHIPSET_VORTEX:
341 case EP_CHIPSET_BOOMERANG2:
342 ep_vortex_probemedia(sc);
343 break;
344
345 /* on ISA we can't yet tell 3c509 from 3c515. Assume the former. */
346 case EP_CHIPSET_3C509:
347 default:
348 ep_isa_probemedia(sc);
349 break;
350 }
351
352 GO_WINDOW(1); /* Window 1 is operating window */
353
354 #if NBPFILTER > 0
355 bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
356 sizeof(struct ether_header));
357 #endif
358
359 #if NRND > 0
360 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
361 #endif
362
363 sc->tx_start_thresh = 20; /* probably a good starting point. */
364
365 /* Establish callback to reset card when we reboot. */
366 shutdownhook_establish(epshutdown, sc);
367
368 ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
369 ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
370 }
371
372
373 /*
374 * Show interface-model-independent info from window 3
375 * internal-configuration register.
376 */
377 void
378 ep_internalconfig(sc)
379 struct ep_softc *sc;
380 {
381 bus_space_tag_t iot = sc->sc_iot;
382 bus_space_handle_t ioh = sc->sc_ioh;
383
384 u_int config0;
385 u_int config1;
386
387 int ram_size, ram_width, ram_speed, rom_size, ram_split;
388 /*
389 * NVRAM buffer Rx:Tx config names for busmastering cards
390 * (Demon, Vortex, and later).
391 */
392 const char *onboard_ram_config[] = {
393 "5:3", "3:1", "1:1", "3:5" };
394
395 GO_WINDOW(3);
396 config0 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
397 config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
398 GO_WINDOW(0);
399
400 ram_size = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
401 ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
402 ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
403 rom_size = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
404
405 ram_split = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
406
407 printf("%s: %dKB %s-wide FIFO, %s Rx:Tx split, ",
408 sc->sc_dev.dv_xname,
409 8 << ram_size,
410 (ram_width) ? "word" : "byte",
411 onboard_ram_config[ram_split]);
412 }
413
414
415 /*
416 * Find supported media on 3c509-generation hardware that doesn't have
417 * a "reset_options" register in window 3.
418 * Use the config_cntrl register in window 0 instead.
419 * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
420 * that implement CONFIG_CTRL. We don't have a good way to set the
421 * default active mediuim; punt to ifconfig instead.
422 *
423 * XXX what about 3c515, pcmcia 10/100?
424 */
425 void
426 ep_isa_probemedia(sc)
427 struct ep_softc *sc;
428 {
429 bus_space_tag_t iot = sc->sc_iot;
430 bus_space_handle_t ioh = sc->sc_ioh;
431 struct ifmedia *ifm = &sc->sc_media;
432 int conn, i;
433 u_int16_t ep_w0_config, port;
434
435 conn = 0;
436 GO_WINDOW(0);
437 ep_w0_config = bus_space_read_2(iot, ioh, EP_W0_CONFIG_CTRL);
438 for (i = 0; i < 3; i++) {
439 struct ep_media * epm = ep_isa_media + i;
440
441 if ((ep_w0_config & epm->epm_eeprom_data) != 0) {
442
443 ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
444 if (conn)
445 printf("/");
446 printf(epm->epm_name);
447 conn |= epm->epm_conn;
448 }
449 }
450 sc->ep_connectors = conn;
451
452 /* get default medium from EEPROM */
453 if (epbusyeeprom(sc))
454 return; /* XXX why is eeprom busy? */
455 bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
456 READ_EEPROM | EEPROM_ADDR_CFG);
457 if (epbusyeeprom(sc))
458 return; /* XXX why is eeprom busy? */
459 port = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
460 port = port >> 14;
461
462 printf(" (default %s)\n", ep_vortex_media[port].epm_name);
463 /* tell ifconfig what currently-active media is. */
464 ifmedia_set(ifm, ep_default_to_media[port]);
465
466 /* XXX autoselect not yet implemented */
467 }
468
469
470 /*
471 * Find media present on large-packet-capable elink3 devices.
472 * Show onboard configuration of large-packet-capable elink3 devices
473 * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
474 * Use media and card-version info in window 3 instead.
475 *
476 * XXX how much of this works with 3c515, pcmcia 10/100?
477 */
478 void
479 ep_vortex_probemedia(sc)
480 struct ep_softc *sc;
481 {
482 bus_space_tag_t iot = sc->sc_iot;
483 bus_space_handle_t ioh = sc->sc_ioh;
484 struct ifmedia *ifm = &sc->sc_media;
485 u_int config1, conn;
486 int reset_options;
487 int default_media; /* 3-bit encoding of default (EEPROM) media */
488 int autoselect; /* boolean: should default to autoselect */
489 const char *medium_name;
490 register int i;
491
492 GO_WINDOW(3);
493 config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
494 reset_options = (int)bus_space_read_1(iot, ioh, EP_W3_RESET_OPTIONS);
495 GO_WINDOW(0);
496
497 default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
498 autoselect = (config1 & CONFIG_AUTOSELECT) >> CONFIG_AUTOSELECT_SHIFT;
499
500 /* set available media options */
501 conn = 0;
502 for (i = 0; i < 8; i++) {
503 struct ep_media * epm = ep_vortex_media + i;
504
505 if ((reset_options & epm->epm_eeprom_data) != 0) {
506 if (conn) printf("/");
507 printf(epm->epm_name);
508 conn |= epm->epm_conn;
509 ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
510 }
511 }
512
513 sc->ep_connectors = conn;
514
515 /* Show eeprom's idea of default media. */
516 medium_name = (default_media > 8)
517 ? "(unknown/impossible media)"
518 : ep_vortex_media[default_media].epm_name;
519 printf(" default %s%s\n",
520 medium_name, (autoselect)? ", autoselect" : "" );
521
522 #ifdef notyet
523 /*
524 * Set default: either the active interface the card
525 * reads from the EEPROM, or if autoselect is true,
526 * whatever we find is actually connected.
527 *
528 * XXX autoselect not yet implemented.
529 */
530 #endif /* notyet */
531
532 /* tell ifconfig what currently-active media is. */
533 ifmedia_set(ifm, ep_default_to_media[default_media]);
534 }
535
536
537 /*
538 * Bring device up.
539 *
540 * The order in here seems important. Otherwise we may not receive
541 * interrupts. ?!
542 */
543 void
544 epinit(sc)
545 register struct ep_softc *sc;
546 {
547 register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
548 bus_space_tag_t iot = sc->sc_iot;
549 bus_space_handle_t ioh = sc->sc_ioh;
550 int i;
551
552 while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
553 ;
554
555 if (sc->bustype != EP_BUS_PCI) {
556 GO_WINDOW(0);
557 bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
558 bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
559 }
560
561 if (sc->bustype == EP_BUS_PCMCIA) {
562 bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
563 }
564
565 GO_WINDOW(2);
566 for (i = 0; i < 6; i++) /* Reload the ether_addr. */
567 bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
568 LLADDR(ifp->if_sadl)[i]);
569
570 /*
571 * Reset the station-address receive filter.
572 * A bug workaround for busmastering (Vortex, Demon) cards.
573 */
574 for (i = 0; i < 6; i++)
575 bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
576
577 ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
578 ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
579
580 GO_WINDOW(1); /* Window 1 is operating window */
581 for (i = 0; i < 31; i++)
582 bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
583
584 /* Set threshhold for for Tx-space avaiable interrupt. */
585 bus_space_write_2(iot, ioh, EP_COMMAND,
586 SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
587
588 /* Enable interrupts. */
589 bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
590 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
591 bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
592 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
593
594 /*
595 * Attempt to get rid of any stray interrupts that occured during
596 * configuration. On the i386 this isn't possible because one may
597 * already be queued. However, a single stray interrupt is
598 * unimportant.
599 */
600 bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
601
602 epsetfilter(sc);
603 epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
604
605 bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
606 bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
607
608 epmbuffill(sc);
609
610 /* Interface is now `running', with no output active. */
611 ifp->if_flags |= IFF_RUNNING;
612 ifp->if_flags &= ~IFF_OACTIVE;
613
614 /* Attempt to start output, if any. */
615 epstart(ifp);
616 }
617
618
619 /*
620 * Set multicast receive filter.
621 * elink3 hardware has no selective multicast filter in hardware.
622 * Enable reception of all multicasts and filter in software.
623 */
624 void
625 epsetfilter(sc)
626 register struct ep_softc *sc;
627 {
628 register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
629
630 GO_WINDOW(1); /* Window 1 is operating window */
631 bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
632 FIL_INDIVIDUAL | FIL_BRDCST |
633 ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
634 ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
635 }
636
637
638 int
639 ep_media_change(ifp)
640 struct ifnet *ifp;
641 {
642 register struct ep_softc *sc = ifp->if_softc;
643
644 /*
645 * If the interface is not currently powered on, just return.
646 * When it is enabled later, epinit() will properly set up the
647 * media for us.
648 */
649 if (sc->enabled == 0)
650 return (0);
651
652 epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
653 return (0);
654 }
655
656 /*
657 * Set active media to a specific given EPMEDIA_<> value.
658 * For vortex/demon/boomerang cards, update media field in w3_internal_config,
659 * and power on selected transceiver.
660 * For 3c509-generation cards (3c509/3c579/3c589/3c509B),
661 * update media field in w0_address_config, and power on selected xcvr.
662 */
663 void
664 epsetmedia(sc, medium)
665 register struct ep_softc *sc;
666 int medium;
667 {
668 bus_space_tag_t iot = sc->sc_iot;
669 bus_space_handle_t ioh = sc->sc_ioh;
670 int w4_media;
671
672 /*
673 * First, change the media-control bits in EP_W4_MEDIA_TYPE.
674 */
675
676 /* Turn everything off. First turn off linkbeat and UTP. */
677 GO_WINDOW(4);
678 w4_media = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
679 w4_media = w4_media & ~(ENABLE_UTP|SQE_ENABLE);
680 bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, w4_media);
681
682 /* Turn off coax */
683 bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
684 delay(1000);
685
686 /*
687 * Now turn on the selected media/transceiver.
688 */
689 GO_WINDOW(4);
690 switch (medium) {
691 case EPMEDIA_10BASE_T:
692 bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
693 w4_media | ENABLE_UTP);
694 break;
695
696 case EPMEDIA_10BASE_2:
697 bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
698 DELAY(1000); /* 50ms not enmough? */
699 break;
700
701 /* XXX following only for new-generation cards */
702 case EPMEDIA_100BASE_TX:
703 case EPMEDIA_100BASE_FX:
704 case EPMEDIA_100BASE_T4: /* XXX check documentation */
705 bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
706 w4_media | LINKBEAT_ENABLE);
707 DELAY(1000); /* not strictly necessary? */
708 break;
709
710 case EPMEDIA_AUI:
711 bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
712 w4_media | SQE_ENABLE);
713 DELAY(1000); /* not strictly necessary? */
714 break;
715 case EPMEDIA_MII:
716 /* XXX talk to phy? */
717 break;
718 default:
719 #if defined(DEBUG)
720 printf("%s unknown media 0x%x\n", sc->sc_dev.dv_xname, medium);
721 #endif
722 break;
723
724 }
725
726 /*
727 * Tell the chip which PHY [sic] to use.
728 */
729 if (sc->ep_chipset==EP_CHIPSET_VORTEX ||
730 sc->ep_chipset==EP_CHIPSET_BOOMERANG2) {
731 int config0, config1;
732
733 GO_WINDOW(3);
734 config0 = (u_int)bus_space_read_2(iot, ioh,
735 EP_W3_INTERNAL_CONFIG);
736 config1 = (u_int)bus_space_read_2(iot, ioh,
737 EP_W3_INTERNAL_CONFIG + 2);
738
739 #if defined(DEBUG)
740 if (epdebug) {
741 printf("%s: read 0x%x, 0x%x from EP_W3_CONFIG register\n",
742 sc->sc_dev.dv_xname, config0, config1);
743 }
744 #endif
745 config1 = config1 & ~CONFIG_MEDIAMASK;
746 config1 |= (medium << CONFIG_MEDIAMASK_SHIFT);
747
748 #if defined(DEBUG)
749 if (epdebug) {
750 printf("epsetmedia: %s: medium 0x%x, 0x%x to EP_W3_CONFIG\n",
751 sc->sc_dev.dv_xname, medium, config1);
752 }
753 #endif
754 bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG, config0);
755 bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2, config1);
756 }
757 else if (sc->ep_chipset == EP_CHIPSET_3C509) {
758 register int w0_addr_cfg;
759
760 GO_WINDOW(0);
761 w0_addr_cfg = bus_space_read_2(iot, ioh, EP_W0_ADDRESS_CFG);
762 w0_addr_cfg &= 0x3fff;
763 bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,
764 w0_addr_cfg | (medium << 14));
765 DELAY(1000);
766 }
767
768 GO_WINDOW(1); /* Window 1 is operating window */
769 }
770
771 /*
772 * Get currently-selected media from card.
773 * (if_media callback, may be called before interface is brought up).
774 */
775 void
776 ep_media_status(ifp, req)
777 struct ifnet *ifp;
778 struct ifmediareq *req;
779 {
780 register struct ep_softc *sc = ifp->if_softc;
781 bus_space_tag_t iot = sc->sc_iot;
782 bus_space_handle_t ioh = sc->sc_ioh;
783 u_int config1;
784 u_int ep_mediastatus;
785
786 if (sc->enabled == 0) {
787 req->ifm_active = IFM_ETHER|IFM_NONE;
788 req->ifm_status = 0;
789 return;
790 }
791
792 /* XXX read from softc when we start autosensing media */
793 req->ifm_active = sc->sc_media.ifm_cur->ifm_media;
794
795 switch (sc->ep_chipset) {
796 case EP_CHIPSET_VORTEX:
797 case EP_CHIPSET_BOOMERANG:
798 GO_WINDOW(3);
799 delay(5000);
800
801 config1 = bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
802 GO_WINDOW(1);
803
804 config1 =
805 (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
806 req->ifm_active = ep_default_to_media[config1];
807
808 /* XXX check full-duplex bits? */
809
810 GO_WINDOW(4);
811 req->ifm_status = IFM_AVALID; /* XXX */
812 ep_mediastatus = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
813 if (ep_mediastatus & LINKBEAT_DETECT)
814 req->ifm_status |= IFM_ACTIVE; /* XXX automedia */
815
816 break;
817
818 case EP_CHIPSET_UNKNOWN:
819 case EP_CHIPSET_3C509:
820 req->ifm_status = 0; /* XXX */
821 break;
822
823 default:
824 printf("%s: media_status on unknown chipset 0x%x\n",
825 ifp->if_xname, sc->ep_chipset);
826 break;
827 }
828
829 /* XXX look for softc heartbeat for other chips or media */
830
831 GO_WINDOW(1);
832 }
833
834
835
836 /*
837 * Start outputting on the interface.
838 * Always called as splnet().
839 */
840 void
841 epstart(ifp)
842 struct ifnet *ifp;
843 {
844 register struct ep_softc *sc = ifp->if_softc;
845 bus_space_tag_t iot = sc->sc_iot;
846 bus_space_handle_t ioh = sc->sc_ioh;
847 struct mbuf *m, *m0;
848 int sh, len, pad;
849
850 /* Don't transmit if interface is busy or not running */
851 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
852 return;
853
854 startagain:
855 /* Sneak a peek at the next packet */
856 m0 = ifp->if_snd.ifq_head;
857 if (m0 == 0)
858 return;
859
860 /* We need to use m->m_pkthdr.len, so require the header */
861 if ((m0->m_flags & M_PKTHDR) == 0)
862 panic("epstart: no header mbuf");
863 len = m0->m_pkthdr.len;
864
865 pad = (4 - len) & 3;
866
867 /*
868 * The 3c509 automatically pads short packets to minimum ethernet
869 * length, but we drop packets that are too large. Perhaps we should
870 * truncate them instead?
871 */
872 if (len + pad > ETHER_MAX_LEN) {
873 /* packet is obviously too large: toss it */
874 ++ifp->if_oerrors;
875 IF_DEQUEUE(&ifp->if_snd, m0);
876 m_freem(m0);
877 goto readcheck;
878 }
879
880 if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
881 bus_space_write_2(iot, ioh, EP_COMMAND,
882 SET_TX_AVAIL_THRESH |
883 ((len + pad + 4) >> sc->ep_pktlenshift));
884 /* not enough room in FIFO */
885 ifp->if_flags |= IFF_OACTIVE;
886 return;
887 } else {
888 bus_space_write_2(iot, ioh, EP_COMMAND,
889 SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE );
890 }
891
892 IF_DEQUEUE(&ifp->if_snd, m0);
893 if (m0 == 0) /* not really needed */
894 return;
895
896 bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
897 ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/) );
898
899 #if NBPFILTER > 0
900 if (ifp->if_bpf)
901 bpf_mtap(ifp->if_bpf, m0);
902 #endif
903
904 /*
905 * Do the output at splhigh() so that an interrupt from another device
906 * won't cause a FIFO underrun.
907 */
908 sh = splhigh();
909
910 bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
911 bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
912 0xffff); /* Second dword meaningless */
913 if (EP_IS_BUS_32(sc->bustype)) {
914 for (m = m0; m; ) {
915 if (m->m_len > 3) {
916 /* align our reads from core */
917 if (mtod(m, u_long) & 3) {
918 u_long count =
919 4 - (mtod(m, u_long) & 3);
920 bus_space_write_multi_1(iot, ioh,
921 EP_W1_TX_PIO_WR_1,
922 mtod(m, u_int8_t *), count);
923 m->m_data =
924 (void *)(mtod(m, u_long) + count);
925 m->m_len -= count;
926 }
927 bus_space_write_multi_4(iot, ioh,
928 EP_W1_TX_PIO_WR_1,
929 mtod(m, u_int32_t *), m->m_len >> 2);
930 m->m_data = (void *)(mtod(m, u_long) +
931 (u_long)(m->m_len & ~3));
932 m->m_len -= m->m_len & ~3;
933 }
934 if (m->m_len) {
935 bus_space_write_multi_1(iot, ioh,
936 EP_W1_TX_PIO_WR_1,
937 mtod(m, u_int8_t *), m->m_len);
938 }
939 MFREE(m, m0);
940 m = m0;
941 }
942 } else {
943 for (m = m0; m; ) {
944 if (m->m_len > 1) {
945 if (mtod(m, u_long) & 1) {
946 bus_space_write_1(iot, ioh,
947 EP_W1_TX_PIO_WR_1,
948 *(mtod(m, u_int8_t *)));
949 m->m_data =
950 (void *)(mtod(m, u_long) + 1);
951 m->m_len -= 1;
952 }
953 bus_space_write_multi_2(iot, ioh,
954 EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
955 m->m_len >> 1);
956 }
957 if (m->m_len & 1) {
958 bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
959 *(mtod(m, u_int8_t *) + m->m_len - 1));
960 }
961 MFREE(m, m0);
962 m = m0;
963 }
964 }
965 while (pad--)
966 bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
967
968 splx(sh);
969
970 ++ifp->if_opackets;
971
972 readcheck:
973 if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
974 /* We received a complete packet. */
975 u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
976
977 if ((status & S_INTR_LATCH) == 0) {
978 /*
979 * No interrupt, read the packet and continue
980 * Is this supposed to happen? Is my motherboard
981 * completely busted?
982 */
983 epread(sc);
984 } else {
985 /* Got an interrupt, return so that it gets serviced. */
986 #if 0
987 printf("%s: S_INTR_LATCH %04x mask=%04x ipending=%04x (%04x)\n",
988 sc->sc_dev.dv_xname, status,
989 cpl, ipending, imask[IPL_NET]);
990 #endif
991
992 return;
993 }
994 } else {
995 /* Check if we are stuck and reset [see XXX comment] */
996 if (epstatus(sc)) {
997 if (ifp->if_flags & IFF_DEBUG)
998 printf("%s: adapter reset\n",
999 sc->sc_dev.dv_xname);
1000 epreset(sc);
1001 }
1002 }
1003
1004 goto startagain;
1005 }
1006
1007
1008 /*
1009 * XXX: The 3c509 card can get in a mode where both the fifo status bit
1010 * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
1011 * We detect this situation and we reset the adapter.
1012 * It happens at times when there is a lot of broadcast traffic
1013 * on the cable (once in a blue moon).
1014 */
1015 static int
1016 epstatus(sc)
1017 register struct ep_softc *sc;
1018 {
1019 bus_space_tag_t iot = sc->sc_iot;
1020 bus_space_handle_t ioh = sc->sc_ioh;
1021 u_int16_t fifost;
1022
1023 /*
1024 * Check the FIFO status and act accordingly
1025 */
1026 GO_WINDOW(4);
1027 fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
1028 GO_WINDOW(1);
1029
1030 if (fifost & FIFOS_RX_UNDERRUN) {
1031 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1032 printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
1033 epreset(sc);
1034 return 0;
1035 }
1036
1037 if (fifost & FIFOS_RX_STATUS_OVERRUN) {
1038 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1039 printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
1040 return 1;
1041 }
1042
1043 if (fifost & FIFOS_RX_OVERRUN) {
1044 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1045 printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
1046 return 1;
1047 }
1048
1049 if (fifost & FIFOS_TX_OVERRUN) {
1050 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1051 printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
1052 epreset(sc);
1053 return 0;
1054 }
1055
1056 return 0;
1057 }
1058
1059
1060 static void
1061 eptxstat(sc)
1062 register struct ep_softc *sc;
1063 {
1064 bus_space_tag_t iot = sc->sc_iot;
1065 bus_space_handle_t ioh = sc->sc_ioh;
1066 int i;
1067
1068 /*
1069 * We need to read+write TX_STATUS until we get a 0 status
1070 * in order to turn off the interrupt flag.
1071 */
1072 while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
1073 bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
1074
1075 if (i & TXS_JABBER) {
1076 ++sc->sc_ethercom.ec_if.if_oerrors;
1077 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1078 printf("%s: jabber (%x)\n",
1079 sc->sc_dev.dv_xname, i);
1080 epreset(sc);
1081 } else if (i & TXS_UNDERRUN) {
1082 ++sc->sc_ethercom.ec_if.if_oerrors;
1083 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1084 printf("%s: fifo underrun (%x) @%d\n",
1085 sc->sc_dev.dv_xname, i,
1086 sc->tx_start_thresh);
1087 if (sc->tx_succ_ok < 100)
1088 sc->tx_start_thresh = min(ETHER_MAX_LEN,
1089 sc->tx_start_thresh + 20);
1090 sc->tx_succ_ok = 0;
1091 epreset(sc);
1092 } else if (i & TXS_MAX_COLLISION) {
1093 ++sc->sc_ethercom.ec_if.if_collisions;
1094 bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
1095 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1096 } else
1097 sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
1098 }
1099 }
1100
1101 int
1102 epintr(arg)
1103 void *arg;
1104 {
1105 register struct ep_softc *sc = arg;
1106 bus_space_tag_t iot = sc->sc_iot;
1107 bus_space_handle_t ioh = sc->sc_ioh;
1108 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1109 u_int16_t status;
1110 int ret = 0;
1111 int addrandom = 0;
1112
1113 if (sc->enabled == 0)
1114 return (0);
1115
1116 for (;;) {
1117 bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
1118
1119 status = bus_space_read_2(iot, ioh, EP_STATUS);
1120
1121 if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
1122 S_RX_COMPLETE | S_CARD_FAILURE)) == 0) {
1123 if ((status & S_INTR_LATCH) == 0) {
1124 #if 0
1125 printf("%s: intr latch cleared\n",
1126 sc->sc_dev.dv_xname);
1127 #endif
1128 break;
1129 }
1130 }
1131
1132 ret = 1;
1133
1134 /*
1135 * Acknowledge any interrupts. It's important that we do this
1136 * first, since there would otherwise be a race condition.
1137 * Due to the i386 interrupt queueing, we may get spurious
1138 * interrupts occasionally.
1139 */
1140 bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR |
1141 (status & (C_INTR_LATCH |
1142 C_CARD_FAILURE |
1143 C_TX_COMPLETE |
1144 C_TX_AVAIL |
1145 C_RX_COMPLETE |
1146 C_RX_EARLY |
1147 C_INT_RQD |
1148 C_UPD_STATS)));
1149
1150 #if 0
1151 status = bus_space_read_2(iot, ioh, EP_STATUS);
1152
1153 printf("%s: intr%s%s%s%s\n", sc->sc_dev.dv_xname,
1154 (status & S_RX_COMPLETE)?" RX_COMPLETE":"",
1155 (status & S_TX_COMPLETE)?" TX_COMPLETE":"",
1156 (status & S_TX_AVAIL)?" TX_AVAIL":"",
1157 (status & S_CARD_FAILURE)?" CARD_FAILURE":"");
1158 #endif
1159
1160 if (status & S_RX_COMPLETE) {
1161 epread(sc);
1162 addrandom = 1;
1163 }
1164 if (status & S_TX_AVAIL) {
1165 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1166 epstart(&sc->sc_ethercom.ec_if);
1167 addrandom = 1;
1168 }
1169 if (status & S_CARD_FAILURE) {
1170 printf("%s: adapter failure (%x)\n",
1171 sc->sc_dev.dv_xname, status);
1172 epreset(sc);
1173 return (1);
1174 }
1175 if (status & S_TX_COMPLETE) {
1176 eptxstat(sc);
1177 epstart(ifp);
1178 addrandom = 1;
1179 }
1180
1181 #if NRND > 0
1182 if (status)
1183 rnd_add_uint32(&sc->rnd_source, status);
1184 #endif
1185 }
1186
1187 /* no more interrupts */
1188 return (ret);
1189 }
1190
1191 void
1192 epread(sc)
1193 register struct ep_softc *sc;
1194 {
1195 bus_space_tag_t iot = sc->sc_iot;
1196 bus_space_handle_t ioh = sc->sc_ioh;
1197 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1198 struct mbuf *m;
1199 struct ether_header *eh;
1200 int len;
1201
1202 len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
1203
1204 again:
1205 if (ifp->if_flags & IFF_DEBUG) {
1206 int err = len & ERR_MASK;
1207 char *s = NULL;
1208
1209 if (len & ERR_INCOMPLETE)
1210 s = "incomplete packet";
1211 else if (err == ERR_OVERRUN)
1212 s = "packet overrun";
1213 else if (err == ERR_RUNT)
1214 s = "runt packet";
1215 else if (err == ERR_ALIGNMENT)
1216 s = "bad alignment";
1217 else if (err == ERR_CRC)
1218 s = "bad crc";
1219 else if (err == ERR_OVERSIZE)
1220 s = "oversized packet";
1221 else if (err == ERR_DRIBBLE)
1222 s = "dribble bits";
1223
1224 if (s)
1225 printf("%s: %s\n", sc->sc_dev.dv_xname, s);
1226 }
1227
1228 if (len & ERR_INCOMPLETE)
1229 return;
1230
1231 if (len & ERR_RX) {
1232 ++ifp->if_ierrors;
1233 goto abort;
1234 }
1235
1236 len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */
1237
1238 /* Pull packet off interface. */
1239 m = epget(sc, len);
1240 if (m == 0) {
1241 ifp->if_ierrors++;
1242 goto abort;
1243 }
1244
1245 ++ifp->if_ipackets;
1246
1247 /* We assume the header fit entirely in one mbuf. */
1248 eh = mtod(m, struct ether_header *);
1249
1250 #if NBPFILTER > 0
1251 /*
1252 * Check if there's a BPF listener on this interface.
1253 * If so, hand off the raw packet to BPF.
1254 */
1255 if (ifp->if_bpf) {
1256 bpf_mtap(ifp->if_bpf, m);
1257
1258 /*
1259 * Note that the interface cannot be in promiscuous mode if
1260 * there are no BPF listeners. And if we are in promiscuous
1261 * mode, we have to check if this packet is really ours.
1262 */
1263 if ((ifp->if_flags & IFF_PROMISC) &&
1264 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1265 bcmp(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl),
1266 sizeof(eh->ether_dhost)) != 0) {
1267 m_freem(m);
1268 return;
1269 }
1270 }
1271 #endif
1272
1273 /* We assume the header fit entirely in one mbuf. */
1274 m_adj(m, sizeof(struct ether_header));
1275 ether_input(ifp, eh, m);
1276
1277 /*
1278 * In periods of high traffic we can actually receive enough
1279 * packets so that the fifo overrun bit will be set at this point,
1280 * even though we just read a packet. In this case we
1281 * are not going to receive any more interrupts. We check for
1282 * this condition and read again until the fifo is not full.
1283 * We could simplify this test by not using epstatus(), but
1284 * rechecking the RX_STATUS register directly. This test could
1285 * result in unnecessary looping in cases where there is a new
1286 * packet but the fifo is not full, but it will not fix the
1287 * stuck behavior.
1288 *
1289 * Even with this improvement, we still get packet overrun errors
1290 * which are hurting performance. Maybe when I get some more time
1291 * I'll modify epread() so that it can handle RX_EARLY interrupts.
1292 */
1293 if (epstatus(sc)) {
1294 len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
1295 /* Check if we are stuck and reset [see XXX comment] */
1296 if (len & ERR_INCOMPLETE) {
1297 if (ifp->if_flags & IFF_DEBUG)
1298 printf("%s: adapter reset\n",
1299 sc->sc_dev.dv_xname);
1300 epreset(sc);
1301 return;
1302 }
1303 goto again;
1304 }
1305
1306 return;
1307
1308 abort:
1309 bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
1310 while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
1311 ;
1312 }
1313
1314 struct mbuf *
1315 epget(sc, totlen)
1316 struct ep_softc *sc;
1317 int totlen;
1318 {
1319 bus_space_tag_t iot = sc->sc_iot;
1320 bus_space_handle_t ioh = sc->sc_ioh;
1321 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1322 struct mbuf *top, **mp, *m;
1323 int len, remaining;
1324 int sh;
1325
1326 m = sc->mb[sc->next_mb];
1327 sc->mb[sc->next_mb] = 0;
1328 if (m == 0) {
1329 MGETHDR(m, M_DONTWAIT, MT_DATA);
1330 if (m == 0)
1331 return 0;
1332 } else {
1333 /* If the queue is no longer full, refill. */
1334 if (sc->last_mb == sc->next_mb)
1335 timeout(epmbuffill, sc, 1);
1336 /* Convert one of our saved mbuf's. */
1337 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1338 m->m_data = m->m_pktdat;
1339 m->m_flags = M_PKTHDR;
1340 }
1341 m->m_pkthdr.rcvif = ifp;
1342 m->m_pkthdr.len = totlen;
1343 len = MHLEN;
1344 top = 0;
1345 mp = ⊤
1346
1347 /*
1348 * We read the packet at splhigh() so that an interrupt from another
1349 * device doesn't cause the card's buffer to overflow while we're
1350 * reading it. We may still lose packets at other times.
1351 */
1352 sh = splhigh();
1353
1354 while (totlen > 0) {
1355 if (top) {
1356 m = sc->mb[sc->next_mb];
1357 sc->mb[sc->next_mb] = 0;
1358 if (m == 0) {
1359 MGET(m, M_DONTWAIT, MT_DATA);
1360 if (m == 0) {
1361 splx(sh);
1362 m_freem(top);
1363 return 0;
1364 }
1365 } else {
1366 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1367 }
1368 len = MLEN;
1369 }
1370 if (totlen >= MINCLSIZE) {
1371 MCLGET(m, M_DONTWAIT);
1372 if ((m->m_flags & M_EXT) == 0) {
1373 splx(sh);
1374 m_free(m);
1375 m_freem(top);
1376 return 0;
1377 }
1378 len = MCLBYTES;
1379 }
1380 if (top == 0) {
1381 /* align the struct ip header */
1382 caddr_t newdata = (caddr_t)
1383 ALIGN(m->m_data + sizeof(struct ether_header))
1384 - sizeof(struct ether_header);
1385 len -= newdata - m->m_data;
1386 m->m_data = newdata;
1387 }
1388 remaining = len = min(totlen, len);
1389 if (EP_IS_BUS_32(sc->bustype)) {
1390 u_long offset = mtod(m, u_long);
1391 /*
1392 * Read bytes up to the point where we are aligned.
1393 * (We can align to 4 bytes, rather than ALIGNBYTES,
1394 * here because we're later reading 4-byte chunks.)
1395 */
1396 if ((remaining > 3) && (offset & 3)) {
1397 int count = (4 - (offset & 3));
1398 bus_space_read_multi_1(iot, ioh,
1399 EP_W1_RX_PIO_RD_1,
1400 (u_int8_t *) offset, count);
1401 offset += count;
1402 remaining -= count;
1403 }
1404 if (remaining > 3) {
1405 bus_space_read_multi_4(iot, ioh,
1406 EP_W1_RX_PIO_RD_1,
1407 (u_int32_t *) offset, remaining >> 2);
1408 offset += remaining & ~3;
1409 remaining &= 3;
1410 }
1411 if (remaining) {
1412 bus_space_read_multi_1(iot, ioh,
1413 EP_W1_RX_PIO_RD_1,
1414 (u_int8_t *) offset, remaining);
1415 }
1416 } else {
1417 u_long offset = mtod(m, u_long);
1418 if ((remaining > 1) && (offset & 1)) {
1419 bus_space_read_multi_1(iot, ioh,
1420 EP_W1_RX_PIO_RD_1,
1421 (u_int8_t *) offset, 1);
1422 remaining -= 1;
1423 offset += 1;
1424 }
1425 if (remaining > 1) {
1426 bus_space_read_multi_2(iot, ioh,
1427 EP_W1_RX_PIO_RD_1,
1428 (u_int16_t *) offset, remaining >> 1);
1429 offset += remaining & ~1;
1430 }
1431 if (remaining & 1) {
1432 bus_space_read_multi_1(iot, ioh,
1433 EP_W1_RX_PIO_RD_1,
1434 (u_int8_t *) offset, remaining & 1);
1435 }
1436 }
1437 m->m_len = len;
1438 totlen -= len;
1439 *mp = m;
1440 mp = &m->m_next;
1441 }
1442
1443 bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
1444 while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
1445 ;
1446
1447 splx(sh);
1448
1449 return top;
1450 }
1451
1452 int
1453 epioctl(ifp, cmd, data)
1454 register struct ifnet *ifp;
1455 u_long cmd;
1456 caddr_t data;
1457 {
1458 struct ep_softc *sc = ifp->if_softc;
1459 struct ifaddr *ifa = (struct ifaddr *)data;
1460 struct ifreq *ifr = (struct ifreq *)data;
1461 int s, error = 0;
1462
1463 s = splnet();
1464
1465 switch (cmd) {
1466
1467 case SIOCSIFADDR:
1468 if ((error = epenable(sc)) != 0)
1469 break;
1470 /* epinit is called just below */
1471 ifp->if_flags |= IFF_UP;
1472 switch (ifa->ifa_addr->sa_family) {
1473 #ifdef INET
1474 case AF_INET:
1475 epinit(sc);
1476 arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
1477 break;
1478 #endif
1479 #ifdef NS
1480 case AF_NS:
1481 {
1482 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1483
1484 if (ns_nullhost(*ina))
1485 ina->x_host = *(union ns_host *)
1486 LLADDR(ifp->if_sadl);
1487 else
1488 bcopy(ina->x_host.c_host,
1489 LLADDR(ifp->if_sadl),
1490 ifp->if_addrlen);
1491 /* Set new address. */
1492 epinit(sc);
1493 break;
1494 }
1495 #endif
1496 default:
1497 epinit(sc);
1498 break;
1499 }
1500 break;
1501
1502 case SIOCSIFMEDIA:
1503 case SIOCGIFMEDIA:
1504 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1505 break;
1506
1507 case SIOCSIFFLAGS:
1508 if ((ifp->if_flags & IFF_UP) == 0 &&
1509 (ifp->if_flags & IFF_RUNNING) != 0) {
1510 /*
1511 * If interface is marked down and it is running, then
1512 * stop it.
1513 */
1514 epstop(sc);
1515 ifp->if_flags &= ~IFF_RUNNING;
1516 epdisable(sc);
1517 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1518 (ifp->if_flags & IFF_RUNNING) == 0) {
1519 /*
1520 * If interface is marked up and it is stopped, then
1521 * start it.
1522 */
1523 if ((error = epenable(sc)) != 0)
1524 break;
1525 epinit(sc);
1526 } else if (sc->enabled) {
1527 /*
1528 * deal with flags changes:
1529 * IFF_MULTICAST, IFF_PROMISC.
1530 */
1531 epsetfilter(sc);
1532 }
1533 break;
1534
1535 case SIOCADDMULTI:
1536 case SIOCDELMULTI:
1537 if (sc->enabled == 0) {
1538 error = EIO;
1539 break;
1540 }
1541
1542 error = (cmd == SIOCADDMULTI) ?
1543 ether_addmulti(ifr, &sc->sc_ethercom) :
1544 ether_delmulti(ifr, &sc->sc_ethercom);
1545
1546 if (error == ENETRESET) {
1547 /*
1548 * Multicast list has changed; set the hardware filter
1549 * accordingly.
1550 */
1551 epreset(sc);
1552 error = 0;
1553 }
1554 break;
1555
1556 default:
1557 error = EINVAL;
1558 break;
1559 }
1560
1561 splx(s);
1562 return (error);
1563 }
1564
1565 void
1566 epreset(sc)
1567 struct ep_softc *sc;
1568 {
1569 int s;
1570
1571 s = splnet();
1572 epstop(sc);
1573 epinit(sc);
1574 splx(s);
1575 }
1576
1577 void
1578 epwatchdog(ifp)
1579 struct ifnet *ifp;
1580 {
1581 struct ep_softc *sc = ifp->if_softc;
1582
1583 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1584 ++sc->sc_ethercom.ec_if.if_oerrors;
1585
1586 epreset(sc);
1587 }
1588
1589 void
1590 epstop(sc)
1591 register struct ep_softc *sc;
1592 {
1593 bus_space_tag_t iot = sc->sc_iot;
1594 bus_space_handle_t ioh = sc->sc_ioh;
1595
1596 bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
1597 bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
1598 while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
1599 ;
1600 bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
1601 bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
1602
1603 ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
1604 ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
1605
1606 bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
1607 bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
1608 bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
1609 bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
1610
1611 epmbufempty(sc);
1612 }
1613
1614
1615 /*
1616 * Before reboots, reset card completely.
1617 */
1618 static void
1619 epshutdown(arg)
1620 void *arg;
1621 {
1622 register struct ep_softc *sc = arg;
1623
1624 if (sc->enabled) {
1625 epstop(sc);
1626 ep_complete_cmd(sc, EP_COMMAND, GLOBAL_RESET);
1627 }
1628 }
1629
1630 /*
1631 * We get eeprom data from the id_port given an offset into the
1632 * eeprom. Basically; after the ID_sequence is sent to all of
1633 * the cards; they enter the ID_CMD state where they will accept
1634 * command requests. 0x80-0xbf loads the eeprom data. We then
1635 * read the port 16 times and with every read; the cards check
1636 * for contention (ie: if one card writes a 0 bit and another
1637 * writes a 1 bit then the host sees a 0. At the end of the cycle;
1638 * each card compares the data on the bus; if there is a difference
1639 * then that card goes into ID_WAIT state again). In the meantime;
1640 * one bit of data is returned in the AX register which is conveniently
1641 * returned to us by bus_space_read_1(). Hence; we read 16 times getting one
1642 * bit of data with each read.
1643 *
1644 * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
1645 */
1646 u_int16_t
1647 epreadeeprom(iot, ioh, offset)
1648 bus_space_tag_t iot;
1649 bus_space_handle_t ioh;
1650 int offset;
1651 {
1652 u_int16_t data = 0;
1653 int i;
1654
1655 bus_space_write_1(iot, ioh, 0, 0x80 + offset);
1656 delay(1000);
1657 for (i = 0; i < 16; i++)
1658 data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
1659 return (data);
1660 }
1661
1662 static int
1663 epbusyeeprom(sc)
1664 struct ep_softc *sc;
1665 {
1666 bus_space_tag_t iot = sc->sc_iot;
1667 bus_space_handle_t ioh = sc->sc_ioh;
1668 int i = 100, j;
1669
1670 if (sc->bustype == EP_BUS_PCMCIA) {
1671 delay(1000);
1672 return 0;
1673 }
1674
1675 j = 0; /* bad GCC flow analysis */
1676 while (i--) {
1677 j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
1678 if (j & EEPROM_BUSY)
1679 delay(100);
1680 else
1681 break;
1682 }
1683 if (!i) {
1684 printf("\n%s: eeprom failed to come ready\n",
1685 sc->sc_dev.dv_xname);
1686 return (1);
1687 }
1688 if (j & EEPROM_TST_MODE) {
1689 /* XXX PnP mode? */
1690 printf("\n%s: erase pencil mark!\n", sc->sc_dev.dv_xname);
1691 return (1);
1692 }
1693 return (0);
1694 }
1695
1696 void
1697 epmbuffill(v)
1698 void *v;
1699 {
1700 struct ep_softc *sc = v;
1701 int s, i;
1702
1703 s = splnet();
1704 i = sc->last_mb;
1705 do {
1706 if (sc->mb[i] == NULL)
1707 MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
1708 if (sc->mb[i] == NULL)
1709 break;
1710 i = (i + 1) % MAX_MBS;
1711 } while (i != sc->next_mb);
1712 sc->last_mb = i;
1713 /* If the queue was not filled, try again. */
1714 if (sc->last_mb != sc->next_mb)
1715 timeout(epmbuffill, sc, 1);
1716 splx(s);
1717 }
1718
1719 void
1720 epmbufempty(sc)
1721 struct ep_softc *sc;
1722 {
1723 int s, i;
1724
1725 s = splnet();
1726 for (i = 0; i<MAX_MBS; i++) {
1727 if (sc->mb[i]) {
1728 m_freem(sc->mb[i]);
1729 sc->mb[i] = NULL;
1730 }
1731 }
1732 sc->last_mb = sc->next_mb = 0;
1733 untimeout(epmbuffill, sc);
1734 splx(s);
1735 }
1736
1737 int
1738 epenable(sc)
1739 struct ep_softc *sc;
1740 {
1741
1742 if (sc->enabled == 0 && sc->enable != NULL) {
1743 if ((*sc->enable)(sc) != 0) {
1744 printf("%s: device enable failed\n",
1745 sc->sc_dev.dv_xname);
1746 return (EIO);
1747 }
1748 }
1749
1750 sc->enabled = 1;
1751 return (0);
1752 }
1753
1754 void
1755 epdisable(sc)
1756 struct ep_softc *sc;
1757 {
1758
1759 if (sc->enabled != 0 && sc->disable != NULL) {
1760 (*sc->disable)(sc);
1761 sc->enabled = 0;
1762 }
1763 }
1764