elink3.c revision 1.1 1 1.1 thorpej /* $NetBSD: elink3.c,v 1.1 1996/04/25 02:17:34 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) novatel.ca>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Herb Peyerl.
18 1.1 thorpej * 4. The name of Herb Peyerl may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej #include "bpfilter.h"
34 1.1 thorpej
35 1.1 thorpej #include <sys/param.h>
36 1.1 thorpej #include <sys/mbuf.h>
37 1.1 thorpej #include <sys/socket.h>
38 1.1 thorpej #include <sys/ioctl.h>
39 1.1 thorpej #include <sys/errno.h>
40 1.1 thorpej #include <sys/syslog.h>
41 1.1 thorpej #include <sys/select.h>
42 1.1 thorpej #include <sys/device.h>
43 1.1 thorpej
44 1.1 thorpej #include <net/if.h>
45 1.1 thorpej #include <net/netisr.h>
46 1.1 thorpej #include <net/if_dl.h>
47 1.1 thorpej #include <net/if_types.h>
48 1.1 thorpej #include <net/netisr.h>
49 1.1 thorpej
50 1.1 thorpej #ifdef INET
51 1.1 thorpej #include <netinet/in.h>
52 1.1 thorpej #include <netinet/in_systm.h>
53 1.1 thorpej #include <netinet/in_var.h>
54 1.1 thorpej #include <netinet/ip.h>
55 1.1 thorpej #include <netinet/if_ether.h>
56 1.1 thorpej #endif
57 1.1 thorpej
58 1.1 thorpej #ifdef NS
59 1.1 thorpej #include <netns/ns.h>
60 1.1 thorpej #include <netns/ns_if.h>
61 1.1 thorpej #endif
62 1.1 thorpej
63 1.1 thorpej #if NBPFILTER > 0
64 1.1 thorpej #include <net/bpf.h>
65 1.1 thorpej #include <net/bpfdesc.h>
66 1.1 thorpej #endif
67 1.1 thorpej
68 1.1 thorpej #include <machine/cpu.h>
69 1.1 thorpej #include <machine/pio.h>
70 1.1 thorpej
71 1.1 thorpej #include <dev/ic/elink3var.h>
72 1.1 thorpej #include <dev/ic/elink3reg.h>
73 1.1 thorpej
74 1.1 thorpej #define ETHER_MIN_LEN 64
75 1.1 thorpej #define ETHER_MAX_LEN 1518
76 1.1 thorpej #define ETHER_ADDR_LEN 6
77 1.1 thorpej
78 1.1 thorpej struct cfdriver ep_cd = {
79 1.1 thorpej NULL, "ep", DV_IFNET
80 1.1 thorpej };
81 1.1 thorpej
82 1.1 thorpej static void epxstat __P((struct ep_softc *));
83 1.1 thorpej static int epstatus __P((struct ep_softc *));
84 1.1 thorpej void epinit __P((struct ep_softc *));
85 1.1 thorpej int epioctl __P((struct ifnet *, u_long, caddr_t));
86 1.1 thorpej void epstart __P((struct ifnet *));
87 1.1 thorpej void epwatchdog __P((int));
88 1.1 thorpej void epreset __P((struct ep_softc *));
89 1.1 thorpej void epread __P((struct ep_softc *));
90 1.1 thorpej struct mbuf *epget __P((struct ep_softc *, int));
91 1.1 thorpej void epmbuffill __P((struct ep_softc *));
92 1.1 thorpej void epmbufempty __P((struct ep_softc *));
93 1.1 thorpej void epstop __P((struct ep_softc *));
94 1.1 thorpej void epsetfilter __P((struct ep_softc *));
95 1.1 thorpej void epsetlink __P((struct ep_softc *));
96 1.1 thorpej u_short epreadeeprom __P((int id_port, int offset));
97 1.1 thorpej
98 1.1 thorpej static int epbusyeeprom __P((struct ep_softc *));
99 1.1 thorpej
100 1.1 thorpej void
101 1.1 thorpej epconfig(sc, conn)
102 1.1 thorpej struct ep_softc *sc;
103 1.1 thorpej u_int conn;
104 1.1 thorpej {
105 1.1 thorpej u_short i;
106 1.1 thorpej
107 1.1 thorpej struct ifnet *ifp = &sc->sc_arpcom.ac_if;
108 1.1 thorpej
109 1.1 thorpej sc->ep_connectors = 0;
110 1.1 thorpej printf("%s: ", sc->sc_dev.dv_xname);
111 1.1 thorpej if (conn & IS_AUI) {
112 1.1 thorpej printf("aui");
113 1.1 thorpej sc->ep_connectors |= AUI;
114 1.1 thorpej }
115 1.1 thorpej if (conn & IS_BNC) {
116 1.1 thorpej if (sc->ep_connectors)
117 1.1 thorpej printf("/");
118 1.1 thorpej printf("bnc");
119 1.1 thorpej sc->ep_connectors |= BNC;
120 1.1 thorpej }
121 1.1 thorpej if (conn & IS_UTP) {
122 1.1 thorpej if (sc->ep_connectors)
123 1.1 thorpej printf("/");
124 1.1 thorpej printf("utp");
125 1.1 thorpej sc->ep_connectors |= UTP;
126 1.1 thorpej }
127 1.1 thorpej if (!sc->ep_connectors)
128 1.1 thorpej printf("no connectors!");
129 1.1 thorpej
130 1.1 thorpej /*
131 1.1 thorpej * Read the station address from the eeprom
132 1.1 thorpej */
133 1.1 thorpej for (i = 0; i < 3; i++) {
134 1.1 thorpej u_short x;
135 1.1 thorpej if (epbusyeeprom(sc))
136 1.1 thorpej return;
137 1.1 thorpej outw(BASE + EP_W0_EEPROM_COMMAND, READ_EEPROM | i);
138 1.1 thorpej if (epbusyeeprom(sc))
139 1.1 thorpej return;
140 1.1 thorpej x = inw(BASE + EP_W0_EEPROM_DATA);
141 1.1 thorpej sc->sc_arpcom.ac_enaddr[(i << 1)] = x >> 8;
142 1.1 thorpej sc->sc_arpcom.ac_enaddr[(i << 1) + 1] = x;
143 1.1 thorpej }
144 1.1 thorpej
145 1.1 thorpej printf(" address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
146 1.1 thorpej
147 1.1 thorpej ifp->if_unit = sc->sc_dev.dv_unit;
148 1.1 thorpej ifp->if_name = ep_cd.cd_name;
149 1.1 thorpej ifp->if_start = epstart;
150 1.1 thorpej ifp->if_ioctl = epioctl;
151 1.1 thorpej ifp->if_watchdog = epwatchdog;
152 1.1 thorpej ifp->if_flags =
153 1.1 thorpej IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
154 1.1 thorpej
155 1.1 thorpej if_attach(ifp);
156 1.1 thorpej ether_ifattach(ifp);
157 1.1 thorpej
158 1.1 thorpej #if NBPFILTER > 0
159 1.1 thorpej bpfattach(&sc->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
160 1.1 thorpej sizeof(struct ether_header));
161 1.1 thorpej #endif
162 1.1 thorpej
163 1.1 thorpej sc->tx_start_thresh = 20; /* probably a good starting point. */
164 1.1 thorpej }
165 1.1 thorpej
166 1.1 thorpej /*
167 1.1 thorpej * The order in here seems important. Otherwise we may not receive
168 1.1 thorpej * interrupts. ?!
169 1.1 thorpej */
170 1.1 thorpej void
171 1.1 thorpej epinit(sc)
172 1.1 thorpej register struct ep_softc *sc;
173 1.1 thorpej {
174 1.1 thorpej register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
175 1.1 thorpej int i;
176 1.1 thorpej
177 1.1 thorpej while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
178 1.1 thorpej ;
179 1.1 thorpej
180 1.1 thorpej if (sc->bustype != EP_BUS_PCI) {
181 1.1 thorpej GO_WINDOW(0);
182 1.1 thorpej outw(BASE + EP_W0_CONFIG_CTRL, 0);
183 1.1 thorpej outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
184 1.1 thorpej }
185 1.1 thorpej
186 1.1 thorpej if (sc->bustype == EP_BUS_PCMCIA) {
187 1.1 thorpej #ifdef EP_COAX_DEFAULT
188 1.1 thorpej outw(BASE + EP_W0_ADDRESS_CFG,3<<14);
189 1.1 thorpej #else
190 1.1 thorpej outw(BASE + EP_W0_ADDRESS_CFG,0<<14);
191 1.1 thorpej #endif
192 1.1 thorpej outw(BASE + EP_W0_RESOURCE_CFG, 0x3f00);
193 1.1 thorpej }
194 1.1 thorpej
195 1.1 thorpej GO_WINDOW(2);
196 1.1 thorpej for (i = 0; i < 6; i++) /* Reload the ether_addr. */
197 1.1 thorpej outb(BASE + EP_W2_ADDR_0 + i, sc->sc_arpcom.ac_enaddr[i]);
198 1.1 thorpej
199 1.1 thorpej outw(BASE + EP_COMMAND, RX_RESET);
200 1.1 thorpej outw(BASE + EP_COMMAND, TX_RESET);
201 1.1 thorpej
202 1.1 thorpej GO_WINDOW(1); /* Window 1 is operating window */
203 1.1 thorpej for (i = 0; i < 31; i++)
204 1.1 thorpej inb(BASE + EP_W1_TX_STATUS);
205 1.1 thorpej
206 1.1 thorpej outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
207 1.1 thorpej S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
208 1.1 thorpej outw(BASE + EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
209 1.1 thorpej S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
210 1.1 thorpej
211 1.1 thorpej /*
212 1.1 thorpej * Attempt to get rid of any stray interrupts that occured during
213 1.1 thorpej * configuration. On the i386 this isn't possible because one may
214 1.1 thorpej * already be queued. However, a single stray interrupt is
215 1.1 thorpej * unimportant.
216 1.1 thorpej */
217 1.1 thorpej outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
218 1.1 thorpej
219 1.1 thorpej epsetfilter(sc);
220 1.1 thorpej epsetlink(sc);
221 1.1 thorpej
222 1.1 thorpej outw(BASE + EP_COMMAND, RX_ENABLE);
223 1.1 thorpej outw(BASE + EP_COMMAND, TX_ENABLE);
224 1.1 thorpej
225 1.1 thorpej epmbuffill(sc);
226 1.1 thorpej
227 1.1 thorpej /* Interface is now `running', with no output active. */
228 1.1 thorpej ifp->if_flags |= IFF_RUNNING;
229 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
230 1.1 thorpej
231 1.1 thorpej /* Attempt to start output, if any. */
232 1.1 thorpej epstart(ifp);
233 1.1 thorpej }
234 1.1 thorpej
235 1.1 thorpej void
236 1.1 thorpej epsetfilter(sc)
237 1.1 thorpej register struct ep_softc *sc;
238 1.1 thorpej {
239 1.1 thorpej register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
240 1.1 thorpej
241 1.1 thorpej GO_WINDOW(1); /* Window 1 is operating window */
242 1.1 thorpej outw(BASE + EP_COMMAND, SET_RX_FILTER |
243 1.1 thorpej FIL_INDIVIDUAL | FIL_BRDCST |
244 1.1 thorpej ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
245 1.1 thorpej ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
246 1.1 thorpej }
247 1.1 thorpej
248 1.1 thorpej void
249 1.1 thorpej epsetlink(sc)
250 1.1 thorpej register struct ep_softc *sc;
251 1.1 thorpej {
252 1.1 thorpej register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
253 1.1 thorpej
254 1.1 thorpej /*
255 1.1 thorpej * you can `ifconfig (link0|-link0) ep0' to get the following
256 1.1 thorpej * behaviour:
257 1.1 thorpej * -link0 disable AUI/UTP. enable BNC.
258 1.1 thorpej * link0 disable BNC. enable AUI.
259 1.1 thorpej * link1 if the card has a UTP connector, and link0 is
260 1.1 thorpej * set too, then you get the UTP port.
261 1.1 thorpej */
262 1.1 thorpej GO_WINDOW(4);
263 1.1 thorpej outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
264 1.1 thorpej if (!(ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & BNC)) {
265 1.1 thorpej if (sc->bustype == EP_BUS_PCMCIA) {
266 1.1 thorpej GO_WINDOW(0);
267 1.1 thorpej outw(BASE + EP_W0_ADDRESS_CFG,3<<14);
268 1.1 thorpej GO_WINDOW(1);
269 1.1 thorpej }
270 1.1 thorpej outw(BASE + EP_COMMAND, START_TRANSCEIVER);
271 1.1 thorpej delay(1000);
272 1.1 thorpej }
273 1.1 thorpej if (ifp->if_flags & IFF_LINK0) {
274 1.1 thorpej outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
275 1.1 thorpej delay(1000);
276 1.1 thorpej if ((ifp->if_flags & IFF_LINK1) && (sc->ep_connectors & UTP)) {
277 1.1 thorpej if (sc->bustype == EP_BUS_PCMCIA) {
278 1.1 thorpej GO_WINDOW(0);
279 1.1 thorpej outw(BASE + EP_W0_ADDRESS_CFG,0<<14);
280 1.1 thorpej GO_WINDOW(4);
281 1.1 thorpej }
282 1.1 thorpej outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
283 1.1 thorpej }
284 1.1 thorpej }
285 1.1 thorpej GO_WINDOW(1);
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej /*
289 1.1 thorpej * Start outputting on the interface.
290 1.1 thorpej * Always called as splnet().
291 1.1 thorpej */
292 1.1 thorpej void
293 1.1 thorpej epstart(ifp)
294 1.1 thorpej struct ifnet *ifp;
295 1.1 thorpej {
296 1.1 thorpej register struct ep_softc *sc = ep_cd.cd_devs[ifp->if_unit];
297 1.1 thorpej struct mbuf *m, *m0;
298 1.1 thorpej int sh, len, pad;
299 1.1 thorpej
300 1.1 thorpej /* Don't transmit if interface is busy or not running */
301 1.1 thorpej if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
302 1.1 thorpej return;
303 1.1 thorpej
304 1.1 thorpej startagain:
305 1.1 thorpej /* Sneak a peek at the next packet */
306 1.1 thorpej m0 = ifp->if_snd.ifq_head;
307 1.1 thorpej if (m0 == 0)
308 1.1 thorpej return;
309 1.1 thorpej
310 1.1 thorpej /* We need to use m->m_pkthdr.len, so require the header */
311 1.1 thorpej if ((m0->m_flags & M_PKTHDR) == 0)
312 1.1 thorpej panic("epstart: no header mbuf");
313 1.1 thorpej len = m0->m_pkthdr.len;
314 1.1 thorpej
315 1.1 thorpej pad = (4 - len) & 3;
316 1.1 thorpej
317 1.1 thorpej /*
318 1.1 thorpej * The 3c509 automatically pads short packets to minimum ethernet
319 1.1 thorpej * length, but we drop packets that are too large. Perhaps we should
320 1.1 thorpej * truncate them instead?
321 1.1 thorpej */
322 1.1 thorpej if (len + pad > ETHER_MAX_LEN) {
323 1.1 thorpej /* packet is obviously too large: toss it */
324 1.1 thorpej ++ifp->if_oerrors;
325 1.1 thorpej IF_DEQUEUE(&ifp->if_snd, m0);
326 1.1 thorpej m_freem(m0);
327 1.1 thorpej goto readcheck;
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
331 1.1 thorpej outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
332 1.1 thorpej /* not enough room in FIFO */
333 1.1 thorpej ifp->if_flags |= IFF_OACTIVE;
334 1.1 thorpej return;
335 1.1 thorpej } else {
336 1.1 thorpej outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 2044);
337 1.1 thorpej }
338 1.1 thorpej
339 1.1 thorpej IF_DEQUEUE(&ifp->if_snd, m0);
340 1.1 thorpej if (m0 == 0) /* not really needed */
341 1.1 thorpej return;
342 1.1 thorpej
343 1.1 thorpej outw(BASE + EP_COMMAND, SET_TX_START_THRESH |
344 1.1 thorpej (len / 4 + sc->tx_start_thresh));
345 1.1 thorpej
346 1.1 thorpej #if NBPFILTER > 0
347 1.1 thorpej if (ifp->if_bpf)
348 1.1 thorpej bpf_mtap(ifp->if_bpf, m0);
349 1.1 thorpej #endif
350 1.1 thorpej
351 1.1 thorpej /*
352 1.1 thorpej * Do the output at splhigh() so that an interrupt from another device
353 1.1 thorpej * won't cause a FIFO underrun.
354 1.1 thorpej */
355 1.1 thorpej sh = splhigh();
356 1.1 thorpej
357 1.1 thorpej outw(BASE + EP_W1_TX_PIO_WR_1, len);
358 1.1 thorpej outw(BASE + EP_W1_TX_PIO_WR_1, 0xffff); /* Second dword meaningless */
359 1.1 thorpej if (EP_IS_BUS_32(sc->bustype)) {
360 1.1 thorpej for (m = m0; m; ) {
361 1.1 thorpej if (m->m_len > 3)
362 1.1 thorpej outsl(BASE + EP_W1_TX_PIO_WR_1,
363 1.1 thorpej mtod(m, caddr_t), m->m_len / 4);
364 1.1 thorpej if (m->m_len & 3)
365 1.1 thorpej outsb(BASE + EP_W1_TX_PIO_WR_1,
366 1.1 thorpej mtod(m, caddr_t) + (m->m_len & ~3),
367 1.1 thorpej m->m_len & 3);
368 1.1 thorpej MFREE(m, m0);
369 1.1 thorpej m = m0;
370 1.1 thorpej }
371 1.1 thorpej } else {
372 1.1 thorpej for (m = m0; m; ) {
373 1.1 thorpej if (m->m_len > 1)
374 1.1 thorpej outsw(BASE + EP_W1_TX_PIO_WR_1,
375 1.1 thorpej mtod(m, caddr_t), m->m_len / 2);
376 1.1 thorpej if (m->m_len & 1)
377 1.1 thorpej outb(BASE + EP_W1_TX_PIO_WR_1,
378 1.1 thorpej *(mtod(m, caddr_t) + m->m_len - 1));
379 1.1 thorpej MFREE(m, m0);
380 1.1 thorpej m = m0;
381 1.1 thorpej }
382 1.1 thorpej }
383 1.1 thorpej while (pad--)
384 1.1 thorpej outb(BASE + EP_W1_TX_PIO_WR_1, 0);
385 1.1 thorpej
386 1.1 thorpej splx(sh);
387 1.1 thorpej
388 1.1 thorpej ++ifp->if_opackets;
389 1.1 thorpej
390 1.1 thorpej readcheck:
391 1.1 thorpej if ((inw(BASE + EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
392 1.1 thorpej /* We received a complete packet. */
393 1.1 thorpej u_short status = inw(BASE + EP_STATUS);
394 1.1 thorpej
395 1.1 thorpej if ((status & S_INTR_LATCH) == 0) {
396 1.1 thorpej /*
397 1.1 thorpej * No interrupt, read the packet and continue
398 1.1 thorpej * Is this supposed to happen? Is my motherboard
399 1.1 thorpej * completely busted?
400 1.1 thorpej */
401 1.1 thorpej epread(sc);
402 1.1 thorpej }
403 1.1 thorpej else
404 1.1 thorpej /* Got an interrupt, return so that it gets serviced. */
405 1.1 thorpej return;
406 1.1 thorpej }
407 1.1 thorpej else {
408 1.1 thorpej /* Check if we are stuck and reset [see XXX comment] */
409 1.1 thorpej if (epstatus(sc)) {
410 1.1 thorpej if (ifp->if_flags & IFF_DEBUG)
411 1.1 thorpej printf("%s: adapter reset\n",
412 1.1 thorpej sc->sc_dev.dv_xname);
413 1.1 thorpej epreset(sc);
414 1.1 thorpej }
415 1.1 thorpej }
416 1.1 thorpej
417 1.1 thorpej goto startagain;
418 1.1 thorpej }
419 1.1 thorpej
420 1.1 thorpej
421 1.1 thorpej /*
422 1.1 thorpej * XXX: The 3c509 card can get in a mode where both the fifo status bit
423 1.1 thorpej * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
424 1.1 thorpej * We detect this situation and we reset the adapter.
425 1.1 thorpej * It happens at times when there is a lot of broadcast traffic
426 1.1 thorpej * on the cable (once in a blue moon).
427 1.1 thorpej */
428 1.1 thorpej static int
429 1.1 thorpej epstatus(sc)
430 1.1 thorpej register struct ep_softc *sc;
431 1.1 thorpej {
432 1.1 thorpej u_short fifost;
433 1.1 thorpej
434 1.1 thorpej /*
435 1.1 thorpej * Check the FIFO status and act accordingly
436 1.1 thorpej */
437 1.1 thorpej GO_WINDOW(4);
438 1.1 thorpej fifost = inw(BASE + EP_W4_FIFO_DIAG);
439 1.1 thorpej GO_WINDOW(1);
440 1.1 thorpej
441 1.1 thorpej if (fifost & FIFOS_RX_UNDERRUN) {
442 1.1 thorpej if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
443 1.1 thorpej printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
444 1.1 thorpej epreset(sc);
445 1.1 thorpej return 0;
446 1.1 thorpej }
447 1.1 thorpej
448 1.1 thorpej if (fifost & FIFOS_RX_STATUS_OVERRUN) {
449 1.1 thorpej if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
450 1.1 thorpej printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
451 1.1 thorpej return 1;
452 1.1 thorpej }
453 1.1 thorpej
454 1.1 thorpej if (fifost & FIFOS_RX_OVERRUN) {
455 1.1 thorpej if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
456 1.1 thorpej printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
457 1.1 thorpej return 1;
458 1.1 thorpej }
459 1.1 thorpej
460 1.1 thorpej if (fifost & FIFOS_TX_OVERRUN) {
461 1.1 thorpej if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
462 1.1 thorpej printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
463 1.1 thorpej epreset(sc);
464 1.1 thorpej return 0;
465 1.1 thorpej }
466 1.1 thorpej
467 1.1 thorpej return 0;
468 1.1 thorpej }
469 1.1 thorpej
470 1.1 thorpej
471 1.1 thorpej static void
472 1.1 thorpej eptxstat(sc)
473 1.1 thorpej register struct ep_softc *sc;
474 1.1 thorpej {
475 1.1 thorpej int i;
476 1.1 thorpej
477 1.1 thorpej /*
478 1.1 thorpej * We need to read+write TX_STATUS until we get a 0 status
479 1.1 thorpej * in order to turn off the interrupt flag.
480 1.1 thorpej */
481 1.1 thorpej while ((i = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
482 1.1 thorpej outb(BASE + EP_W1_TX_STATUS, 0x0);
483 1.1 thorpej
484 1.1 thorpej if (i & TXS_JABBER) {
485 1.1 thorpej ++sc->sc_arpcom.ac_if.if_oerrors;
486 1.1 thorpej if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
487 1.1 thorpej printf("%s: jabber (%x)\n",
488 1.1 thorpej sc->sc_dev.dv_xname, i);
489 1.1 thorpej epreset(sc);
490 1.1 thorpej } else if (i & TXS_UNDERRUN) {
491 1.1 thorpej ++sc->sc_arpcom.ac_if.if_oerrors;
492 1.1 thorpej if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
493 1.1 thorpej printf("%s: fifo underrun (%x) @%d\n",
494 1.1 thorpej sc->sc_dev.dv_xname, i,
495 1.1 thorpej sc->tx_start_thresh);
496 1.1 thorpej if (sc->tx_succ_ok < 100)
497 1.1 thorpej sc->tx_start_thresh = min(ETHER_MAX_LEN,
498 1.1 thorpej sc->tx_start_thresh + 20);
499 1.1 thorpej sc->tx_succ_ok = 0;
500 1.1 thorpej epreset(sc);
501 1.1 thorpej } else if (i & TXS_MAX_COLLISION) {
502 1.1 thorpej ++sc->sc_arpcom.ac_if.if_collisions;
503 1.1 thorpej outw(BASE + EP_COMMAND, TX_ENABLE);
504 1.1 thorpej sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
505 1.1 thorpej } else
506 1.1 thorpej sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
507 1.1 thorpej
508 1.1 thorpej }
509 1.1 thorpej }
510 1.1 thorpej
511 1.1 thorpej int
512 1.1 thorpej epintr(arg)
513 1.1 thorpej void *arg;
514 1.1 thorpej {
515 1.1 thorpej register struct ep_softc *sc = arg;
516 1.1 thorpej struct ifnet *ifp = &sc->sc_arpcom.ac_if;
517 1.1 thorpej u_short status;
518 1.1 thorpej int ret = 0;
519 1.1 thorpej
520 1.1 thorpej for (;;) {
521 1.1 thorpej outw(BASE + EP_COMMAND, C_INTR_LATCH);
522 1.1 thorpej
523 1.1 thorpej status = inw(BASE + EP_STATUS);
524 1.1 thorpej
525 1.1 thorpej if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
526 1.1 thorpej S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
527 1.1 thorpej break;
528 1.1 thorpej
529 1.1 thorpej ret = 1;
530 1.1 thorpej
531 1.1 thorpej /*
532 1.1 thorpej * Acknowledge any interrupts. It's important that we do this
533 1.1 thorpej * first, since there would otherwise be a race condition.
534 1.1 thorpej * Due to the i386 interrupt queueing, we may get spurious
535 1.1 thorpej * interrupts occasionally.
536 1.1 thorpej */
537 1.1 thorpej outw(BASE + EP_COMMAND, ACK_INTR | status);
538 1.1 thorpej
539 1.1 thorpej if (status & S_RX_COMPLETE)
540 1.1 thorpej epread(sc);
541 1.1 thorpej if (status & S_TX_AVAIL) {
542 1.1 thorpej sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
543 1.1 thorpej epstart(&sc->sc_arpcom.ac_if);
544 1.1 thorpej }
545 1.1 thorpej if (status & S_CARD_FAILURE) {
546 1.1 thorpej printf("%s: adapter failure (%x)\n",
547 1.1 thorpej sc->sc_dev.dv_xname, status);
548 1.1 thorpej epreset(sc);
549 1.1 thorpej return (1);
550 1.1 thorpej }
551 1.1 thorpej if (status & S_TX_COMPLETE) {
552 1.1 thorpej eptxstat(sc);
553 1.1 thorpej epstart(ifp);
554 1.1 thorpej }
555 1.1 thorpej }
556 1.1 thorpej
557 1.1 thorpej /* no more interrupts */
558 1.1 thorpej return (ret);
559 1.1 thorpej }
560 1.1 thorpej
561 1.1 thorpej void
562 1.1 thorpej epread(sc)
563 1.1 thorpej register struct ep_softc *sc;
564 1.1 thorpej {
565 1.1 thorpej struct ifnet *ifp = &sc->sc_arpcom.ac_if;
566 1.1 thorpej struct mbuf *m;
567 1.1 thorpej struct ether_header *eh;
568 1.1 thorpej int len;
569 1.1 thorpej
570 1.1 thorpej len = inw(BASE + EP_W1_RX_STATUS);
571 1.1 thorpej
572 1.1 thorpej again:
573 1.1 thorpej if (ifp->if_flags & IFF_DEBUG) {
574 1.1 thorpej int err = len & ERR_MASK;
575 1.1 thorpej char *s = NULL;
576 1.1 thorpej
577 1.1 thorpej if (len & ERR_INCOMPLETE)
578 1.1 thorpej s = "incomplete packet";
579 1.1 thorpej else if (err == ERR_OVERRUN)
580 1.1 thorpej s = "packet overrun";
581 1.1 thorpej else if (err == ERR_RUNT)
582 1.1 thorpej s = "runt packet";
583 1.1 thorpej else if (err == ERR_ALIGNMENT)
584 1.1 thorpej s = "bad alignment";
585 1.1 thorpej else if (err == ERR_CRC)
586 1.1 thorpej s = "bad crc";
587 1.1 thorpej else if (err == ERR_OVERSIZE)
588 1.1 thorpej s = "oversized packet";
589 1.1 thorpej else if (err == ERR_DRIBBLE)
590 1.1 thorpej s = "dribble bits";
591 1.1 thorpej
592 1.1 thorpej if (s)
593 1.1 thorpej printf("%s: %s\n", sc->sc_dev.dv_xname, s);
594 1.1 thorpej }
595 1.1 thorpej
596 1.1 thorpej if (len & ERR_INCOMPLETE)
597 1.1 thorpej return;
598 1.1 thorpej
599 1.1 thorpej if (len & ERR_RX) {
600 1.1 thorpej ++ifp->if_ierrors;
601 1.1 thorpej goto abort;
602 1.1 thorpej }
603 1.1 thorpej
604 1.1 thorpej len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */
605 1.1 thorpej
606 1.1 thorpej /* Pull packet off interface. */
607 1.1 thorpej m = epget(sc, len);
608 1.1 thorpej if (m == 0) {
609 1.1 thorpej ifp->if_ierrors++;
610 1.1 thorpej goto abort;
611 1.1 thorpej }
612 1.1 thorpej
613 1.1 thorpej ++ifp->if_ipackets;
614 1.1 thorpej
615 1.1 thorpej /* We assume the header fit entirely in one mbuf. */
616 1.1 thorpej eh = mtod(m, struct ether_header *);
617 1.1 thorpej
618 1.1 thorpej #if NBPFILTER > 0
619 1.1 thorpej /*
620 1.1 thorpej * Check if there's a BPF listener on this interface.
621 1.1 thorpej * If so, hand off the raw packet to BPF.
622 1.1 thorpej */
623 1.1 thorpej if (ifp->if_bpf) {
624 1.1 thorpej bpf_mtap(ifp->if_bpf, m);
625 1.1 thorpej
626 1.1 thorpej /*
627 1.1 thorpej * Note that the interface cannot be in promiscuous mode if
628 1.1 thorpej * there are no BPF listeners. And if we are in promiscuous
629 1.1 thorpej * mode, we have to check if this packet is really ours.
630 1.1 thorpej */
631 1.1 thorpej if ((ifp->if_flags & IFF_PROMISC) &&
632 1.1 thorpej (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
633 1.1 thorpej bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
634 1.1 thorpej sizeof(eh->ether_dhost)) != 0) {
635 1.1 thorpej m_freem(m);
636 1.1 thorpej return;
637 1.1 thorpej }
638 1.1 thorpej }
639 1.1 thorpej #endif
640 1.1 thorpej
641 1.1 thorpej /* We assume the header fit entirely in one mbuf. */
642 1.1 thorpej m_adj(m, sizeof(struct ether_header));
643 1.1 thorpej ether_input(ifp, eh, m);
644 1.1 thorpej
645 1.1 thorpej /*
646 1.1 thorpej * In periods of high traffic we can actually receive enough
647 1.1 thorpej * packets so that the fifo overrun bit will be set at this point,
648 1.1 thorpej * even though we just read a packet. In this case we
649 1.1 thorpej * are not going to receive any more interrupts. We check for
650 1.1 thorpej * this condition and read again until the fifo is not full.
651 1.1 thorpej * We could simplify this test by not using epstatus(), but
652 1.1 thorpej * rechecking the RX_STATUS register directly. This test could
653 1.1 thorpej * result in unnecessary looping in cases where there is a new
654 1.1 thorpej * packet but the fifo is not full, but it will not fix the
655 1.1 thorpej * stuck behavior.
656 1.1 thorpej *
657 1.1 thorpej * Even with this improvement, we still get packet overrun errors
658 1.1 thorpej * which are hurting performance. Maybe when I get some more time
659 1.1 thorpej * I'll modify epread() so that it can handle RX_EARLY interrupts.
660 1.1 thorpej */
661 1.1 thorpej if (epstatus(sc)) {
662 1.1 thorpej len = inw(BASE + EP_W1_RX_STATUS);
663 1.1 thorpej /* Check if we are stuck and reset [see XXX comment] */
664 1.1 thorpej if (len & ERR_INCOMPLETE) {
665 1.1 thorpej if (ifp->if_flags & IFF_DEBUG)
666 1.1 thorpej printf("%s: adapter reset\n",
667 1.1 thorpej sc->sc_dev.dv_xname);
668 1.1 thorpej epreset(sc);
669 1.1 thorpej return;
670 1.1 thorpej }
671 1.1 thorpej goto again;
672 1.1 thorpej }
673 1.1 thorpej
674 1.1 thorpej return;
675 1.1 thorpej
676 1.1 thorpej abort:
677 1.1 thorpej outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
678 1.1 thorpej while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
679 1.1 thorpej ;
680 1.1 thorpej }
681 1.1 thorpej
682 1.1 thorpej struct mbuf *
683 1.1 thorpej epget(sc, totlen)
684 1.1 thorpej struct ep_softc *sc;
685 1.1 thorpej int totlen;
686 1.1 thorpej {
687 1.1 thorpej struct ifnet *ifp = &sc->sc_arpcom.ac_if;
688 1.1 thorpej struct mbuf *top, **mp, *m;
689 1.1 thorpej int len;
690 1.1 thorpej int sh;
691 1.1 thorpej
692 1.1 thorpej m = sc->mb[sc->next_mb];
693 1.1 thorpej sc->mb[sc->next_mb] = 0;
694 1.1 thorpej if (m == 0) {
695 1.1 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
696 1.1 thorpej if (m == 0)
697 1.1 thorpej return 0;
698 1.1 thorpej } else {
699 1.1 thorpej /* If the queue is no longer full, refill. */
700 1.1 thorpej if (sc->last_mb == sc->next_mb)
701 1.1 thorpej timeout(epmbuffill, sc, 1);
702 1.1 thorpej /* Convert one of our saved mbuf's. */
703 1.1 thorpej sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
704 1.1 thorpej m->m_data = m->m_pktdat;
705 1.1 thorpej m->m_flags = M_PKTHDR;
706 1.1 thorpej }
707 1.1 thorpej m->m_pkthdr.rcvif = ifp;
708 1.1 thorpej m->m_pkthdr.len = totlen;
709 1.1 thorpej len = MHLEN;
710 1.1 thorpej top = 0;
711 1.1 thorpej mp = ⊤
712 1.1 thorpej
713 1.1 thorpej /*
714 1.1 thorpej * We read the packet at splhigh() so that an interrupt from another
715 1.1 thorpej * device doesn't cause the card's buffer to overflow while we're
716 1.1 thorpej * reading it. We may still lose packets at other times.
717 1.1 thorpej */
718 1.1 thorpej sh = splhigh();
719 1.1 thorpej
720 1.1 thorpej while (totlen > 0) {
721 1.1 thorpej if (top) {
722 1.1 thorpej m = sc->mb[sc->next_mb];
723 1.1 thorpej sc->mb[sc->next_mb] = 0;
724 1.1 thorpej if (m == 0) {
725 1.1 thorpej MGET(m, M_DONTWAIT, MT_DATA);
726 1.1 thorpej if (m == 0) {
727 1.1 thorpej splx(sh);
728 1.1 thorpej m_freem(top);
729 1.1 thorpej return 0;
730 1.1 thorpej }
731 1.1 thorpej } else {
732 1.1 thorpej sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
733 1.1 thorpej }
734 1.1 thorpej len = MLEN;
735 1.1 thorpej }
736 1.1 thorpej if (totlen >= MINCLSIZE) {
737 1.1 thorpej MCLGET(m, M_DONTWAIT);
738 1.1 thorpej if (m->m_flags & M_EXT)
739 1.1 thorpej len = MCLBYTES;
740 1.1 thorpej }
741 1.1 thorpej len = min(totlen, len);
742 1.1 thorpej if (EP_IS_BUS_32(sc->bustype)) {
743 1.1 thorpej if (len > 3) {
744 1.1 thorpej len &= ~3;
745 1.1 thorpej insl(BASE + EP_W1_RX_PIO_RD_1,
746 1.1 thorpej mtod(m, caddr_t), len / 4);
747 1.1 thorpej } else
748 1.1 thorpej insb(BASE + EP_W1_RX_PIO_RD_1,
749 1.1 thorpej mtod(m, caddr_t), len);
750 1.1 thorpej } else {
751 1.1 thorpej if (len > 1) {
752 1.1 thorpej len &= ~1;
753 1.1 thorpej insw(BASE + EP_W1_RX_PIO_RD_1,
754 1.1 thorpej mtod(m, caddr_t), len / 2);
755 1.1 thorpej } else
756 1.1 thorpej *(mtod(m, caddr_t)) =
757 1.1 thorpej inb(BASE + EP_W1_RX_PIO_RD_1);
758 1.1 thorpej }
759 1.1 thorpej m->m_len = len;
760 1.1 thorpej totlen -= len;
761 1.1 thorpej *mp = m;
762 1.1 thorpej mp = &m->m_next;
763 1.1 thorpej }
764 1.1 thorpej
765 1.1 thorpej outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
766 1.1 thorpej while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
767 1.1 thorpej ;
768 1.1 thorpej
769 1.1 thorpej splx(sh);
770 1.1 thorpej
771 1.1 thorpej return top;
772 1.1 thorpej }
773 1.1 thorpej
774 1.1 thorpej int
775 1.1 thorpej epioctl(ifp, cmd, data)
776 1.1 thorpej register struct ifnet *ifp;
777 1.1 thorpej u_long cmd;
778 1.1 thorpej caddr_t data;
779 1.1 thorpej {
780 1.1 thorpej struct ep_softc *sc = ep_cd.cd_devs[ifp->if_unit];
781 1.1 thorpej struct ifaddr *ifa = (struct ifaddr *)data;
782 1.1 thorpej struct ifreq *ifr = (struct ifreq *)data;
783 1.1 thorpej int s, error = 0;
784 1.1 thorpej
785 1.1 thorpej s = splnet();
786 1.1 thorpej
787 1.1 thorpej switch (cmd) {
788 1.1 thorpej
789 1.1 thorpej case SIOCSIFADDR:
790 1.1 thorpej ifp->if_flags |= IFF_UP;
791 1.1 thorpej
792 1.1 thorpej switch (ifa->ifa_addr->sa_family) {
793 1.1 thorpej #ifdef INET
794 1.1 thorpej case AF_INET:
795 1.1 thorpej epinit(sc);
796 1.1 thorpej arp_ifinit(&sc->sc_arpcom, ifa);
797 1.1 thorpej break;
798 1.1 thorpej #endif
799 1.1 thorpej #ifdef NS
800 1.1 thorpej case AF_NS:
801 1.1 thorpej {
802 1.1 thorpej register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
803 1.1 thorpej
804 1.1 thorpej if (ns_nullhost(*ina))
805 1.1 thorpej ina->x_host =
806 1.1 thorpej *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
807 1.1 thorpej else
808 1.1 thorpej bcopy(ina->x_host.c_host,
809 1.1 thorpej sc->sc_arpcom.ac_enaddr,
810 1.1 thorpej sizeof(sc->sc_arpcom.ac_enaddr));
811 1.1 thorpej /* Set new address. */
812 1.1 thorpej epinit(sc);
813 1.1 thorpej break;
814 1.1 thorpej }
815 1.1 thorpej #endif
816 1.1 thorpej default:
817 1.1 thorpej epinit(sc);
818 1.1 thorpej break;
819 1.1 thorpej }
820 1.1 thorpej break;
821 1.1 thorpej
822 1.1 thorpej case SIOCSIFFLAGS:
823 1.1 thorpej if ((ifp->if_flags & IFF_UP) == 0 &&
824 1.1 thorpej (ifp->if_flags & IFF_RUNNING) != 0) {
825 1.1 thorpej /*
826 1.1 thorpej * If interface is marked down and it is running, then
827 1.1 thorpej * stop it.
828 1.1 thorpej */
829 1.1 thorpej epstop(sc);
830 1.1 thorpej ifp->if_flags &= ~IFF_RUNNING;
831 1.1 thorpej } else if ((ifp->if_flags & IFF_UP) != 0 &&
832 1.1 thorpej (ifp->if_flags & IFF_RUNNING) == 0) {
833 1.1 thorpej /*
834 1.1 thorpej * If interface is marked up and it is stopped, then
835 1.1 thorpej * start it.
836 1.1 thorpej */
837 1.1 thorpej epinit(sc);
838 1.1 thorpej } else {
839 1.1 thorpej /*
840 1.1 thorpej * deal with flags changes:
841 1.1 thorpej * IFF_MULTICAST, IFF_PROMISC,
842 1.1 thorpej * IFF_LINK0, IFF_LINK1,
843 1.1 thorpej */
844 1.1 thorpej epsetfilter(sc);
845 1.1 thorpej epsetlink(sc);
846 1.1 thorpej }
847 1.1 thorpej break;
848 1.1 thorpej
849 1.1 thorpej case SIOCADDMULTI:
850 1.1 thorpej case SIOCDELMULTI:
851 1.1 thorpej error = (cmd == SIOCADDMULTI) ?
852 1.1 thorpej ether_addmulti(ifr, &sc->sc_arpcom) :
853 1.1 thorpej ether_delmulti(ifr, &sc->sc_arpcom);
854 1.1 thorpej
855 1.1 thorpej if (error == ENETRESET) {
856 1.1 thorpej /*
857 1.1 thorpej * Multicast list has changed; set the hardware filter
858 1.1 thorpej * accordingly.
859 1.1 thorpej */
860 1.1 thorpej epreset(sc);
861 1.1 thorpej error = 0;
862 1.1 thorpej }
863 1.1 thorpej break;
864 1.1 thorpej
865 1.1 thorpej default:
866 1.1 thorpej error = EINVAL;
867 1.1 thorpej break;
868 1.1 thorpej }
869 1.1 thorpej
870 1.1 thorpej splx(s);
871 1.1 thorpej return (error);
872 1.1 thorpej }
873 1.1 thorpej
874 1.1 thorpej void
875 1.1 thorpej epreset(sc)
876 1.1 thorpej struct ep_softc *sc;
877 1.1 thorpej {
878 1.1 thorpej int s;
879 1.1 thorpej
880 1.1 thorpej s = splnet();
881 1.1 thorpej epstop(sc);
882 1.1 thorpej epinit(sc);
883 1.1 thorpej splx(s);
884 1.1 thorpej }
885 1.1 thorpej
886 1.1 thorpej void
887 1.1 thorpej epwatchdog(unit)
888 1.1 thorpej int unit;
889 1.1 thorpej {
890 1.1 thorpej struct ep_softc *sc = ep_cd.cd_devs[unit];
891 1.1 thorpej
892 1.1 thorpej log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
893 1.1 thorpej ++sc->sc_arpcom.ac_if.if_oerrors;
894 1.1 thorpej
895 1.1 thorpej epreset(sc);
896 1.1 thorpej }
897 1.1 thorpej
898 1.1 thorpej void
899 1.1 thorpej epstop(sc)
900 1.1 thorpej register struct ep_softc *sc;
901 1.1 thorpej {
902 1.1 thorpej
903 1.1 thorpej outw(BASE + EP_COMMAND, RX_DISABLE);
904 1.1 thorpej outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
905 1.1 thorpej while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
906 1.1 thorpej ;
907 1.1 thorpej outw(BASE + EP_COMMAND, TX_DISABLE);
908 1.1 thorpej outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
909 1.1 thorpej outw(BASE + EP_COMMAND, RX_RESET);
910 1.1 thorpej outw(BASE + EP_COMMAND, TX_RESET);
911 1.1 thorpej outw(BASE + EP_COMMAND, C_INTR_LATCH);
912 1.1 thorpej outw(BASE + EP_COMMAND, SET_RD_0_MASK);
913 1.1 thorpej outw(BASE + EP_COMMAND, SET_INTR_MASK);
914 1.1 thorpej outw(BASE + EP_COMMAND, SET_RX_FILTER);
915 1.1 thorpej
916 1.1 thorpej epmbufempty(sc);
917 1.1 thorpej }
918 1.1 thorpej
919 1.1 thorpej /*
920 1.1 thorpej * We get eeprom data from the id_port given an offset into the
921 1.1 thorpej * eeprom. Basically; after the ID_sequence is sent to all of
922 1.1 thorpej * the cards; they enter the ID_CMD state where they will accept
923 1.1 thorpej * command requests. 0x80-0xbf loads the eeprom data. We then
924 1.1 thorpej * read the port 16 times and with every read; the cards check
925 1.1 thorpej * for contention (ie: if one card writes a 0 bit and another
926 1.1 thorpej * writes a 1 bit then the host sees a 0. At the end of the cycle;
927 1.1 thorpej * each card compares the data on the bus; if there is a difference
928 1.1 thorpej * then that card goes into ID_WAIT state again). In the meantime;
929 1.1 thorpej * one bit of data is returned in the AX register which is conveniently
930 1.1 thorpej * returned to us by inb(). Hence; we read 16 times getting one
931 1.1 thorpej * bit of data with each read.
932 1.1 thorpej */
933 1.1 thorpej u_short
934 1.1 thorpej epreadeeprom(id_port, offset)
935 1.1 thorpej int id_port;
936 1.1 thorpej int offset;
937 1.1 thorpej {
938 1.1 thorpej int i, data = 0;
939 1.1 thorpej
940 1.1 thorpej outb(id_port, 0x80 + offset);
941 1.1 thorpej delay(1000);
942 1.1 thorpej for (i = 0; i < 16; i++)
943 1.1 thorpej data = (data << 1) | (inw(id_port) & 1);
944 1.1 thorpej return (data);
945 1.1 thorpej }
946 1.1 thorpej
947 1.1 thorpej static int
948 1.1 thorpej epbusyeeprom(sc)
949 1.1 thorpej struct ep_softc *sc;
950 1.1 thorpej {
951 1.1 thorpej int i = 100, j;
952 1.1 thorpej
953 1.1 thorpej if (sc->bustype == EP_BUS_PCMCIA) {
954 1.1 thorpej delay(1000);
955 1.1 thorpej return 0;
956 1.1 thorpej }
957 1.1 thorpej
958 1.1 thorpej while (i--) {
959 1.1 thorpej j = inw(BASE + EP_W0_EEPROM_COMMAND);
960 1.1 thorpej if (j & EEPROM_BUSY)
961 1.1 thorpej delay(100);
962 1.1 thorpej else
963 1.1 thorpej break;
964 1.1 thorpej }
965 1.1 thorpej if (!i) {
966 1.1 thorpej printf("\n%s: eeprom failed to come ready\n",
967 1.1 thorpej sc->sc_dev.dv_xname);
968 1.1 thorpej return (1);
969 1.1 thorpej }
970 1.1 thorpej if (j & EEPROM_TST_MODE) {
971 1.1 thorpej printf("\n%s: erase pencil mark, or disable plug-n-play mode!\n",
972 1.1 thorpej sc->sc_dev.dv_xname);
973 1.1 thorpej return (1);
974 1.1 thorpej }
975 1.1 thorpej return (0);
976 1.1 thorpej }
977 1.1 thorpej
978 1.1 thorpej void
979 1.1 thorpej epmbuffill(sc)
980 1.1 thorpej struct ep_softc *sc;
981 1.1 thorpej {
982 1.1 thorpej int s, i;
983 1.1 thorpej
984 1.1 thorpej s = splnet();
985 1.1 thorpej i = sc->last_mb;
986 1.1 thorpej do {
987 1.1 thorpej if (sc->mb[i] == NULL)
988 1.1 thorpej MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
989 1.1 thorpej if (sc->mb[i] == NULL)
990 1.1 thorpej break;
991 1.1 thorpej i = (i + 1) % MAX_MBS;
992 1.1 thorpej } while (i != sc->next_mb);
993 1.1 thorpej sc->last_mb = i;
994 1.1 thorpej /* If the queue was not filled, try again. */
995 1.1 thorpej if (sc->last_mb != sc->next_mb)
996 1.1 thorpej timeout(epmbuffill, sc, 1);
997 1.1 thorpej splx(s);
998 1.1 thorpej }
999 1.1 thorpej
1000 1.1 thorpej void
1001 1.1 thorpej epmbufempty(sc)
1002 1.1 thorpej struct ep_softc *sc;
1003 1.1 thorpej {
1004 1.1 thorpej int s, i;
1005 1.1 thorpej
1006 1.1 thorpej s = splnet();
1007 1.1 thorpej for (i = 0; i<MAX_MBS; i++) {
1008 1.1 thorpej if (sc->mb[i]) {
1009 1.1 thorpej m_freem(sc->mb[i]);
1010 1.1 thorpej sc->mb[i] = NULL;
1011 1.1 thorpej }
1012 1.1 thorpej }
1013 1.1 thorpej sc->last_mb = sc->next_mb = 0;
1014 1.1 thorpej untimeout(epmbuffill, sc);
1015 1.1 thorpej splx(s);
1016 1.1 thorpej }
1017