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