dp8390.c revision 1.96 1 1.96 thorpej /* $NetBSD: dp8390.c,v 1.96 2020/01/29 14:14:55 thorpej Exp $ */
2 1.1 scottr
3 1.1 scottr /*
4 1.1 scottr * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5 1.1 scottr * adapters.
6 1.1 scottr *
7 1.1 scottr * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
8 1.1 scottr *
9 1.1 scottr * Copyright (C) 1993, David Greenman. This software may be used, modified,
10 1.1 scottr * copied, distributed, and sold, in both source and binary form provided that
11 1.1 scottr * the above copyright and these terms are retained. Under no circumstances is
12 1.1 scottr * the author responsible for the proper functioning of this software, nor does
13 1.1 scottr * the author assume any responsibility for damages incurred with its use.
14 1.1 scottr */
15 1.49 lukem
16 1.49 lukem #include <sys/cdefs.h>
17 1.96 thorpej __KERNEL_RCSID(0, "$NetBSD: dp8390.c,v 1.96 2020/01/29 14:14:55 thorpej Exp $");
18 1.1 scottr
19 1.12 jonathan #include "opt_inet.h"
20 1.1 scottr
21 1.1 scottr #include <sys/param.h>
22 1.1 scottr #include <sys/systm.h>
23 1.1 scottr #include <sys/device.h>
24 1.1 scottr #include <sys/errno.h>
25 1.1 scottr #include <sys/ioctl.h>
26 1.1 scottr #include <sys/mbuf.h>
27 1.1 scottr #include <sys/socket.h>
28 1.1 scottr #include <sys/syslog.h>
29 1.82 riastrad #include <sys/rndsource.h>
30 1.93 msaitoh #include <sys/bus.h>
31 1.20 thorpej
32 1.1 scottr #include <net/if.h>
33 1.1 scottr #include <net/if_dl.h>
34 1.1 scottr #include <net/if_types.h>
35 1.8 thorpej #include <net/if_media.h>
36 1.1 scottr #include <net/if_ether.h>
37 1.88 msaitoh #include <net/bpf.h>
38 1.1 scottr
39 1.1 scottr #ifdef INET
40 1.1 scottr #include <netinet/in.h>
41 1.1 scottr #include <netinet/in_systm.h>
42 1.1 scottr #include <netinet/in_var.h>
43 1.1 scottr #include <netinet/ip.h>
44 1.1 scottr #include <netinet/if_inarp.h>
45 1.1 scottr #endif
46 1.1 scottr
47 1.1 scottr #include <dev/ic/dp8390reg.h>
48 1.1 scottr #include <dev/ic/dp8390var.h>
49 1.1 scottr
50 1.1 scottr #ifdef DEBUG
51 1.51 kristerw int dp8390_debug = 0;
52 1.1 scottr #endif
53 1.1 scottr
54 1.77 tsutsui static void dp8390_xmit(struct dp8390_softc *);
55 1.1 scottr
56 1.77 tsutsui static void dp8390_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *);
57 1.77 tsutsui static int dp8390_ring_copy(struct dp8390_softc *, int, void *, u_short);
58 1.77 tsutsui static int dp8390_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
59 1.1 scottr
60 1.77 tsutsui static int dp8390_test_mem(struct dp8390_softc *);
61 1.1 scottr
62 1.42 thorpej /*
63 1.42 thorpej * Standard media init routine for the dp8390.
64 1.42 thorpej */
65 1.42 thorpej void
66 1.42 thorpej dp8390_media_init(struct dp8390_softc *sc)
67 1.42 thorpej {
68 1.42 thorpej
69 1.95 msaitoh sc->sc_ec.ec_ifmedia = &sc->sc_media;
70 1.42 thorpej ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
71 1.93 msaitoh ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
72 1.93 msaitoh ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
73 1.42 thorpej }
74 1.4 scottr
75 1.1 scottr /*
76 1.1 scottr * Do bus-independent setup.
77 1.1 scottr */
78 1.1 scottr int
79 1.70 dsl dp8390_config(struct dp8390_softc *sc)
80 1.1 scottr {
81 1.1 scottr struct ifnet *ifp = &sc->sc_ec.ec_if;
82 1.42 thorpej int rv;
83 1.1 scottr
84 1.1 scottr rv = 1;
85 1.1 scottr
86 1.76 tsutsui if (sc->test_mem == NULL)
87 1.1 scottr sc->test_mem = dp8390_test_mem;
88 1.76 tsutsui if (sc->read_hdr == NULL)
89 1.76 tsutsui sc->read_hdr = dp8390_read_hdr;
90 1.76 tsutsui if (sc->recv_int == NULL)
91 1.76 tsutsui sc->recv_int = dp8390_rint;
92 1.76 tsutsui if (sc->ring_copy == NULL)
93 1.76 tsutsui sc->ring_copy = dp8390_ring_copy;
94 1.76 tsutsui if (sc->write_mbuf == NULL)
95 1.76 tsutsui sc->write_mbuf = dp8390_write_mbuf;
96 1.1 scottr
97 1.1 scottr /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
98 1.1 scottr if ((sc->mem_size < 16384) ||
99 1.1 scottr (sc->sc_flags & DP8390_NO_MULTI_BUFFERING))
100 1.1 scottr sc->txb_cnt = 1;
101 1.27 enami else if (sc->mem_size < 8192 * 3)
102 1.27 enami sc->txb_cnt = 2;
103 1.1 scottr else
104 1.27 enami sc->txb_cnt = 3;
105 1.1 scottr
106 1.7 thorpej sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
107 1.1 scottr sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
108 1.1 scottr sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
109 1.74 tsutsui sc->mem_ring = sc->mem_start +
110 1.74 tsutsui ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
111 1.1 scottr sc->mem_end = sc->mem_start + sc->mem_size;
112 1.1 scottr
113 1.1 scottr /* Now zero memory and verify that it is clear. */
114 1.1 scottr if ((*sc->test_mem)(sc))
115 1.1 scottr goto out;
116 1.1 scottr
117 1.1 scottr /* Set interface to stopped condition (reset). */
118 1.1 scottr dp8390_stop(sc);
119 1.1 scottr
120 1.1 scottr /* Initialize ifnet structure. */
121 1.68 cube strcpy(ifp->if_xname, device_xname(sc->sc_dev));
122 1.1 scottr ifp->if_softc = sc;
123 1.1 scottr ifp->if_start = dp8390_start;
124 1.1 scottr ifp->if_ioctl = dp8390_ioctl;
125 1.77 tsutsui if (ifp->if_watchdog == NULL)
126 1.1 scottr ifp->if_watchdog = dp8390_watchdog;
127 1.92 msaitoh ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
128 1.41 thorpej IFQ_SET_READY(&ifp->if_snd);
129 1.1 scottr
130 1.43 thorpej /* Print additional info when attached. */
131 1.68 cube aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
132 1.43 thorpej ether_sprintf(sc->sc_enaddr));
133 1.43 thorpej
134 1.8 thorpej /* Initialize media goo. */
135 1.42 thorpej (*sc->sc_media_init)(sc);
136 1.42 thorpej
137 1.93 msaitoh /* We can support 802.1Q VLAN-sized frames. */
138 1.39 bouyer sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
139 1.8 thorpej
140 1.1 scottr /* Attach the interface. */
141 1.1 scottr if_attach(ifp);
142 1.87 ozaki if_deferred_start_init(ifp, NULL);
143 1.1 scottr ether_ifattach(ifp, sc->sc_enaddr);
144 1.1 scottr
145 1.68 cube rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
146 1.81 tls RND_TYPE_NET, RND_FLAG_DEFAULT);
147 1.1 scottr
148 1.37 jhawk /* The attach is successful. */
149 1.37 jhawk sc->sc_flags |= DP8390_ATTACHED;
150 1.37 jhawk
151 1.1 scottr rv = 0;
152 1.77 tsutsui out:
153 1.77 tsutsui return rv;
154 1.1 scottr }
155 1.1 scottr
156 1.1 scottr /*
157 1.8 thorpej * Media change callback.
158 1.8 thorpej */
159 1.8 thorpej int
160 1.70 dsl dp8390_mediachange(struct ifnet *ifp)
161 1.8 thorpej {
162 1.8 thorpej struct dp8390_softc *sc = ifp->if_softc;
163 1.8 thorpej
164 1.8 thorpej if (sc->sc_mediachange)
165 1.77 tsutsui return (*sc->sc_mediachange)(sc);
166 1.77 tsutsui return 0;
167 1.8 thorpej }
168 1.8 thorpej
169 1.8 thorpej /*
170 1.8 thorpej * Media status callback.
171 1.8 thorpej */
172 1.8 thorpej void
173 1.70 dsl dp8390_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
174 1.8 thorpej {
175 1.8 thorpej struct dp8390_softc *sc = ifp->if_softc;
176 1.8 thorpej
177 1.8 thorpej if (sc->sc_enabled == 0) {
178 1.8 thorpej ifmr->ifm_active = IFM_ETHER | IFM_NONE;
179 1.8 thorpej ifmr->ifm_status = 0;
180 1.8 thorpej return;
181 1.8 thorpej }
182 1.8 thorpej
183 1.8 thorpej if (sc->sc_mediastatus)
184 1.8 thorpej (*sc->sc_mediastatus)(sc, ifmr);
185 1.8 thorpej }
186 1.8 thorpej
187 1.8 thorpej /*
188 1.1 scottr * Reset interface.
189 1.1 scottr */
190 1.1 scottr void
191 1.70 dsl dp8390_reset(struct dp8390_softc *sc)
192 1.1 scottr {
193 1.77 tsutsui int s;
194 1.1 scottr
195 1.1 scottr s = splnet();
196 1.1 scottr dp8390_stop(sc);
197 1.1 scottr dp8390_init(sc);
198 1.1 scottr splx(s);
199 1.1 scottr }
200 1.1 scottr
201 1.1 scottr /*
202 1.1 scottr * Take interface offline.
203 1.1 scottr */
204 1.1 scottr void
205 1.70 dsl dp8390_stop(struct dp8390_softc *sc)
206 1.1 scottr {
207 1.1 scottr bus_space_tag_t regt = sc->sc_regt;
208 1.1 scottr bus_space_handle_t regh = sc->sc_regh;
209 1.1 scottr int n = 5000;
210 1.1 scottr
211 1.1 scottr /* Stop everything on the interface, and select page 0 registers. */
212 1.34 ws NIC_BARRIER(regt, regh);
213 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
214 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
215 1.34 ws NIC_BARRIER(regt, regh);
216 1.1 scottr
217 1.1 scottr /*
218 1.1 scottr * Wait for interface to enter stopped state, but limit # of checks to
219 1.1 scottr * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
220 1.1 scottr * just in case it's an old one.
221 1.1 scottr */
222 1.77 tsutsui while (((NIC_GET(regt, regh, ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
223 1.34 ws DELAY(1);
224 1.44 thorpej
225 1.44 thorpej if (sc->stop_card != NULL)
226 1.44 thorpej (*sc->stop_card)(sc);
227 1.1 scottr }
228 1.1 scottr
229 1.1 scottr /*
230 1.1 scottr * Device timeout/watchdog routine. Entered if the device neglects to generate
231 1.1 scottr * an interrupt after a transmit has been started on it.
232 1.1 scottr */
233 1.1 scottr
234 1.1 scottr void
235 1.70 dsl dp8390_watchdog(struct ifnet *ifp)
236 1.1 scottr {
237 1.1 scottr struct dp8390_softc *sc = ifp->if_softc;
238 1.1 scottr
239 1.68 cube log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
240 1.96 thorpej if_statinc(ifp, if_oerrors);
241 1.1 scottr
242 1.1 scottr dp8390_reset(sc);
243 1.1 scottr }
244 1.1 scottr
245 1.1 scottr /*
246 1.1 scottr * Initialize device.
247 1.1 scottr */
248 1.1 scottr void
249 1.70 dsl dp8390_init(struct dp8390_softc *sc)
250 1.1 scottr {
251 1.1 scottr bus_space_tag_t regt = sc->sc_regt;
252 1.1 scottr bus_space_handle_t regh = sc->sc_regh;
253 1.1 scottr struct ifnet *ifp = &sc->sc_ec.ec_if;
254 1.77 tsutsui uint8_t mcaf[8];
255 1.1 scottr int i;
256 1.1 scottr
257 1.1 scottr /*
258 1.1 scottr * Initialize the NIC in the exact order outlined in the NS manual.
259 1.1 scottr * This init procedure is "mandatory"...don't change what or when
260 1.1 scottr * things happen.
261 1.1 scottr */
262 1.1 scottr
263 1.1 scottr /* Reset transmitter flags. */
264 1.1 scottr ifp->if_timer = 0;
265 1.1 scottr
266 1.1 scottr sc->txb_inuse = 0;
267 1.1 scottr sc->txb_new = 0;
268 1.1 scottr sc->txb_next_tx = 0;
269 1.1 scottr
270 1.1 scottr /* Set interface for page 0, remote DMA complete, stopped. */
271 1.34 ws NIC_BARRIER(regt, regh);
272 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
273 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
274 1.34 ws NIC_BARRIER(regt, regh);
275 1.1 scottr
276 1.2 scottr if (sc->dcr_reg & ED_DCR_LS) {
277 1.3 scottr NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
278 1.2 scottr } else {
279 1.1 scottr /*
280 1.1 scottr * Set FIFO threshold to 8, No auto-init Remote DMA, byte
281 1.2 scottr * order=80x86, byte-wide DMA xfers,
282 1.1 scottr */
283 1.1 scottr NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
284 1.1 scottr }
285 1.1 scottr
286 1.1 scottr /* Clear remote byte count registers. */
287 1.1 scottr NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
288 1.1 scottr NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
289 1.1 scottr
290 1.1 scottr /* Tell RCR to do nothing for now. */
291 1.33 enami NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON | sc->rcr_proto);
292 1.1 scottr
293 1.1 scottr /* Place NIC in internal loopback mode. */
294 1.1 scottr NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
295 1.1 scottr
296 1.1 scottr /* Set lower bits of byte addressable framing to 0. */
297 1.1 scottr if (sc->is790)
298 1.1 scottr NIC_PUT(regt, regh, 0x09, 0);
299 1.1 scottr
300 1.1 scottr /* Initialize receive buffer ring. */
301 1.1 scottr NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
302 1.1 scottr NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
303 1.1 scottr NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
304 1.1 scottr
305 1.1 scottr /*
306 1.1 scottr * Enable the following interrupts: receive/transmit complete,
307 1.1 scottr * receive/transmit error, and Receiver OverWrite.
308 1.1 scottr *
309 1.1 scottr * Counter overflow and Remote DMA complete are *not* enabled.
310 1.1 scottr */
311 1.1 scottr NIC_PUT(regt, regh, ED_P0_IMR,
312 1.1 scottr ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
313 1.1 scottr ED_IMR_OVWE);
314 1.1 scottr
315 1.14 mycroft /*
316 1.14 mycroft * Clear all interrupts. A '1' in each bit position clears the
317 1.14 mycroft * corresponding flag.
318 1.14 mycroft */
319 1.14 mycroft NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
320 1.14 mycroft
321 1.1 scottr /* Program command register for page 1. */
322 1.34 ws NIC_BARRIER(regt, regh);
323 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
324 1.1 scottr sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
325 1.34 ws NIC_BARRIER(regt, regh);
326 1.1 scottr
327 1.1 scottr /* Copy out our station address. */
328 1.77 tsutsui for (i = 0; i < ETHER_ADDR_LEN; i++)
329 1.77 tsutsui NIC_PUT(regt, regh, ED_P1_PAR0 + i, CLLADDR(ifp->if_sadl)[i]);
330 1.1 scottr
331 1.1 scottr /* Set multicast filter on chip. */
332 1.1 scottr dp8390_getmcaf(&sc->sc_ec, mcaf);
333 1.1 scottr for (i = 0; i < 8; i++)
334 1.1 scottr NIC_PUT(regt, regh, ED_P1_MAR0 + i, mcaf[i]);
335 1.1 scottr
336 1.1 scottr /*
337 1.1 scottr * Set current page pointer to one page after the boundary pointer, as
338 1.1 scottr * recommended in the National manual.
339 1.1 scottr */
340 1.1 scottr sc->next_packet = sc->rec_page_start + 1;
341 1.1 scottr NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
342 1.1 scottr
343 1.1 scottr /* Program command register for page 0. */
344 1.34 ws NIC_BARRIER(regt, regh);
345 1.1 scottr NIC_PUT(regt, regh, ED_P1_CR,
346 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
347 1.34 ws NIC_BARRIER(regt, regh);
348 1.1 scottr
349 1.14 mycroft /* Accept broadcast and multicast packets by default. */
350 1.33 enami i = ED_RCR_AB | ED_RCR_AM | sc->rcr_proto;
351 1.1 scottr if (ifp->if_flags & IFF_PROMISC) {
352 1.1 scottr /*
353 1.1 scottr * Set promiscuous mode. Multicast filter was set earlier so
354 1.1 scottr * that we should receive all multicast packets.
355 1.1 scottr */
356 1.1 scottr i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
357 1.1 scottr }
358 1.1 scottr NIC_PUT(regt, regh, ED_P0_RCR, i);
359 1.1 scottr
360 1.1 scottr /* Take interface out of loopback. */
361 1.1 scottr NIC_PUT(regt, regh, ED_P0_TCR, 0);
362 1.1 scottr
363 1.1 scottr /* Do any card-specific initialization, if applicable. */
364 1.77 tsutsui if (sc->init_card != NULL)
365 1.1 scottr (*sc->init_card)(sc);
366 1.1 scottr
367 1.1 scottr /* Fire up the interface. */
368 1.34 ws NIC_BARRIER(regt, regh);
369 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
370 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
371 1.1 scottr
372 1.1 scottr /* Set 'running' flag, and clear output active flag. */
373 1.1 scottr ifp->if_flags |= IFF_RUNNING;
374 1.1 scottr ifp->if_flags &= ~IFF_OACTIVE;
375 1.1 scottr
376 1.1 scottr /* ...and attempt to start output. */
377 1.1 scottr dp8390_start(ifp);
378 1.1 scottr }
379 1.1 scottr
380 1.1 scottr /*
381 1.1 scottr * This routine actually starts the transmission on the interface.
382 1.1 scottr */
383 1.76 tsutsui static void
384 1.70 dsl dp8390_xmit(struct dp8390_softc *sc)
385 1.1 scottr {
386 1.1 scottr bus_space_tag_t regt = sc->sc_regt;
387 1.1 scottr bus_space_handle_t regh = sc->sc_regh;
388 1.1 scottr struct ifnet *ifp = &sc->sc_ec.ec_if;
389 1.1 scottr u_short len;
390 1.1 scottr
391 1.14 mycroft #ifdef DIAGNOSTIC
392 1.14 mycroft if ((sc->txb_next_tx + sc->txb_inuse) % sc->txb_cnt != sc->txb_new)
393 1.14 mycroft panic("dp8390_xmit: desync, next_tx=%d inuse=%d cnt=%d new=%d",
394 1.14 mycroft sc->txb_next_tx, sc->txb_inuse, sc->txb_cnt, sc->txb_new);
395 1.14 mycroft
396 1.14 mycroft if (sc->txb_inuse == 0)
397 1.50 provos panic("dp8390_xmit: no packets to xmit");
398 1.14 mycroft #endif
399 1.14 mycroft
400 1.1 scottr len = sc->txb_len[sc->txb_next_tx];
401 1.1 scottr
402 1.1 scottr /* Set NIC for page 0 register access. */
403 1.34 ws NIC_BARRIER(regt, regh);
404 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
405 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
406 1.34 ws NIC_BARRIER(regt, regh);
407 1.1 scottr
408 1.1 scottr /* Set TX buffer start page. */
409 1.77 tsutsui NIC_PUT(regt, regh, ED_P0_TPSR,
410 1.77 tsutsui sc->tx_page_start + sc->txb_next_tx * ED_TXBUF_SIZE);
411 1.1 scottr
412 1.1 scottr /* Set TX length. */
413 1.1 scottr NIC_PUT(regt, regh, ED_P0_TBCR0, len);
414 1.1 scottr NIC_PUT(regt, regh, ED_P0_TBCR1, len >> 8);
415 1.1 scottr
416 1.1 scottr /* Set page 0, remote DMA complete, transmit packet, and *start*. */
417 1.34 ws NIC_BARRIER(regt, regh);
418 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
419 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
420 1.1 scottr
421 1.1 scottr /* Point to next transmit buffer slot and wrap if necessary. */
422 1.14 mycroft if (++sc->txb_next_tx == sc->txb_cnt)
423 1.1 scottr sc->txb_next_tx = 0;
424 1.1 scottr
425 1.1 scottr /* Set a timer just in case we never hear from the board again. */
426 1.1 scottr ifp->if_timer = 2;
427 1.1 scottr }
428 1.1 scottr
429 1.1 scottr /*
430 1.1 scottr * Start output on interface.
431 1.1 scottr * We make two assumptions here:
432 1.1 scottr * 1) that the current priority is set to splnet _before_ this code
433 1.1 scottr * is called *and* is returned to the appropriate priority after
434 1.1 scottr * return
435 1.1 scottr * 2) that the IFF_OACTIVE flag is checked before this code is called
436 1.1 scottr * (i.e. that the output part of the interface is idle)
437 1.1 scottr */
438 1.1 scottr void
439 1.70 dsl dp8390_start(struct ifnet *ifp)
440 1.1 scottr {
441 1.1 scottr struct dp8390_softc *sc = ifp->if_softc;
442 1.1 scottr struct mbuf *m0;
443 1.1 scottr int buffer;
444 1.1 scottr int len;
445 1.1 scottr
446 1.1 scottr if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
447 1.1 scottr return;
448 1.1 scottr
449 1.77 tsutsui outloop:
450 1.1 scottr /* See if there is room to put another packet in the buffer. */
451 1.1 scottr if (sc->txb_inuse == sc->txb_cnt) {
452 1.1 scottr /* No room. Indicate this to the outside world and exit. */
453 1.1 scottr ifp->if_flags |= IFF_OACTIVE;
454 1.1 scottr return;
455 1.1 scottr }
456 1.41 thorpej IFQ_DEQUEUE(&ifp->if_snd, m0);
457 1.77 tsutsui if (m0 == NULL)
458 1.1 scottr return;
459 1.1 scottr
460 1.1 scottr /* We need to use m->m_pkthdr.len, so require the header */
461 1.1 scottr if ((m0->m_flags & M_PKTHDR) == 0)
462 1.1 scottr panic("dp8390_start: no header mbuf");
463 1.1 scottr
464 1.1 scottr /* Tap off here if there is a BPF listener. */
465 1.89 msaitoh bpf_mtap(ifp, m0, BPF_D_OUT);
466 1.1 scottr
467 1.1 scottr /* txb_new points to next open buffer slot. */
468 1.1 scottr buffer = sc->mem_start +
469 1.1 scottr ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
470 1.1 scottr
471 1.76 tsutsui len = (*sc->write_mbuf)(sc, m0, buffer);
472 1.1 scottr
473 1.1 scottr m_freem(m0);
474 1.52 bouyer sc->txb_len[sc->txb_new] = len;
475 1.1 scottr
476 1.1 scottr /* Point to next buffer slot and wrap if necessary. */
477 1.1 scottr if (++sc->txb_new == sc->txb_cnt)
478 1.1 scottr sc->txb_new = 0;
479 1.1 scottr
480 1.14 mycroft /* Start the first packet transmitting. */
481 1.14 mycroft if (sc->txb_inuse++ == 0)
482 1.14 mycroft dp8390_xmit(sc);
483 1.1 scottr
484 1.1 scottr /* Loop back to the top to possibly buffer more packets. */
485 1.1 scottr goto outloop;
486 1.1 scottr }
487 1.1 scottr
488 1.1 scottr /*
489 1.1 scottr * Ethernet interface receiver interrupt.
490 1.1 scottr */
491 1.1 scottr void
492 1.70 dsl dp8390_rint(struct dp8390_softc *sc)
493 1.1 scottr {
494 1.1 scottr bus_space_tag_t regt = sc->sc_regt;
495 1.1 scottr bus_space_handle_t regh = sc->sc_regh;
496 1.1 scottr struct dp8390_ring packet_hdr;
497 1.1 scottr int packet_ptr;
498 1.77 tsutsui uint16_t len;
499 1.77 tsutsui uint8_t boundary, current;
500 1.77 tsutsui uint8_t nlen;
501 1.1 scottr
502 1.77 tsutsui loop:
503 1.1 scottr /* Set NIC to page 1 registers to get 'current' pointer. */
504 1.34 ws NIC_BARRIER(regt, regh);
505 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
506 1.1 scottr sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
507 1.34 ws NIC_BARRIER(regt, regh);
508 1.1 scottr
509 1.1 scottr /*
510 1.1 scottr * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
511 1.1 scottr * it points to where new data has been buffered. The 'CURR' (current)
512 1.1 scottr * register points to the logical end of the ring-buffer - i.e. it
513 1.1 scottr * points to where additional new data will be added. We loop here
514 1.1 scottr * until the logical beginning equals the logical end (or in other
515 1.1 scottr * words, until the ring-buffer is empty).
516 1.1 scottr */
517 1.1 scottr current = NIC_GET(regt, regh, ED_P1_CURR);
518 1.1 scottr if (sc->next_packet == current)
519 1.1 scottr return;
520 1.1 scottr
521 1.1 scottr /* Set NIC to page 0 registers to update boundary register. */
522 1.34 ws NIC_BARRIER(regt, regh);
523 1.1 scottr NIC_PUT(regt, regh, ED_P1_CR,
524 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
525 1.34 ws NIC_BARRIER(regt, regh);
526 1.1 scottr
527 1.1 scottr do {
528 1.1 scottr /* Get pointer to this buffer's header structure. */
529 1.1 scottr packet_ptr = sc->mem_ring +
530 1.1 scottr ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
531 1.1 scottr
532 1.76 tsutsui (*sc->read_hdr)(sc, packet_ptr, &packet_hdr);
533 1.1 scottr len = packet_hdr.count;
534 1.1 scottr
535 1.1 scottr /*
536 1.1 scottr * Try do deal with old, buggy chips that sometimes duplicate
537 1.1 scottr * the low byte of the length into the high byte. We do this
538 1.1 scottr * by simply ignoring the high byte of the length and always
539 1.1 scottr * recalculating it.
540 1.1 scottr *
541 1.1 scottr * NOTE: sc->next_packet is pointing at the current packet.
542 1.1 scottr */
543 1.1 scottr if (packet_hdr.next_packet >= sc->next_packet)
544 1.1 scottr nlen = (packet_hdr.next_packet - sc->next_packet);
545 1.1 scottr else
546 1.1 scottr nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
547 1.1 scottr (sc->rec_page_stop - sc->next_packet));
548 1.1 scottr --nlen;
549 1.1 scottr if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
550 1.1 scottr --nlen;
551 1.1 scottr len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
552 1.1 scottr #ifdef DIAGNOSTIC
553 1.1 scottr if (len != packet_hdr.count) {
554 1.68 cube aprint_verbose_dev(sc->sc_dev, "length does not match "
555 1.68 cube "next packet pointer\n");
556 1.68 cube aprint_verbose_dev(sc->sc_dev, "len %04x nlen %04x "
557 1.68 cube "start %02x first %02x curr %02x next %02x "
558 1.68 cube "stop %02x\n", packet_hdr.count, len,
559 1.1 scottr sc->rec_page_start, sc->next_packet, current,
560 1.1 scottr packet_hdr.next_packet, sc->rec_page_stop);
561 1.1 scottr }
562 1.1 scottr #endif
563 1.1 scottr
564 1.1 scottr /*
565 1.1 scottr * Be fairly liberal about what we allow as a "reasonable"
566 1.1 scottr * length so that a [crufty] packet will make it to BPF (and
567 1.1 scottr * can thus be analyzed). Note that all that is really
568 1.1 scottr * important is that we have a length that will fit into one
569 1.1 scottr * mbuf cluster or less; the upper layer protocols can then
570 1.1 scottr * figure out the length from their own length field(s).
571 1.1 scottr */
572 1.1 scottr if (len <= MCLBYTES &&
573 1.1 scottr packet_hdr.next_packet >= sc->rec_page_start &&
574 1.1 scottr packet_hdr.next_packet < sc->rec_page_stop) {
575 1.1 scottr /* Go get packet. */
576 1.1 scottr dp8390_read(sc,
577 1.1 scottr packet_ptr + sizeof(struct dp8390_ring),
578 1.1 scottr len - sizeof(struct dp8390_ring));
579 1.1 scottr } else {
580 1.1 scottr /* Really BAD. The ring pointers are corrupted. */
581 1.1 scottr log(LOG_ERR, "%s: NIC memory corrupt - "
582 1.1 scottr "invalid packet length %d\n",
583 1.68 cube device_xname(sc->sc_dev), len);
584 1.96 thorpej if_statinc(&sc->sc_ec.ec_if, if_ierrors);
585 1.1 scottr dp8390_reset(sc);
586 1.1 scottr return;
587 1.1 scottr }
588 1.1 scottr
589 1.1 scottr /* Update next packet pointer. */
590 1.1 scottr sc->next_packet = packet_hdr.next_packet;
591 1.1 scottr
592 1.1 scottr /*
593 1.1 scottr * Update NIC boundary pointer - being careful to keep it one
594 1.1 scottr * buffer behind (as recommended by NS databook).
595 1.1 scottr */
596 1.1 scottr boundary = sc->next_packet - 1;
597 1.1 scottr if (boundary < sc->rec_page_start)
598 1.1 scottr boundary = sc->rec_page_stop - 1;
599 1.1 scottr NIC_PUT(regt, regh, ED_P0_BNRY, boundary);
600 1.1 scottr } while (sc->next_packet != current);
601 1.1 scottr
602 1.1 scottr goto loop;
603 1.1 scottr }
604 1.1 scottr
605 1.1 scottr /* Ethernet interface interrupt processor. */
606 1.7 thorpej int
607 1.70 dsl dp8390_intr(void *arg)
608 1.1 scottr {
609 1.77 tsutsui struct dp8390_softc *sc = arg;
610 1.1 scottr bus_space_tag_t regt = sc->sc_regt;
611 1.1 scottr bus_space_handle_t regh = sc->sc_regh;
612 1.1 scottr struct ifnet *ifp = &sc->sc_ec.ec_if;
613 1.77 tsutsui uint8_t isr;
614 1.77 tsutsui uint8_t rndisr;
615 1.1 scottr
616 1.93 msaitoh if (sc->sc_enabled == 0 || !device_is_active(sc->sc_dev))
617 1.77 tsutsui return 0;
618 1.7 thorpej
619 1.1 scottr /* Set NIC to page 0 registers. */
620 1.34 ws NIC_BARRIER(regt, regh);
621 1.93 msaitoh NIC_PUT(regt, regh, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
622 1.34 ws NIC_BARRIER(regt, regh);
623 1.1 scottr
624 1.1 scottr isr = NIC_GET(regt, regh, ED_P0_ISR);
625 1.77 tsutsui if (isr == 0)
626 1.77 tsutsui return 0;
627 1.1 scottr
628 1.20 thorpej rndisr = isr;
629 1.20 thorpej
630 1.1 scottr /* Loop until there are no more new interrupts. */
631 1.1 scottr for (;;) {
632 1.1 scottr /*
633 1.1 scottr * Reset all the bits that we are 'acknowledging' by writing a
634 1.1 scottr * '1' to each bit position that was set.
635 1.1 scottr * (Writing a '1' *clears* the bit.)
636 1.1 scottr */
637 1.1 scottr NIC_PUT(regt, regh, ED_P0_ISR, isr);
638 1.33 enami
639 1.33 enami /* Work around for AX88190 bug */
640 1.33 enami if ((sc->sc_flags & DP8390_DO_AX88190_WORKAROUND) != 0)
641 1.33 enami while ((NIC_GET(regt, regh, ED_P0_ISR) & isr) != 0) {
642 1.33 enami NIC_PUT(regt, regh, ED_P0_ISR, 0);
643 1.33 enami NIC_PUT(regt, regh, ED_P0_ISR, isr);
644 1.33 enami }
645 1.1 scottr
646 1.1 scottr /*
647 1.1 scottr * Handle transmitter interrupts. Handle these first because
648 1.1 scottr * the receiver will reset the board under some conditions.
649 1.14 mycroft *
650 1.14 mycroft * If the chip was reset while a packet was transmitting, it
651 1.14 mycroft * may still deliver a TX interrupt. In this case, just ignore
652 1.14 mycroft * the interrupt.
653 1.1 scottr */
654 1.77 tsutsui if ((isr & (ED_ISR_PTX | ED_ISR_TXE)) != 0 &&
655 1.14 mycroft sc->txb_inuse != 0) {
656 1.96 thorpej net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
657 1.77 tsutsui uint8_t collisions =
658 1.14 mycroft NIC_GET(regt, regh, ED_P0_NCR) & 0x0f;
659 1.1 scottr
660 1.1 scottr /*
661 1.1 scottr * Check for transmit error. If a TX completed with an
662 1.1 scottr * error, we end up throwing the packet away. Really
663 1.1 scottr * the only error that is possible is excessive
664 1.1 scottr * collisions, and in this case it is best to allow the
665 1.1 scottr * automatic mechanisms of TCP to backoff the flow. Of
666 1.1 scottr * course, with UDP we're screwed, but this is expected
667 1.1 scottr * when a network is heavily loaded.
668 1.1 scottr */
669 1.77 tsutsui if ((isr & ED_ISR_TXE) != 0) {
670 1.93 msaitoh /* Excessive collisions (16). */
671 1.1 scottr if ((NIC_GET(regt, regh, ED_P0_TSR)
672 1.1 scottr & ED_TSR_ABT) && (collisions == 0)) {
673 1.1 scottr /*
674 1.1 scottr * When collisions total 16, the P0_NCR
675 1.1 scottr * will indicate 0, and the TSR_ABT is
676 1.1 scottr * set.
677 1.1 scottr */
678 1.1 scottr collisions = 16;
679 1.1 scottr }
680 1.1 scottr
681 1.1 scottr /* Update output errors counter. */
682 1.96 thorpej if_statinc_ref(nsr, if_oerrors);
683 1.1 scottr } else {
684 1.1 scottr /*
685 1.14 mycroft * Throw away the non-error status bits.
686 1.14 mycroft *
687 1.14 mycroft * XXX
688 1.14 mycroft * It may be useful to detect loss of carrier
689 1.14 mycroft * and late collisions here.
690 1.14 mycroft */
691 1.14 mycroft (void)NIC_GET(regt, regh, ED_P0_TSR);
692 1.14 mycroft
693 1.14 mycroft /*
694 1.1 scottr * Update total number of successfully
695 1.1 scottr * transmitted packets.
696 1.1 scottr */
697 1.96 thorpej if_statinc_ref(nsr, if_opackets);
698 1.1 scottr }
699 1.1 scottr
700 1.1 scottr /* Clear watchdog timer. */
701 1.1 scottr ifp->if_timer = 0;
702 1.1 scottr ifp->if_flags &= ~IFF_OACTIVE;
703 1.1 scottr
704 1.1 scottr /*
705 1.1 scottr * Add in total number of collisions on last
706 1.1 scottr * transmission.
707 1.1 scottr */
708 1.96 thorpej if (collisions)
709 1.96 thorpej if_statadd_ref(nsr, if_collisions, collisions);
710 1.96 thorpej
711 1.96 thorpej IF_STAT_PUTREF(ifp);
712 1.1 scottr
713 1.1 scottr /*
714 1.1 scottr * Decrement buffer in-use count if not zero (can only
715 1.93 msaitoh * be zero if a transmitter interrupt occurred while
716 1.93 msaitoh * not actually transmitting).
717 1.1 scottr * If data is ready to transmit, start it transmitting,
718 1.1 scottr * otherwise defer until after handling receiver.
719 1.1 scottr */
720 1.14 mycroft if (--sc->txb_inuse != 0)
721 1.1 scottr dp8390_xmit(sc);
722 1.1 scottr }
723 1.1 scottr
724 1.1 scottr /* Handle receiver interrupts. */
725 1.77 tsutsui if ((isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) != 0) {
726 1.1 scottr /*
727 1.1 scottr * Overwrite warning. In order to make sure that a
728 1.1 scottr * lockup of the local DMA hasn't occurred, we reset
729 1.1 scottr * and re-init the NIC. The NSC manual suggests only a
730 1.1 scottr * partial reset/re-init is necessary - but some chips
731 1.1 scottr * seem to want more. The DMA lockup has been seen
732 1.1 scottr * only with early rev chips - Methinks this bug was
733 1.1 scottr * fixed in later revs. -DG
734 1.1 scottr */
735 1.77 tsutsui if ((isr & ED_ISR_OVW) != 0) {
736 1.96 thorpej if_statinc(ifp, if_ierrors);
737 1.1 scottr #ifdef DIAGNOSTIC
738 1.1 scottr log(LOG_WARNING, "%s: warning - receiver "
739 1.1 scottr "ring buffer overrun\n",
740 1.68 cube device_xname(sc->sc_dev));
741 1.1 scottr #endif
742 1.1 scottr /* Stop/reset/re-init NIC. */
743 1.1 scottr dp8390_reset(sc);
744 1.1 scottr } else {
745 1.1 scottr /*
746 1.1 scottr * Receiver Error. One or more of: CRC error,
747 1.1 scottr * frame alignment error FIFO overrun, or
748 1.1 scottr * missed packet.
749 1.1 scottr */
750 1.77 tsutsui if ((isr & ED_ISR_RXE) != 0) {
751 1.96 thorpej if_statinc(ifp, if_ierrors);
752 1.1 scottr #ifdef DEBUG
753 1.4 scottr if (dp8390_debug) {
754 1.4 scottr printf("%s: receive error %x\n",
755 1.68 cube device_xname(sc->sc_dev),
756 1.4 scottr NIC_GET(regt, regh,
757 1.4 scottr ED_P0_RSR));
758 1.4 scottr }
759 1.1 scottr #endif
760 1.1 scottr }
761 1.1 scottr
762 1.1 scottr /*
763 1.1 scottr * Go get the packet(s)
764 1.1 scottr * XXX - Doing this on an error is dubious
765 1.1 scottr * because there shouldn't be any data to get
766 1.1 scottr * (we've configured the interface to not
767 1.1 scottr * accept packets with errors).
768 1.1 scottr */
769 1.76 tsutsui (*sc->recv_int)(sc);
770 1.1 scottr }
771 1.1 scottr }
772 1.1 scottr
773 1.1 scottr /*
774 1.1 scottr * If it looks like the transmitter can take more data, attempt
775 1.1 scottr * to start output on the interface. This is done after
776 1.1 scottr * handling the receiver to give the receiver priority.
777 1.1 scottr */
778 1.87 ozaki if_schedule_deferred_start(ifp);
779 1.1 scottr
780 1.1 scottr /*
781 1.1 scottr * Return NIC CR to standard state: page 0, remote DMA
782 1.1 scottr * complete, start (toggling the TXP bit off, even if was just
783 1.1 scottr * set in the transmit routine, is *okay* - it is 'edge'
784 1.1 scottr * triggered from low to high).
785 1.1 scottr */
786 1.34 ws NIC_BARRIER(regt, regh);
787 1.1 scottr NIC_PUT(regt, regh, ED_P0_CR,
788 1.1 scottr sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
789 1.34 ws NIC_BARRIER(regt, regh);
790 1.1 scottr
791 1.1 scottr /*
792 1.1 scottr * If the Network Talley Counters overflow, read them to reset
793 1.1 scottr * them. It appears that old 8390's won't clear the ISR flag
794 1.1 scottr * otherwise - resulting in an infinite loop.
795 1.1 scottr */
796 1.77 tsutsui if ((isr & ED_ISR_CNT) != 0) {
797 1.1 scottr (void)NIC_GET(regt, regh, ED_P0_CNTR0);
798 1.1 scottr (void)NIC_GET(regt, regh, ED_P0_CNTR1);
799 1.1 scottr (void)NIC_GET(regt, regh, ED_P0_CNTR2);
800 1.1 scottr }
801 1.1 scottr
802 1.1 scottr isr = NIC_GET(regt, regh, ED_P0_ISR);
803 1.77 tsutsui if (isr == 0)
804 1.20 thorpej goto out;
805 1.1 scottr }
806 1.20 thorpej
807 1.20 thorpej out:
808 1.20 thorpej rnd_add_uint32(&sc->rnd_source, rndisr);
809 1.77 tsutsui return 1;
810 1.1 scottr }
811 1.1 scottr
812 1.1 scottr /*
813 1.1 scottr * Process an ioctl request. This code needs some work - it looks pretty ugly.
814 1.1 scottr */
815 1.1 scottr int
816 1.70 dsl dp8390_ioctl(struct ifnet *ifp, u_long cmd, void *data)
817 1.1 scottr {
818 1.1 scottr struct dp8390_softc *sc = ifp->if_softc;
819 1.77 tsutsui struct ifaddr *ifa = data;
820 1.8 thorpej int s, error = 0;
821 1.1 scottr
822 1.1 scottr s = splnet();
823 1.1 scottr
824 1.1 scottr switch (cmd) {
825 1.1 scottr
826 1.69 dyoung case SIOCINITIFADDR:
827 1.7 thorpej if ((error = dp8390_enable(sc)) != 0)
828 1.7 thorpej break;
829 1.1 scottr ifp->if_flags |= IFF_UP;
830 1.1 scottr
831 1.69 dyoung dp8390_init(sc);
832 1.1 scottr switch (ifa->ifa_addr->sa_family) {
833 1.1 scottr #ifdef INET
834 1.1 scottr case AF_INET:
835 1.1 scottr arp_ifinit(ifp, ifa);
836 1.1 scottr break;
837 1.1 scottr #endif
838 1.1 scottr default:
839 1.1 scottr break;
840 1.1 scottr }
841 1.1 scottr break;
842 1.1 scottr
843 1.1 scottr case SIOCSIFFLAGS:
844 1.69 dyoung if ((error = ifioctl_common(ifp, cmd, data)) != 0)
845 1.69 dyoung break;
846 1.93 msaitoh switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
847 1.67 dyoung case IFF_RUNNING:
848 1.1 scottr /*
849 1.1 scottr * If interface is marked down and it is running, then
850 1.1 scottr * stop it.
851 1.1 scottr */
852 1.1 scottr dp8390_stop(sc);
853 1.1 scottr ifp->if_flags &= ~IFF_RUNNING;
854 1.7 thorpej dp8390_disable(sc);
855 1.67 dyoung break;
856 1.67 dyoung case IFF_UP:
857 1.1 scottr /*
858 1.1 scottr * If interface is marked up and it is stopped, then
859 1.1 scottr * start it.
860 1.1 scottr */
861 1.7 thorpej if ((error = dp8390_enable(sc)) != 0)
862 1.7 thorpej break;
863 1.1 scottr dp8390_init(sc);
864 1.67 dyoung break;
865 1.93 msaitoh case IFF_UP | IFF_RUNNING:
866 1.1 scottr /*
867 1.1 scottr * Reset the interface to pick up changes in any other
868 1.1 scottr * flags that affect hardware registers.
869 1.1 scottr */
870 1.1 scottr dp8390_stop(sc);
871 1.1 scottr dp8390_init(sc);
872 1.67 dyoung break;
873 1.67 dyoung default:
874 1.67 dyoung break;
875 1.1 scottr }
876 1.1 scottr break;
877 1.1 scottr
878 1.1 scottr case SIOCADDMULTI:
879 1.1 scottr case SIOCDELMULTI:
880 1.7 thorpej if (sc->sc_enabled == 0) {
881 1.7 thorpej error = EIO;
882 1.7 thorpej break;
883 1.7 thorpej }
884 1.7 thorpej
885 1.1 scottr /* Update our multicast list. */
886 1.65 dyoung if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
887 1.1 scottr /*
888 1.1 scottr * Multicast list has changed; set the hardware filter
889 1.1 scottr * accordingly.
890 1.1 scottr */
891 1.54 thorpej if (ifp->if_flags & IFF_RUNNING) {
892 1.54 thorpej dp8390_stop(sc); /* XXX for ds_setmcaf? */
893 1.54 thorpej dp8390_init(sc);
894 1.54 thorpej }
895 1.1 scottr error = 0;
896 1.1 scottr }
897 1.8 thorpej break;
898 1.8 thorpej
899 1.1 scottr default:
900 1.69 dyoung error = ether_ioctl(ifp, cmd, data);
901 1.1 scottr break;
902 1.1 scottr }
903 1.1 scottr
904 1.1 scottr splx(s);
905 1.77 tsutsui return error;
906 1.1 scottr }
907 1.1 scottr
908 1.1 scottr /*
909 1.1 scottr * Retrieve packet from buffer memory and send to the next level up via
910 1.1 scottr * ether_input(). If there is a BPF listener, give a copy to BPF, too.
911 1.1 scottr */
912 1.1 scottr void
913 1.70 dsl dp8390_read(struct dp8390_softc *sc, int buf, u_short len)
914 1.1 scottr {
915 1.1 scottr struct ifnet *ifp = &sc->sc_ec.ec_if;
916 1.1 scottr struct mbuf *m;
917 1.1 scottr
918 1.1 scottr /* Pull packet off interface. */
919 1.1 scottr m = dp8390_get(sc, buf, len);
920 1.77 tsutsui if (m == NULL) {
921 1.96 thorpej if_statinc(ifp, if_ierrors);
922 1.1 scottr return;
923 1.1 scottr }
924 1.1 scottr
925 1.83 ozaki if_percpuq_enqueue(ifp->if_percpuq, m);
926 1.1 scottr }
927 1.1 scottr
928 1.1 scottr
929 1.1 scottr /*
930 1.1 scottr * Supporting routines.
931 1.1 scottr */
932 1.1 scottr
933 1.1 scottr /*
934 1.1 scottr * Compute the multicast address filter from the list of multicast addresses we
935 1.1 scottr * need to listen to.
936 1.1 scottr */
937 1.1 scottr void
938 1.77 tsutsui dp8390_getmcaf(struct ethercom *ec, uint8_t *af)
939 1.1 scottr {
940 1.1 scottr struct ifnet *ifp = &ec->ec_if;
941 1.1 scottr struct ether_multi *enm;
942 1.77 tsutsui uint32_t crc;
943 1.36 thorpej int i;
944 1.1 scottr struct ether_multistep step;
945 1.1 scottr
946 1.1 scottr /*
947 1.1 scottr * Set up multicast address filter by passing all multicast addresses
948 1.1 scottr * through a crc generator, and then using the high order 6 bits as an
949 1.1 scottr * index into the 64 bit logical address filter. The high order bit
950 1.1 scottr * selects the word, while the rest of the bits select the bit within
951 1.1 scottr * the word.
952 1.1 scottr */
953 1.1 scottr
954 1.1 scottr if (ifp->if_flags & IFF_PROMISC) {
955 1.1 scottr ifp->if_flags |= IFF_ALLMULTI;
956 1.1 scottr for (i = 0; i < 8; i++)
957 1.1 scottr af[i] = 0xff;
958 1.1 scottr return;
959 1.1 scottr }
960 1.1 scottr for (i = 0; i < 8; i++)
961 1.1 scottr af[i] = 0;
962 1.94 msaitoh ETHER_LOCK(ec);
963 1.1 scottr ETHER_FIRST_MULTI(step, ec, enm);
964 1.1 scottr while (enm != NULL) {
965 1.45 thorpej if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
966 1.1 scottr sizeof(enm->enm_addrlo)) != 0) {
967 1.1 scottr /*
968 1.1 scottr * We must listen to a range of multicast addresses.
969 1.1 scottr * For now, just accept all multicasts, rather than
970 1.1 scottr * trying to set only those filter bits needed to match
971 1.1 scottr * the range. (At this time, the only use of address
972 1.1 scottr * ranges is for IP multicast routing, for which the
973 1.1 scottr * range is big enough to require all bits set.)
974 1.1 scottr */
975 1.1 scottr ifp->if_flags |= IFF_ALLMULTI;
976 1.1 scottr for (i = 0; i < 8; i++)
977 1.1 scottr af[i] = 0xff;
978 1.94 msaitoh ETHER_UNLOCK(ec);
979 1.1 scottr return;
980 1.1 scottr }
981 1.36 thorpej
982 1.36 thorpej crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
983 1.36 thorpej
984 1.1 scottr /* Just want the 6 most significant bits. */
985 1.1 scottr crc >>= 26;
986 1.1 scottr
987 1.1 scottr /* Turn on the corresponding bit in the filter. */
988 1.1 scottr af[crc >> 3] |= 1 << (crc & 0x7);
989 1.1 scottr
990 1.1 scottr ETHER_NEXT_MULTI(step, enm);
991 1.1 scottr }
992 1.94 msaitoh ETHER_UNLOCK(ec);
993 1.1 scottr ifp->if_flags &= ~IFF_ALLMULTI;
994 1.1 scottr }
995 1.1 scottr
996 1.1 scottr /*
997 1.17 bad * Copy data from receive buffer to a new mbuf chain allocating mbufs
998 1.17 bad * as needed. Return pointer to first mbuf in chain.
999 1.1 scottr * sc = dp8390 info (softc)
1000 1.1 scottr * src = pointer in dp8390 ring buffer
1001 1.17 bad * total_len = amount of data to copy
1002 1.1 scottr */
1003 1.1 scottr struct mbuf *
1004 1.70 dsl dp8390_get(struct dp8390_softc *sc, int src, u_short total_len)
1005 1.1 scottr {
1006 1.1 scottr struct ifnet *ifp = &sc->sc_ec.ec_if;
1007 1.18 bad struct mbuf *m, *m0, *newm;
1008 1.1 scottr u_short len;
1009 1.1 scottr
1010 1.19 mycroft MGETHDR(m0, M_DONTWAIT, MT_DATA);
1011 1.77 tsutsui if (m0 == NULL)
1012 1.77 tsutsui return NULL;
1013 1.85 ozaki m_set_rcvif(m0, ifp);
1014 1.19 mycroft m0->m_pkthdr.len = total_len;
1015 1.1 scottr len = MHLEN;
1016 1.19 mycroft m = m0;
1017 1.1 scottr
1018 1.1 scottr while (total_len > 0) {
1019 1.1 scottr if (total_len >= MINCLSIZE) {
1020 1.1 scottr MCLGET(m, M_DONTWAIT);
1021 1.19 mycroft if ((m->m_flags & M_EXT) == 0)
1022 1.19 mycroft goto bad;
1023 1.1 scottr len = MCLBYTES;
1024 1.1 scottr }
1025 1.11 thorpej
1026 1.93 msaitoh /* Make sure the data after the Ethernet header is aligned. */
1027 1.18 bad if (m == m0) {
1028 1.63 christos char *newdata = (char *)
1029 1.11 thorpej ALIGN(m->m_data + sizeof(struct ether_header)) -
1030 1.11 thorpej sizeof(struct ether_header);
1031 1.11 thorpej len -= newdata - m->m_data;
1032 1.11 thorpej m->m_data = newdata;
1033 1.11 thorpej }
1034 1.11 thorpej
1035 1.91 riastrad m->m_len = len = uimin(total_len, len);
1036 1.76 tsutsui src = (*sc->ring_copy)(sc, src, mtod(m, void *), len);
1037 1.19 mycroft
1038 1.1 scottr total_len -= len;
1039 1.18 bad if (total_len > 0) {
1040 1.18 bad MGET(newm, M_DONTWAIT, MT_DATA);
1041 1.77 tsutsui if (newm == NULL)
1042 1.19 mycroft goto bad;
1043 1.18 bad len = MLEN;
1044 1.19 mycroft m = m->m_next = newm;
1045 1.18 bad }
1046 1.1 scottr }
1047 1.1 scottr
1048 1.77 tsutsui return m0;
1049 1.19 mycroft
1050 1.77 tsutsui bad:
1051 1.19 mycroft m_freem(m0);
1052 1.77 tsutsui return NULL;
1053 1.1 scottr }
1054 1.1 scottr
1055 1.1 scottr
1056 1.1 scottr /*
1057 1.1 scottr * Default driver support functions.
1058 1.1 scottr *
1059 1.1 scottr * NOTE: all support functions assume 8-bit shared memory.
1060 1.1 scottr */
1061 1.1 scottr /*
1062 1.1 scottr * Zero NIC buffer memory and verify that it is clear.
1063 1.1 scottr */
1064 1.1 scottr static int
1065 1.70 dsl dp8390_test_mem(struct dp8390_softc *sc)
1066 1.1 scottr {
1067 1.1 scottr bus_space_tag_t buft = sc->sc_buft;
1068 1.1 scottr bus_space_handle_t bufh = sc->sc_bufh;
1069 1.1 scottr int i;
1070 1.1 scottr
1071 1.1 scottr bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size);
1072 1.1 scottr
1073 1.1 scottr for (i = 0; i < sc->mem_size; ++i) {
1074 1.1 scottr if (bus_space_read_1(buft, bufh, sc->mem_start + i)) {
1075 1.1 scottr printf(": failed to clear NIC buffer at offset %x - "
1076 1.1 scottr "check configuration\n", (sc->mem_start + i));
1077 1.1 scottr return 1;
1078 1.1 scottr }
1079 1.1 scottr }
1080 1.1 scottr
1081 1.1 scottr return 0;
1082 1.1 scottr }
1083 1.1 scottr
1084 1.1 scottr /*
1085 1.1 scottr * Read a packet header from the ring, given the source offset.
1086 1.1 scottr */
1087 1.76 tsutsui static void
1088 1.70 dsl dp8390_read_hdr(struct dp8390_softc *sc, int src, struct dp8390_ring *hdrp)
1089 1.1 scottr {
1090 1.1 scottr bus_space_tag_t buft = sc->sc_buft;
1091 1.1 scottr bus_space_handle_t bufh = sc->sc_bufh;
1092 1.1 scottr
1093 1.1 scottr /*
1094 1.1 scottr * The byte count includes a 4 byte header that was added by
1095 1.1 scottr * the NIC.
1096 1.1 scottr */
1097 1.1 scottr hdrp->rsr = bus_space_read_1(buft, bufh, src);
1098 1.1 scottr hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1);
1099 1.1 scottr hdrp->count = bus_space_read_1(buft, bufh, src + 2) |
1100 1.1 scottr (bus_space_read_1(buft, bufh, src + 3) << 8);
1101 1.1 scottr }
1102 1.1 scottr
1103 1.1 scottr /*
1104 1.1 scottr * Copy `amount' bytes from a packet in the ring buffer to a linear
1105 1.1 scottr * destination buffer, given a source offset and destination address.
1106 1.1 scottr * Takes into account ring-wrap.
1107 1.1 scottr */
1108 1.76 tsutsui static int
1109 1.70 dsl dp8390_ring_copy(struct dp8390_softc *sc, int src, void *dst, u_short amount)
1110 1.1 scottr {
1111 1.1 scottr bus_space_tag_t buft = sc->sc_buft;
1112 1.1 scottr bus_space_handle_t bufh = sc->sc_bufh;
1113 1.1 scottr u_short tmp_amount;
1114 1.1 scottr
1115 1.1 scottr /* Does copy wrap to lower addr in ring buffer? */
1116 1.1 scottr if (src + amount > sc->mem_end) {
1117 1.1 scottr tmp_amount = sc->mem_end - src;
1118 1.1 scottr
1119 1.1 scottr /* Copy amount up to end of NIC memory. */
1120 1.1 scottr bus_space_read_region_1(buft, bufh, src, dst, tmp_amount);
1121 1.1 scottr
1122 1.1 scottr amount -= tmp_amount;
1123 1.1 scottr src = sc->mem_ring;
1124 1.63 christos dst = (char *)dst + tmp_amount;
1125 1.1 scottr }
1126 1.1 scottr bus_space_read_region_1(buft, bufh, src, dst, amount);
1127 1.1 scottr
1128 1.77 tsutsui return src + amount;
1129 1.1 scottr }
1130 1.1 scottr
1131 1.1 scottr /*
1132 1.1 scottr * Copy a packet from an mbuf to the transmit buffer on the card.
1133 1.1 scottr *
1134 1.1 scottr * Currently uses an extra buffer/extra memory copy, unless the whole
1135 1.1 scottr * packet fits in one mbuf.
1136 1.1 scottr */
1137 1.76 tsutsui static int
1138 1.70 dsl dp8390_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
1139 1.1 scottr {
1140 1.1 scottr bus_space_tag_t buft = sc->sc_buft;
1141 1.1 scottr bus_space_handle_t bufh = sc->sc_bufh;
1142 1.77 tsutsui uint8_t *data;
1143 1.1 scottr int len, totlen = 0;
1144 1.1 scottr
1145 1.1 scottr for (; m ; m = m->m_next) {
1146 1.77 tsutsui data = mtod(m, uint8_t *);
1147 1.1 scottr len = m->m_len;
1148 1.1 scottr if (len > 0) {
1149 1.9 scottr bus_space_write_region_1(buft, bufh, buf, data, len);
1150 1.1 scottr totlen += len;
1151 1.9 scottr buf += len;
1152 1.1 scottr }
1153 1.1 scottr }
1154 1.52 bouyer if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1155 1.52 bouyer bus_space_set_region_1(buft, bufh, buf, 0,
1156 1.52 bouyer ETHER_MIN_LEN - ETHER_CRC_LEN - totlen);
1157 1.52 bouyer totlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
1158 1.52 bouyer }
1159 1.77 tsutsui return totlen;
1160 1.7 thorpej }
1161 1.7 thorpej
1162 1.7 thorpej /*
1163 1.7 thorpej * Enable power on the interface.
1164 1.7 thorpej */
1165 1.7 thorpej int
1166 1.70 dsl dp8390_enable(struct dp8390_softc *sc)
1167 1.7 thorpej {
1168 1.7 thorpej
1169 1.7 thorpej if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1170 1.7 thorpej if ((*sc->sc_enable)(sc) != 0) {
1171 1.68 cube aprint_error_dev(sc->sc_dev,
1172 1.68 cube "device enable failed\n");
1173 1.77 tsutsui return EIO;
1174 1.7 thorpej }
1175 1.7 thorpej }
1176 1.7 thorpej
1177 1.7 thorpej sc->sc_enabled = 1;
1178 1.77 tsutsui return 0;
1179 1.7 thorpej }
1180 1.7 thorpej
1181 1.7 thorpej /*
1182 1.7 thorpej * Disable power on the interface.
1183 1.7 thorpej */
1184 1.7 thorpej void
1185 1.70 dsl dp8390_disable(struct dp8390_softc *sc)
1186 1.7 thorpej {
1187 1.7 thorpej
1188 1.7 thorpej if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1189 1.7 thorpej (*sc->sc_disable)(sc);
1190 1.7 thorpej sc->sc_enabled = 0;
1191 1.7 thorpej }
1192 1.16 thorpej }
1193 1.16 thorpej
1194 1.16 thorpej int
1195 1.71 cegger dp8390_activate(device_t self, enum devact act)
1196 1.16 thorpej {
1197 1.72 dyoung struct dp8390_softc *sc = device_private(self);
1198 1.16 thorpej
1199 1.16 thorpej switch (act) {
1200 1.16 thorpej case DVACT_DEACTIVATE:
1201 1.28 itojun if_deactivate(&sc->sc_ec.ec_if);
1202 1.72 dyoung return 0;
1203 1.72 dyoung default:
1204 1.72 dyoung return EOPNOTSUPP;
1205 1.16 thorpej }
1206 1.28 itojun }
1207 1.28 itojun
1208 1.28 itojun int
1209 1.61 christos dp8390_detach(struct dp8390_softc *sc, int flags)
1210 1.28 itojun {
1211 1.28 itojun struct ifnet *ifp = &sc->sc_ec.ec_if;
1212 1.37 jhawk
1213 1.37 jhawk /* Succeed now if there's no work to do. */
1214 1.37 jhawk if ((sc->sc_flags & DP8390_ATTACHED) == 0)
1215 1.77 tsutsui return 0;
1216 1.28 itojun
1217 1.31 itojun /* dp8390_disable() checks sc->sc_enabled */
1218 1.28 itojun dp8390_disable(sc);
1219 1.29 enami
1220 1.42 thorpej if (sc->sc_media_fini != NULL)
1221 1.42 thorpej (*sc->sc_media_fini)(sc);
1222 1.42 thorpej
1223 1.42 thorpej /* Delete all remaining media. */
1224 1.32 enami ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
1225 1.28 itojun
1226 1.32 enami rnd_detach_source(&sc->rnd_source);
1227 1.32 enami ether_ifdetach(ifp);
1228 1.32 enami if_detach(ifp);
1229 1.28 itojun
1230 1.77 tsutsui return 0;
1231 1.1 scottr }
1232