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