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