smc83c170.c revision 1.93 1 1.93 thorpej /* $NetBSD: smc83c170.c,v 1.93 2020/02/07 00:56:48 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.10 thorpej * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej /*
34 1.1 thorpej * Device driver for the Standard Microsystems Corp. 83C170
35 1.1 thorpej * Ethernet PCI Integrated Controller (EPIC/100).
36 1.1 thorpej */
37 1.49 lukem
38 1.49 lukem #include <sys/cdefs.h>
39 1.93 thorpej __KERNEL_RCSID(0, "$NetBSD: smc83c170.c,v 1.93 2020/02/07 00:56:48 thorpej Exp $");
40 1.1 thorpej
41 1.1 thorpej
42 1.1 thorpej #include <sys/param.h>
43 1.59 perry #include <sys/systm.h>
44 1.29 thorpej #include <sys/callout.h>
45 1.59 perry #include <sys/mbuf.h>
46 1.1 thorpej #include <sys/malloc.h>
47 1.1 thorpej #include <sys/kernel.h>
48 1.1 thorpej #include <sys/socket.h>
49 1.1 thorpej #include <sys/ioctl.h>
50 1.1 thorpej #include <sys/errno.h>
51 1.1 thorpej #include <sys/device.h>
52 1.38 thorpej
53 1.1 thorpej #include <net/if.h>
54 1.1 thorpej #include <net/if_dl.h>
55 1.1 thorpej #include <net/if_media.h>
56 1.1 thorpej #include <net/if_ether.h>
57 1.1 thorpej
58 1.1 thorpej #include <net/bpf.h>
59 1.1 thorpej
60 1.67 ad #include <sys/bus.h>
61 1.67 ad #include <sys/intr.h>
62 1.1 thorpej
63 1.8 thorpej #include <dev/mii/miivar.h>
64 1.43 drochner #include <dev/mii/lxtphyreg.h>
65 1.8 thorpej
66 1.1 thorpej #include <dev/ic/smc83c170reg.h>
67 1.1 thorpej #include <dev/ic/smc83c170var.h>
68 1.1 thorpej
69 1.91 thorpej static void epic_start(struct ifnet *);
70 1.91 thorpej static void epic_watchdog(struct ifnet *);
71 1.91 thorpej static int epic_ioctl(struct ifnet *, u_long, void *);
72 1.91 thorpej static int epic_init(struct ifnet *);
73 1.91 thorpej static void epic_stop(struct ifnet *, int);
74 1.91 thorpej
75 1.91 thorpej static bool epic_shutdown(device_t, int);
76 1.91 thorpej
77 1.91 thorpej static void epic_reset(struct epic_softc *);
78 1.91 thorpej static void epic_rxdrain(struct epic_softc *);
79 1.91 thorpej static int epic_add_rxbuf(struct epic_softc *, int);
80 1.91 thorpej static void epic_read_eeprom(struct epic_softc *, int, int, uint16_t *);
81 1.91 thorpej static void epic_set_mchash(struct epic_softc *);
82 1.91 thorpej static void epic_fixup_clock_source(struct epic_softc *);
83 1.91 thorpej static int epic_mii_read(device_t, int, int, uint16_t *);
84 1.91 thorpej static int epic_mii_write(device_t, int, int, uint16_t);
85 1.91 thorpej static int epic_mii_wait(struct epic_softc *, uint32_t);
86 1.91 thorpej static void epic_tick(void *);
87 1.58 perry
88 1.91 thorpej static void epic_statchg(struct ifnet *);
89 1.91 thorpej static int epic_mediachange(struct ifnet *);
90 1.1 thorpej
91 1.1 thorpej #define INTMASK (INTSTAT_FATAL_INT | INTSTAT_TXU | \
92 1.21 thorpej INTSTAT_TXC | INTSTAT_RXE | INTSTAT_RQE | INTSTAT_RCC)
93 1.1 thorpej
94 1.19 thorpej int epic_copy_small = 0;
95 1.19 thorpej
96 1.52 bouyer #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
97 1.52 bouyer
98 1.1 thorpej /*
99 1.1 thorpej * Attach an EPIC interface to the system.
100 1.1 thorpej */
101 1.1 thorpej void
102 1.76 tsutsui epic_attach(struct epic_softc *sc)
103 1.1 thorpej {
104 1.1 thorpej bus_space_tag_t st = sc->sc_st;
105 1.1 thorpej bus_space_handle_t sh = sc->sc_sh;
106 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
107 1.88 msaitoh struct mii_data * const mii = &sc->sc_mii;
108 1.51 thorpej int rseg, error, miiflags;
109 1.51 thorpej u_int i;
110 1.1 thorpej bus_dma_segment_t seg;
111 1.63 tsutsui uint8_t enaddr[ETHER_ADDR_LEN], devname[12 + 1];
112 1.63 tsutsui uint16_t myea[ETHER_ADDR_LEN / 2], mydevname[6];
113 1.52 bouyer char *nullbuf;
114 1.1 thorpej
115 1.65 ad callout_init(&sc->sc_mii_callout, 0);
116 1.93 thorpej callout_setfunc(&sc->sc_mii_callout, epic_tick, sc);
117 1.29 thorpej
118 1.1 thorpej /*
119 1.1 thorpej * Allocate the control data structures, and create and load the
120 1.1 thorpej * DMA map for it.
121 1.1 thorpej */
122 1.1 thorpej if ((error = bus_dmamem_alloc(sc->sc_dmat,
123 1.52 bouyer sizeof(struct epic_control_data) + ETHER_PAD_LEN, PAGE_SIZE, 0,
124 1.52 bouyer &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
125 1.88 msaitoh aprint_error_dev(sc->sc_dev,
126 1.76 tsutsui "unable to allocate control data, error = %d\n", error);
127 1.14 thorpej goto fail_0;
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
131 1.52 bouyer sizeof(struct epic_control_data) + ETHER_PAD_LEN,
132 1.64 christos (void **)&sc->sc_control_data,
133 1.88 msaitoh BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
134 1.76 tsutsui aprint_error_dev(sc->sc_dev,
135 1.76 tsutsui "unable to map control data, error = %d\n", error);
136 1.14 thorpej goto fail_1;
137 1.1 thorpej }
138 1.52 bouyer nullbuf =
139 1.52 bouyer (char *)sc->sc_control_data + sizeof(struct epic_control_data);
140 1.52 bouyer memset(nullbuf, 0, ETHER_PAD_LEN);
141 1.1 thorpej
142 1.1 thorpej if ((error = bus_dmamap_create(sc->sc_dmat,
143 1.1 thorpej sizeof(struct epic_control_data), 1,
144 1.1 thorpej sizeof(struct epic_control_data), 0, BUS_DMA_NOWAIT,
145 1.1 thorpej &sc->sc_cddmamap)) != 0) {
146 1.76 tsutsui aprint_error_dev(sc->sc_dev,
147 1.76 tsutsui "unable to create control data DMA map, error = %d\n",
148 1.76 tsutsui error);
149 1.14 thorpej goto fail_2;
150 1.1 thorpej }
151 1.1 thorpej
152 1.1 thorpej if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
153 1.1 thorpej sc->sc_control_data, sizeof(struct epic_control_data), NULL,
154 1.1 thorpej BUS_DMA_NOWAIT)) != 0) {
155 1.88 msaitoh aprint_error_dev(sc->sc_dev,
156 1.73 cegger "unable to load control data DMA map, error = %d\n",
157 1.73 cegger error);
158 1.14 thorpej goto fail_3;
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej /*
162 1.1 thorpej * Create the transmit buffer DMA maps.
163 1.1 thorpej */
164 1.1 thorpej for (i = 0; i < EPIC_NTXDESC; i++) {
165 1.1 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
166 1.1 thorpej EPIC_NFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
167 1.10 thorpej &EPIC_DSTX(sc, i)->ds_dmamap)) != 0) {
168 1.76 tsutsui aprint_error_dev(sc->sc_dev,
169 1.76 tsutsui "unable to create tx DMA map %d, error = %d\n",
170 1.76 tsutsui i, error);
171 1.14 thorpej goto fail_4;
172 1.1 thorpej }
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej /*
176 1.42 tsutsui * Create the receive buffer DMA maps.
177 1.1 thorpej */
178 1.1 thorpej for (i = 0; i < EPIC_NRXDESC; i++) {
179 1.1 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
180 1.1 thorpej MCLBYTES, 0, BUS_DMA_NOWAIT,
181 1.10 thorpej &EPIC_DSRX(sc, i)->ds_dmamap)) != 0) {
182 1.76 tsutsui aprint_error_dev(sc->sc_dev,
183 1.76 tsutsui "unable to create rx DMA map %d, error = %d\n",
184 1.76 tsutsui i, error);
185 1.14 thorpej goto fail_5;
186 1.1 thorpej }
187 1.19 thorpej EPIC_DSRX(sc, i)->ds_mbuf = NULL;
188 1.1 thorpej }
189 1.1 thorpej
190 1.52 bouyer /*
191 1.52 bouyer * create and map the pad buffer
192 1.52 bouyer */
193 1.52 bouyer if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_PAD_LEN, 1,
194 1.52 bouyer ETHER_PAD_LEN, 0, BUS_DMA_NOWAIT,&sc->sc_nulldmamap)) != 0) {
195 1.76 tsutsui aprint_error_dev(sc->sc_dev,
196 1.76 tsutsui "unable to create pad buffer DMA map, error = %d\n", error);
197 1.52 bouyer goto fail_5;
198 1.52 bouyer }
199 1.52 bouyer
200 1.52 bouyer if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_nulldmamap,
201 1.52 bouyer nullbuf, ETHER_PAD_LEN, NULL, BUS_DMA_NOWAIT)) != 0) {
202 1.76 tsutsui aprint_error_dev(sc->sc_dev,
203 1.76 tsutsui "unable to load pad buffer DMA map, error = %d\n", error);
204 1.52 bouyer goto fail_6;
205 1.52 bouyer }
206 1.52 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_nulldmamap, 0, ETHER_PAD_LEN,
207 1.52 bouyer BUS_DMASYNC_PREWRITE);
208 1.1 thorpej
209 1.1 thorpej /*
210 1.1 thorpej * Bring the chip out of low-power mode and reset it to a known state.
211 1.1 thorpej */
212 1.1 thorpej bus_space_write_4(st, sh, EPIC_GENCTL, 0);
213 1.1 thorpej epic_reset(sc);
214 1.1 thorpej
215 1.1 thorpej /*
216 1.1 thorpej * Read the Ethernet address from the EEPROM.
217 1.1 thorpej */
218 1.62 tsutsui epic_read_eeprom(sc, 0, __arraycount(myea), myea);
219 1.62 tsutsui for (i = 0; i < __arraycount(myea); i++) {
220 1.89 msaitoh enaddr[i * 2] = myea[i] & 0xff;
221 1.32 tsutsui enaddr[i * 2 + 1] = myea[i] >> 8;
222 1.32 tsutsui }
223 1.1 thorpej
224 1.1 thorpej /*
225 1.1 thorpej * ...and the device name.
226 1.1 thorpej */
227 1.62 tsutsui epic_read_eeprom(sc, 0x2c, __arraycount(mydevname), mydevname);
228 1.62 tsutsui for (i = 0; i < __arraycount(mydevname); i++) {
229 1.89 msaitoh devname[i * 2] = mydevname[i] & 0xff;
230 1.32 tsutsui devname[i * 2 + 1] = mydevname[i] >> 8;
231 1.32 tsutsui }
232 1.32 tsutsui
233 1.1 thorpej devname[sizeof(mydevname)] = '\0';
234 1.61 tsutsui for (i = sizeof(mydevname) ; i > 0; i--) {
235 1.61 tsutsui if (devname[i - 1] == ' ')
236 1.61 tsutsui devname[i - 1] = '\0';
237 1.1 thorpej else
238 1.1 thorpej break;
239 1.1 thorpej }
240 1.1 thorpej
241 1.76 tsutsui aprint_normal_dev(sc->sc_dev, "%s, Ethernet address %s\n",
242 1.1 thorpej devname, ether_sprintf(enaddr));
243 1.1 thorpej
244 1.43 drochner miiflags = 0;
245 1.43 drochner if (sc->sc_hwflags & EPIC_HAS_MII_FIBER)
246 1.43 drochner miiflags |= MIIF_HAVEFIBER;
247 1.43 drochner
248 1.8 thorpej /*
249 1.8 thorpej * Initialize our media structures and probe the MII.
250 1.8 thorpej */
251 1.88 msaitoh mii->mii_ifp = ifp;
252 1.88 msaitoh mii->mii_readreg = epic_mii_read;
253 1.88 msaitoh mii->mii_writereg = epic_mii_write;
254 1.88 msaitoh mii->mii_statchg = epic_statchg;
255 1.71 dyoung
256 1.88 msaitoh sc->sc_ethercom.ec_mii = mii;
257 1.88 msaitoh ifmedia_init(&mii->mii_media, IFM_IMASK, epic_mediachange,
258 1.71 dyoung ether_mediastatus);
259 1.88 msaitoh mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
260 1.43 drochner MII_OFFSET_ANY, miiflags);
261 1.88 msaitoh if (LIST_EMPTY(&mii->mii_phys)) {
262 1.88 msaitoh ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
263 1.88 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
264 1.8 thorpej } else
265 1.88 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
266 1.8 thorpej
267 1.43 drochner if (sc->sc_hwflags & EPIC_HAS_BNC) {
268 1.43 drochner /* use the next free media instance */
269 1.88 msaitoh sc->sc_serinst = mii->mii_instance++;
270 1.88 msaitoh ifmedia_add(&mii->mii_media,
271 1.76 tsutsui IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_serinst),
272 1.76 tsutsui 0, NULL);
273 1.76 tsutsui aprint_normal_dev(sc->sc_dev, "10base2/BNC\n");
274 1.43 drochner } else
275 1.43 drochner sc->sc_serinst = -1;
276 1.43 drochner
277 1.76 tsutsui strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
278 1.1 thorpej ifp->if_softc = sc;
279 1.1 thorpej ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
280 1.1 thorpej ifp->if_ioctl = epic_ioctl;
281 1.1 thorpej ifp->if_start = epic_start;
282 1.1 thorpej ifp->if_watchdog = epic_watchdog;
283 1.34 thorpej ifp->if_init = epic_init;
284 1.34 thorpej ifp->if_stop = epic_stop;
285 1.40 thorpej IFQ_SET_READY(&ifp->if_snd);
286 1.36 bouyer
287 1.36 bouyer /*
288 1.36 bouyer * We can support 802.1Q VLAN-sized frames.
289 1.36 bouyer */
290 1.36 bouyer sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
291 1.1 thorpej
292 1.1 thorpej /*
293 1.1 thorpej * Attach the interface.
294 1.1 thorpej */
295 1.1 thorpej if_attach(ifp);
296 1.85 ozaki if_deferred_start_init(ifp, NULL);
297 1.1 thorpej ether_ifattach(ifp, enaddr);
298 1.1 thorpej
299 1.1 thorpej /*
300 1.1 thorpej * Make sure the interface is shutdown during reboot.
301 1.1 thorpej */
302 1.77 tsutsui if (pmf_device_register1(sc->sc_dev, NULL, NULL, epic_shutdown))
303 1.77 tsutsui pmf_class_network_register(sc->sc_dev, ifp);
304 1.77 tsutsui else
305 1.76 tsutsui aprint_error_dev(sc->sc_dev,
306 1.77 tsutsui "couldn't establish power handler\n");
307 1.77 tsutsui
308 1.1 thorpej return;
309 1.1 thorpej
310 1.1 thorpej /*
311 1.1 thorpej * Free any resources we've allocated during the failed attach
312 1.1 thorpej * attempt. Do this in reverse order and fall through.
313 1.1 thorpej */
314 1.52 bouyer fail_6:
315 1.52 bouyer bus_dmamap_destroy(sc->sc_dmat, sc->sc_nulldmamap);
316 1.14 thorpej fail_5:
317 1.14 thorpej for (i = 0; i < EPIC_NRXDESC; i++) {
318 1.14 thorpej if (EPIC_DSRX(sc, i)->ds_dmamap != NULL)
319 1.1 thorpej bus_dmamap_destroy(sc->sc_dmat,
320 1.10 thorpej EPIC_DSRX(sc, i)->ds_dmamap);
321 1.14 thorpej }
322 1.14 thorpej fail_4:
323 1.14 thorpej for (i = 0; i < EPIC_NTXDESC; i++) {
324 1.14 thorpej if (EPIC_DSTX(sc, i)->ds_dmamap != NULL)
325 1.1 thorpej bus_dmamap_destroy(sc->sc_dmat,
326 1.10 thorpej EPIC_DSTX(sc, i)->ds_dmamap);
327 1.1 thorpej }
328 1.14 thorpej bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
329 1.14 thorpej fail_3:
330 1.14 thorpej bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
331 1.14 thorpej fail_2:
332 1.64 christos bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
333 1.14 thorpej sizeof(struct epic_control_data));
334 1.14 thorpej fail_1:
335 1.14 thorpej bus_dmamem_free(sc->sc_dmat, &seg, rseg);
336 1.14 thorpej fail_0:
337 1.14 thorpej return;
338 1.1 thorpej }
339 1.1 thorpej
340 1.1 thorpej /*
341 1.1 thorpej * Shutdown hook. Make sure the interface is stopped at reboot.
342 1.1 thorpej */
343 1.91 thorpej static bool
344 1.77 tsutsui epic_shutdown(device_t self, int howto)
345 1.1 thorpej {
346 1.77 tsutsui struct epic_softc *sc = device_private(self);
347 1.1 thorpej
348 1.34 thorpej epic_stop(&sc->sc_ethercom.ec_if, 1);
349 1.77 tsutsui
350 1.77 tsutsui return true;
351 1.1 thorpej }
352 1.1 thorpej
353 1.1 thorpej /*
354 1.1 thorpej * Start packet transmission on the interface.
355 1.1 thorpej * [ifnet interface function]
356 1.1 thorpej */
357 1.91 thorpej static void
358 1.76 tsutsui epic_start(struct ifnet *ifp)
359 1.1 thorpej {
360 1.1 thorpej struct epic_softc *sc = ifp->if_softc;
361 1.10 thorpej struct mbuf *m0, *m;
362 1.1 thorpej struct epic_txdesc *txd;
363 1.1 thorpej struct epic_descsoft *ds;
364 1.1 thorpej struct epic_fraglist *fr;
365 1.1 thorpej bus_dmamap_t dmamap;
366 1.10 thorpej int error, firsttx, nexttx, opending, seg;
367 1.55 tsutsui u_int len;
368 1.1 thorpej
369 1.10 thorpej /*
370 1.10 thorpej * Remember the previous txpending and the first transmit
371 1.10 thorpej * descriptor we use.
372 1.10 thorpej */
373 1.10 thorpej opending = sc->sc_txpending;
374 1.10 thorpej firsttx = EPIC_NEXTTX(sc->sc_txlast);
375 1.1 thorpej
376 1.1 thorpej /*
377 1.1 thorpej * Loop through the send queue, setting up transmit descriptors
378 1.1 thorpej * until we drain the queue, or use up all available transmit
379 1.1 thorpej * descriptors.
380 1.1 thorpej */
381 1.10 thorpej while (sc->sc_txpending < EPIC_NTXDESC) {
382 1.1 thorpej /*
383 1.1 thorpej * Grab a packet off the queue.
384 1.1 thorpej */
385 1.40 thorpej IFQ_POLL(&ifp->if_snd, m0);
386 1.10 thorpej if (m0 == NULL)
387 1.10 thorpej break;
388 1.41 thorpej m = NULL;
389 1.1 thorpej
390 1.1 thorpej /*
391 1.1 thorpej * Get the last and next available transmit descriptor.
392 1.1 thorpej */
393 1.1 thorpej nexttx = EPIC_NEXTTX(sc->sc_txlast);
394 1.10 thorpej txd = EPIC_CDTX(sc, nexttx);
395 1.10 thorpej fr = EPIC_CDFL(sc, nexttx);
396 1.10 thorpej ds = EPIC_DSTX(sc, nexttx);
397 1.1 thorpej dmamap = ds->ds_dmamap;
398 1.1 thorpej
399 1.1 thorpej /*
400 1.10 thorpej * Load the DMA map. If this fails, the packet either
401 1.10 thorpej * didn't fit in the alloted number of frags, or we were
402 1.89 msaitoh * short on resources. In this case, we'll copy and try
403 1.10 thorpej * again.
404 1.1 thorpej */
405 1.52 bouyer if ((error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
406 1.88 msaitoh BUS_DMA_WRITE | BUS_DMA_NOWAIT)) != 0 ||
407 1.52 bouyer (m0->m_pkthdr.len < ETHER_PAD_LEN &&
408 1.52 bouyer dmamap-> dm_nsegs == EPIC_NFRAGS)) {
409 1.52 bouyer if (error == 0)
410 1.52 bouyer bus_dmamap_unload(sc->sc_dmat, dmamap);
411 1.59 perry
412 1.10 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
413 1.10 thorpej if (m == NULL) {
414 1.76 tsutsui printf("%s: unable to allocate Tx mbuf\n",
415 1.76 tsutsui device_xname(sc->sc_dev));
416 1.10 thorpej break;
417 1.1 thorpej }
418 1.1 thorpej if (m0->m_pkthdr.len > MHLEN) {
419 1.10 thorpej MCLGET(m, M_DONTWAIT);
420 1.10 thorpej if ((m->m_flags & M_EXT) == 0) {
421 1.76 tsutsui printf("%s: unable to allocate Tx "
422 1.76 tsutsui "cluster\n",
423 1.76 tsutsui device_xname(sc->sc_dev));
424 1.10 thorpej m_freem(m);
425 1.10 thorpej break;
426 1.1 thorpej }
427 1.1 thorpej }
428 1.64 christos m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
429 1.10 thorpej m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
430 1.10 thorpej error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
431 1.88 msaitoh m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
432 1.10 thorpej if (error) {
433 1.76 tsutsui printf("%s: unable to load Tx buffer, "
434 1.76 tsutsui "error = %d\n", device_xname(sc->sc_dev),
435 1.76 tsutsui error);
436 1.10 thorpej break;
437 1.10 thorpej }
438 1.1 thorpej }
439 1.40 thorpej IFQ_DEQUEUE(&ifp->if_snd, m0);
440 1.41 thorpej if (m != NULL) {
441 1.41 thorpej m_freem(m0);
442 1.41 thorpej m0 = m;
443 1.41 thorpej }
444 1.1 thorpej
445 1.10 thorpej /* Initialize the fraglist. */
446 1.1 thorpej for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
447 1.1 thorpej fr->ef_frags[seg].ef_addr =
448 1.1 thorpej dmamap->dm_segs[seg].ds_addr;
449 1.1 thorpej fr->ef_frags[seg].ef_length =
450 1.1 thorpej dmamap->dm_segs[seg].ds_len;
451 1.1 thorpej }
452 1.55 tsutsui len = m0->m_pkthdr.len;
453 1.55 tsutsui if (len < ETHER_PAD_LEN) {
454 1.52 bouyer fr->ef_frags[seg].ef_addr = sc->sc_nulldma;
455 1.55 tsutsui fr->ef_frags[seg].ef_length = ETHER_PAD_LEN - len;
456 1.55 tsutsui len = ETHER_PAD_LEN;
457 1.52 bouyer seg++;
458 1.52 bouyer }
459 1.52 bouyer fr->ef_nfrags = seg;
460 1.1 thorpej
461 1.10 thorpej EPIC_CDFLSYNC(sc, nexttx, BUS_DMASYNC_PREWRITE);
462 1.10 thorpej
463 1.10 thorpej /* Sync the DMA map. */
464 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
465 1.1 thorpej BUS_DMASYNC_PREWRITE);
466 1.1 thorpej
467 1.1 thorpej /*
468 1.1 thorpej * Store a pointer to the packet so we can free it later.
469 1.1 thorpej */
470 1.1 thorpej ds->ds_mbuf = m0;
471 1.1 thorpej
472 1.1 thorpej /*
473 1.52 bouyer * Fill in the transmit descriptor.
474 1.1 thorpej */
475 1.10 thorpej txd->et_control = ET_TXCTL_LASTDESC | ET_TXCTL_FRAGLIST;
476 1.1 thorpej
477 1.1 thorpej /*
478 1.10 thorpej * If this is the first descriptor we're enqueueing,
479 1.10 thorpej * don't give it to the EPIC yet. That could cause
480 1.10 thorpej * a race condition. We'll do it below.
481 1.1 thorpej */
482 1.10 thorpej if (nexttx == firsttx)
483 1.55 tsutsui txd->et_txstatus = TXSTAT_TXLENGTH(len);
484 1.10 thorpej else
485 1.55 tsutsui txd->et_txstatus =
486 1.55 tsutsui TXSTAT_TXLENGTH(len) | ET_TXSTAT_OWNER;
487 1.10 thorpej
488 1.10 thorpej EPIC_CDTXSYNC(sc, nexttx,
489 1.88 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
490 1.1 thorpej
491 1.10 thorpej /* Advance the tx pointer. */
492 1.1 thorpej sc->sc_txpending++;
493 1.10 thorpej sc->sc_txlast = nexttx;
494 1.1 thorpej
495 1.1 thorpej /*
496 1.1 thorpej * Pass the packet to any BPF listeners.
497 1.1 thorpej */
498 1.86 msaitoh bpf_mtap(ifp, m0, BPF_D_OUT);
499 1.1 thorpej }
500 1.1 thorpej
501 1.10 thorpej if (sc->sc_txpending == EPIC_NTXDESC) {
502 1.10 thorpej /* No more slots left; notify upper layer. */
503 1.10 thorpej ifp->if_flags |= IFF_OACTIVE;
504 1.10 thorpej }
505 1.10 thorpej
506 1.10 thorpej if (sc->sc_txpending != opending) {
507 1.10 thorpej /*
508 1.89 msaitoh * We enqueued packets. If the transmitter was idle,
509 1.10 thorpej * reset the txdirty pointer.
510 1.10 thorpej */
511 1.10 thorpej if (opending == 0)
512 1.10 thorpej sc->sc_txdirty = firsttx;
513 1.10 thorpej
514 1.10 thorpej /*
515 1.10 thorpej * Cause a transmit interrupt to happen on the
516 1.10 thorpej * last packet we enqueued.
517 1.10 thorpej */
518 1.10 thorpej EPIC_CDTX(sc, sc->sc_txlast)->et_control |= ET_TXCTL_IAF;
519 1.10 thorpej EPIC_CDTXSYNC(sc, sc->sc_txlast,
520 1.88 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
521 1.10 thorpej
522 1.10 thorpej /*
523 1.10 thorpej * The entire packet chain is set up. Give the
524 1.10 thorpej * first descriptor to the EPIC now.
525 1.10 thorpej */
526 1.55 tsutsui EPIC_CDTX(sc, firsttx)->et_txstatus |= ET_TXSTAT_OWNER;
527 1.10 thorpej EPIC_CDTXSYNC(sc, firsttx,
528 1.88 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
529 1.10 thorpej
530 1.10 thorpej /* Start the transmitter. */
531 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
532 1.1 thorpej COMMAND_TXQUEUED);
533 1.1 thorpej
534 1.10 thorpej /* Set a watchdog timer in case the chip flakes out. */
535 1.1 thorpej ifp->if_timer = 5;
536 1.1 thorpej }
537 1.1 thorpej }
538 1.1 thorpej
539 1.1 thorpej /*
540 1.1 thorpej * Watchdog timer handler.
541 1.1 thorpej * [ifnet interface function]
542 1.1 thorpej */
543 1.91 thorpej static void
544 1.76 tsutsui epic_watchdog(struct ifnet *ifp)
545 1.1 thorpej {
546 1.1 thorpej struct epic_softc *sc = ifp->if_softc;
547 1.1 thorpej
548 1.76 tsutsui printf("%s: device timeout\n", device_xname(sc->sc_dev));
549 1.92 thorpej if_statinc(ifp, if_oerrors);
550 1.1 thorpej
551 1.76 tsutsui (void)epic_init(ifp);
552 1.1 thorpej }
553 1.1 thorpej
554 1.1 thorpej /*
555 1.1 thorpej * Handle control requests from the operator.
556 1.1 thorpej * [ifnet interface function]
557 1.1 thorpej */
558 1.91 thorpej static int
559 1.76 tsutsui epic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
560 1.1 thorpej {
561 1.1 thorpej struct epic_softc *sc = ifp->if_softc;
562 1.34 thorpej int s, error;
563 1.1 thorpej
564 1.7 mycroft s = splnet();
565 1.1 thorpej
566 1.71 dyoung error = ether_ioctl(ifp, cmd, data);
567 1.71 dyoung if (error == ENETRESET) {
568 1.71 dyoung /*
569 1.71 dyoung * Multicast list has changed; set the hardware filter
570 1.89 msaitoh * accordingly. Update our idea of the current media;
571 1.71 dyoung * epic_set_mchash() needs to know what it is.
572 1.71 dyoung */
573 1.71 dyoung if (ifp->if_flags & IFF_RUNNING) {
574 1.71 dyoung mii_pollstat(&sc->sc_mii);
575 1.71 dyoung epic_set_mchash(sc);
576 1.1 thorpej }
577 1.71 dyoung error = 0;
578 1.1 thorpej }
579 1.1 thorpej
580 1.1 thorpej splx(s);
581 1.76 tsutsui return error;
582 1.1 thorpej }
583 1.1 thorpej
584 1.1 thorpej /*
585 1.1 thorpej * Interrupt handler.
586 1.1 thorpej */
587 1.1 thorpej int
588 1.76 tsutsui epic_intr(void *arg)
589 1.1 thorpej {
590 1.1 thorpej struct epic_softc *sc = arg;
591 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
592 1.1 thorpej struct epic_rxdesc *rxd;
593 1.1 thorpej struct epic_txdesc *txd;
594 1.1 thorpej struct epic_descsoft *ds;
595 1.1 thorpej struct mbuf *m;
596 1.63 tsutsui uint32_t intstat, rxstatus, txstatus;
597 1.51 thorpej int i, claimed = 0;
598 1.51 thorpej u_int len;
599 1.1 thorpej
600 1.1 thorpej top:
601 1.1 thorpej /*
602 1.1 thorpej * Get the interrupt status from the EPIC.
603 1.1 thorpej */
604 1.1 thorpej intstat = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_INTSTAT);
605 1.1 thorpej if ((intstat & INTSTAT_INT_ACTV) == 0)
606 1.76 tsutsui return claimed;
607 1.1 thorpej
608 1.1 thorpej claimed = 1;
609 1.1 thorpej
610 1.1 thorpej /*
611 1.1 thorpej * Acknowledge the interrupt.
612 1.1 thorpej */
613 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_INTSTAT,
614 1.1 thorpej intstat & INTMASK);
615 1.1 thorpej
616 1.1 thorpej /*
617 1.1 thorpej * Check for receive interrupts.
618 1.1 thorpej */
619 1.21 thorpej if (intstat & (INTSTAT_RCC | INTSTAT_RXE | INTSTAT_RQE)) {
620 1.1 thorpej for (i = sc->sc_rxptr;; i = EPIC_NEXTRX(i)) {
621 1.10 thorpej rxd = EPIC_CDRX(sc, i);
622 1.10 thorpej ds = EPIC_DSRX(sc, i);
623 1.10 thorpej
624 1.10 thorpej EPIC_CDRXSYNC(sc, i,
625 1.88 msaitoh BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
626 1.1 thorpej
627 1.55 tsutsui rxstatus = rxd->er_rxstatus;
628 1.55 tsutsui if (rxstatus & ER_RXSTAT_OWNER) {
629 1.1 thorpej /*
630 1.1 thorpej * We have processed all of the
631 1.1 thorpej * receive buffers.
632 1.1 thorpej */
633 1.1 thorpej break;
634 1.1 thorpej }
635 1.1 thorpej
636 1.1 thorpej /*
637 1.89 msaitoh * Make sure the packet arrived intact. If an error
638 1.10 thorpej * occurred, update stats and reset the descriptor.
639 1.10 thorpej * The buffer will be reused the next time the
640 1.10 thorpej * descriptor comes up in the ring.
641 1.1 thorpej */
642 1.55 tsutsui if ((rxstatus & ER_RXSTAT_PKTINTACT) == 0) {
643 1.55 tsutsui if (rxstatus & ER_RXSTAT_CRCERROR)
644 1.76 tsutsui printf("%s: CRC error\n",
645 1.76 tsutsui device_xname(sc->sc_dev));
646 1.55 tsutsui if (rxstatus & ER_RXSTAT_ALIGNERROR)
647 1.76 tsutsui printf("%s: alignment error\n",
648 1.76 tsutsui device_xname(sc->sc_dev));
649 1.92 thorpej if_statinc(ifp, if_ierrors);
650 1.10 thorpej EPIC_INIT_RXDESC(sc, i);
651 1.10 thorpej continue;
652 1.1 thorpej }
653 1.1 thorpej
654 1.10 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
655 1.10 thorpej ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
656 1.10 thorpej
657 1.21 thorpej /*
658 1.57 thorpej * The EPIC includes the CRC with every packet;
659 1.57 thorpej * trim it.
660 1.21 thorpej */
661 1.57 thorpej len = RXSTAT_RXLENGTH(rxstatus) - ETHER_CRC_LEN;
662 1.21 thorpej
663 1.19 thorpej if (len < sizeof(struct ether_header)) {
664 1.19 thorpej /*
665 1.19 thorpej * Runt packet; drop it now.
666 1.19 thorpej */
667 1.92 thorpej if_statinc(ifp, if_ierrors);
668 1.10 thorpej EPIC_INIT_RXDESC(sc, i);
669 1.10 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
670 1.10 thorpej ds->ds_dmamap->dm_mapsize,
671 1.10 thorpej BUS_DMASYNC_PREREAD);
672 1.10 thorpej continue;
673 1.10 thorpej }
674 1.10 thorpej
675 1.19 thorpej /*
676 1.19 thorpej * If the packet is small enough to fit in a
677 1.19 thorpej * single header mbuf, allocate one and copy
678 1.19 thorpej * the data into it. This greatly reduces
679 1.19 thorpej * memory consumption when we receive lots
680 1.19 thorpej * of small packets.
681 1.19 thorpej *
682 1.19 thorpej * Otherwise, we add a new buffer to the receive
683 1.19 thorpej * chain. If this fails, we drop the packet and
684 1.19 thorpej * recycle the old buffer.
685 1.19 thorpej */
686 1.19 thorpej if (epic_copy_small != 0 && len <= MHLEN) {
687 1.19 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
688 1.19 thorpej if (m == NULL)
689 1.19 thorpej goto dropit;
690 1.64 christos memcpy(mtod(m, void *),
691 1.64 christos mtod(ds->ds_mbuf, void *), len);
692 1.19 thorpej EPIC_INIT_RXDESC(sc, i);
693 1.19 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
694 1.19 thorpej ds->ds_dmamap->dm_mapsize,
695 1.19 thorpej BUS_DMASYNC_PREREAD);
696 1.19 thorpej } else {
697 1.19 thorpej m = ds->ds_mbuf;
698 1.19 thorpej if (epic_add_rxbuf(sc, i) != 0) {
699 1.19 thorpej dropit:
700 1.92 thorpej if_statinc(ifp, if_ierrors);
701 1.19 thorpej EPIC_INIT_RXDESC(sc, i);
702 1.19 thorpej bus_dmamap_sync(sc->sc_dmat,
703 1.19 thorpej ds->ds_dmamap, 0,
704 1.19 thorpej ds->ds_dmamap->dm_mapsize,
705 1.19 thorpej BUS_DMASYNC_PREREAD);
706 1.19 thorpej continue;
707 1.19 thorpej }
708 1.10 thorpej }
709 1.10 thorpej
710 1.83 ozaki m_set_rcvif(m, ifp);
711 1.10 thorpej m->m_pkthdr.len = m->m_len = len;
712 1.1 thorpej
713 1.16 thorpej /* Pass it on. */
714 1.82 ozaki if_percpuq_enqueue(ifp->if_percpuq, m);
715 1.1 thorpej }
716 1.10 thorpej
717 1.42 tsutsui /* Update the receive pointer. */
718 1.1 thorpej sc->sc_rxptr = i;
719 1.1 thorpej
720 1.1 thorpej /*
721 1.1 thorpej * Check for receive queue underflow.
722 1.1 thorpej */
723 1.1 thorpej if (intstat & INTSTAT_RQE) {
724 1.76 tsutsui printf("%s: receiver queue empty\n",
725 1.76 tsutsui device_xname(sc->sc_dev));
726 1.1 thorpej /*
727 1.1 thorpej * Ring is already built; just restart the
728 1.1 thorpej * receiver.
729 1.1 thorpej */
730 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_PRCDAR,
731 1.10 thorpej EPIC_CDRXADDR(sc, sc->sc_rxptr));
732 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
733 1.1 thorpej COMMAND_RXQUEUED | COMMAND_START_RX);
734 1.1 thorpej }
735 1.1 thorpej }
736 1.1 thorpej
737 1.1 thorpej /*
738 1.1 thorpej * Check for transmission complete interrupts.
739 1.1 thorpej */
740 1.1 thorpej if (intstat & (INTSTAT_TXC | INTSTAT_TXU)) {
741 1.10 thorpej ifp->if_flags &= ~IFF_OACTIVE;
742 1.10 thorpej for (i = sc->sc_txdirty; sc->sc_txpending != 0;
743 1.10 thorpej i = EPIC_NEXTTX(i), sc->sc_txpending--) {
744 1.10 thorpej txd = EPIC_CDTX(sc, i);
745 1.10 thorpej ds = EPIC_DSTX(sc, i);
746 1.1 thorpej
747 1.10 thorpej EPIC_CDTXSYNC(sc, i,
748 1.88 msaitoh BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
749 1.10 thorpej
750 1.55 tsutsui txstatus = txd->et_txstatus;
751 1.55 tsutsui if (txstatus & ET_TXSTAT_OWNER)
752 1.1 thorpej break;
753 1.1 thorpej
754 1.10 thorpej EPIC_CDFLSYNC(sc, i, BUS_DMASYNC_POSTWRITE);
755 1.10 thorpej
756 1.10 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
757 1.10 thorpej 0, ds->ds_dmamap->dm_mapsize,
758 1.10 thorpej BUS_DMASYNC_POSTWRITE);
759 1.10 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
760 1.10 thorpej m_freem(ds->ds_mbuf);
761 1.10 thorpej ds->ds_mbuf = NULL;
762 1.1 thorpej
763 1.1 thorpej /*
764 1.1 thorpej * Check for errors and collisions.
765 1.1 thorpej */
766 1.92 thorpej net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
767 1.55 tsutsui if ((txstatus & ET_TXSTAT_PACKETTX) == 0)
768 1.92 thorpej if_statinc_ref(nsr, if_oerrors);
769 1.10 thorpej else
770 1.92 thorpej if_statinc_ref(nsr, if_opackets);
771 1.92 thorpej if (TXSTAT_COLLISIONS(txstatus))
772 1.92 thorpej if_statadd_ref(nsr, if_collisions,
773 1.92 thorpej TXSTAT_COLLISIONS(txstatus));
774 1.55 tsutsui if (txstatus & ET_TXSTAT_CARSENSELOST)
775 1.76 tsutsui printf("%s: lost carrier\n",
776 1.76 tsutsui device_xname(sc->sc_dev));
777 1.92 thorpej IF_STAT_PUTREF(ifp);
778 1.1 thorpej }
779 1.59 perry
780 1.10 thorpej /* Update the dirty transmit buffer pointer. */
781 1.1 thorpej sc->sc_txdirty = i;
782 1.1 thorpej
783 1.1 thorpej /*
784 1.1 thorpej * Cancel the watchdog timer if there are no pending
785 1.1 thorpej * transmissions.
786 1.1 thorpej */
787 1.1 thorpej if (sc->sc_txpending == 0)
788 1.1 thorpej ifp->if_timer = 0;
789 1.1 thorpej
790 1.1 thorpej /*
791 1.1 thorpej * Kick the transmitter after a DMA underrun.
792 1.1 thorpej */
793 1.1 thorpej if (intstat & INTSTAT_TXU) {
794 1.76 tsutsui printf("%s: transmit underrun\n",
795 1.76 tsutsui device_xname(sc->sc_dev));
796 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh,
797 1.1 thorpej EPIC_COMMAND, COMMAND_TXUGO);
798 1.1 thorpej if (sc->sc_txpending)
799 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh,
800 1.1 thorpej EPIC_COMMAND, COMMAND_TXQUEUED);
801 1.1 thorpej }
802 1.1 thorpej
803 1.1 thorpej /*
804 1.1 thorpej * Try to get more packets going.
805 1.1 thorpej */
806 1.85 ozaki if_schedule_deferred_start(ifp);
807 1.1 thorpej }
808 1.1 thorpej
809 1.1 thorpej /*
810 1.1 thorpej * Check for fatal interrupts.
811 1.1 thorpej */
812 1.1 thorpej if (intstat & INTSTAT_FATAL_INT) {
813 1.21 thorpej if (intstat & INTSTAT_PTA)
814 1.76 tsutsui printf("%s: PCI target abort error\n",
815 1.76 tsutsui device_xname(sc->sc_dev));
816 1.21 thorpej else if (intstat & INTSTAT_PMA)
817 1.76 tsutsui printf("%s: PCI master abort error\n",
818 1.76 tsutsui device_xname(sc->sc_dev));
819 1.21 thorpej else if (intstat & INTSTAT_APE)
820 1.76 tsutsui printf("%s: PCI address parity error\n",
821 1.76 tsutsui device_xname(sc->sc_dev));
822 1.21 thorpej else if (intstat & INTSTAT_DPE)
823 1.76 tsutsui printf("%s: PCI data parity error\n",
824 1.76 tsutsui device_xname(sc->sc_dev));
825 1.21 thorpej else
826 1.76 tsutsui printf("%s: unknown fatal error\n",
827 1.76 tsutsui device_xname(sc->sc_dev));
828 1.76 tsutsui (void)epic_init(ifp);
829 1.1 thorpej }
830 1.1 thorpej
831 1.1 thorpej /*
832 1.1 thorpej * Check for more interrupts.
833 1.1 thorpej */
834 1.1 thorpej goto top;
835 1.1 thorpej }
836 1.1 thorpej
837 1.1 thorpej /*
838 1.8 thorpej * One second timer, used to tick the MII.
839 1.8 thorpej */
840 1.91 thorpej static void
841 1.76 tsutsui epic_tick(void *arg)
842 1.8 thorpej {
843 1.8 thorpej struct epic_softc *sc = arg;
844 1.8 thorpej int s;
845 1.8 thorpej
846 1.12 thorpej s = splnet();
847 1.8 thorpej mii_tick(&sc->sc_mii);
848 1.8 thorpej splx(s);
849 1.8 thorpej
850 1.93 thorpej callout_schedule(&sc->sc_mii_callout, hz);
851 1.8 thorpej }
852 1.8 thorpej
853 1.8 thorpej /*
854 1.6 thorpej * Fixup the clock source on the EPIC.
855 1.6 thorpej */
856 1.91 thorpej static void
857 1.76 tsutsui epic_fixup_clock_source(struct epic_softc *sc)
858 1.6 thorpej {
859 1.6 thorpej int i;
860 1.6 thorpej
861 1.6 thorpej /*
862 1.6 thorpej * According to SMC Application Note 7-15, the EPIC's clock
863 1.6 thorpej * source is incorrect following a reset. This manifests itself
864 1.6 thorpej * as failure to recognize when host software has written to
865 1.6 thorpej * a register on the EPIC. The appnote recommends issuing at
866 1.6 thorpej * least 16 consecutive writes to the CLOCK TEST bit to correctly
867 1.6 thorpej * configure the clock source.
868 1.6 thorpej */
869 1.6 thorpej for (i = 0; i < 16; i++)
870 1.6 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TEST,
871 1.6 thorpej TEST_CLOCKTEST);
872 1.6 thorpej }
873 1.6 thorpej
874 1.6 thorpej /*
875 1.1 thorpej * Perform a soft reset on the EPIC.
876 1.1 thorpej */
877 1.91 thorpej static void
878 1.76 tsutsui epic_reset(struct epic_softc *sc)
879 1.1 thorpej {
880 1.1 thorpej
881 1.6 thorpej epic_fixup_clock_source(sc);
882 1.6 thorpej
883 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, 0);
884 1.1 thorpej delay(100);
885 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, GENCTL_SOFTRESET);
886 1.1 thorpej delay(100);
887 1.6 thorpej
888 1.6 thorpej epic_fixup_clock_source(sc);
889 1.1 thorpej }
890 1.1 thorpej
891 1.1 thorpej /*
892 1.7 mycroft * Initialize the interface. Must be called at splnet().
893 1.1 thorpej */
894 1.91 thorpej static int
895 1.76 tsutsui epic_init(struct ifnet *ifp)
896 1.1 thorpej {
897 1.34 thorpej struct epic_softc *sc = ifp->if_softc;
898 1.1 thorpej bus_space_tag_t st = sc->sc_st;
899 1.1 thorpej bus_space_handle_t sh = sc->sc_sh;
900 1.66 dyoung const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
901 1.1 thorpej struct epic_txdesc *txd;
902 1.19 thorpej struct epic_descsoft *ds;
903 1.63 tsutsui uint32_t genctl, reg0;
904 1.19 thorpej int i, error = 0;
905 1.1 thorpej
906 1.1 thorpej /*
907 1.1 thorpej * Cancel any pending I/O.
908 1.1 thorpej */
909 1.34 thorpej epic_stop(ifp, 0);
910 1.1 thorpej
911 1.1 thorpej /*
912 1.1 thorpej * Reset the EPIC to a known state.
913 1.1 thorpej */
914 1.1 thorpej epic_reset(sc);
915 1.1 thorpej
916 1.1 thorpej /*
917 1.1 thorpej * Magical mystery initialization.
918 1.1 thorpej */
919 1.1 thorpej bus_space_write_4(st, sh, EPIC_TXTEST, 0);
920 1.1 thorpej
921 1.1 thorpej /*
922 1.1 thorpej * Initialize the EPIC genctl register:
923 1.1 thorpej *
924 1.1 thorpej * - 64 byte receive FIFO threshold
925 1.1 thorpej * - automatic advance to next receive frame
926 1.1 thorpej */
927 1.1 thorpej genctl = GENCTL_RX_FIFO_THRESH0 | GENCTL_ONECOPY;
928 1.18 thorpej #if BYTE_ORDER == BIG_ENDIAN
929 1.18 thorpej genctl |= GENCTL_BIG_ENDIAN;
930 1.18 thorpej #endif
931 1.1 thorpej bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
932 1.1 thorpej
933 1.1 thorpej /*
934 1.1 thorpej * Reset the MII bus and PHY.
935 1.1 thorpej */
936 1.1 thorpej reg0 = bus_space_read_4(st, sh, EPIC_NVCTL);
937 1.1 thorpej bus_space_write_4(st, sh, EPIC_NVCTL, reg0 | NVCTL_GPIO1 | NVCTL_GPOE1);
938 1.1 thorpej bus_space_write_4(st, sh, EPIC_MIICFG, MIICFG_ENASER);
939 1.1 thorpej bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_RESET_PHY);
940 1.1 thorpej delay(100);
941 1.1 thorpej bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
942 1.44 drochner delay(1000);
943 1.1 thorpej bus_space_write_4(st, sh, EPIC_NVCTL, reg0);
944 1.1 thorpej
945 1.1 thorpej /*
946 1.1 thorpej * Initialize Ethernet address.
947 1.1 thorpej */
948 1.1 thorpej reg0 = enaddr[1] << 8 | enaddr[0];
949 1.1 thorpej bus_space_write_4(st, sh, EPIC_LAN0, reg0);
950 1.1 thorpej reg0 = enaddr[3] << 8 | enaddr[2];
951 1.1 thorpej bus_space_write_4(st, sh, EPIC_LAN1, reg0);
952 1.1 thorpej reg0 = enaddr[5] << 8 | enaddr[4];
953 1.1 thorpej bus_space_write_4(st, sh, EPIC_LAN2, reg0);
954 1.1 thorpej
955 1.1 thorpej /*
956 1.89 msaitoh * Initialize receive control. Remember the external buffer
957 1.1 thorpej * size setting.
958 1.1 thorpej */
959 1.1 thorpej reg0 = bus_space_read_4(st, sh, EPIC_RXCON) &
960 1.1 thorpej (RXCON_EXTBUFSIZESEL1 | RXCON_EXTBUFSIZESEL0);
961 1.1 thorpej reg0 |= (RXCON_RXMULTICAST | RXCON_RXBROADCAST);
962 1.1 thorpej if (ifp->if_flags & IFF_PROMISC)
963 1.1 thorpej reg0 |= RXCON_PROMISCMODE;
964 1.1 thorpej bus_space_write_4(st, sh, EPIC_RXCON, reg0);
965 1.1 thorpej
966 1.13 thorpej /* Set the current media. */
967 1.71 dyoung if ((error = epic_mediachange(ifp)) != 0)
968 1.71 dyoung goto out;
969 1.1 thorpej
970 1.13 thorpej /* Set up the multicast hash table. */
971 1.13 thorpej epic_set_mchash(sc);
972 1.13 thorpej
973 1.1 thorpej /*
974 1.10 thorpej * Initialize the transmit descriptor ring. txlast is initialized
975 1.10 thorpej * to the end of the list so that it will wrap around to the first
976 1.10 thorpej * descriptor when the first packet is transmitted.
977 1.1 thorpej */
978 1.1 thorpej for (i = 0; i < EPIC_NTXDESC; i++) {
979 1.10 thorpej txd = EPIC_CDTX(sc, i);
980 1.10 thorpej memset(txd, 0, sizeof(struct epic_txdesc));
981 1.10 thorpej txd->et_bufaddr = EPIC_CDFLADDR(sc, i);
982 1.10 thorpej txd->et_nextdesc = EPIC_CDTXADDR(sc, EPIC_NEXTTX(i));
983 1.88 msaitoh EPIC_CDTXSYNC(sc, i,
984 1.88 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
985 1.1 thorpej }
986 1.10 thorpej sc->sc_txpending = 0;
987 1.10 thorpej sc->sc_txdirty = 0;
988 1.10 thorpej sc->sc_txlast = EPIC_NTXDESC - 1;
989 1.1 thorpej
990 1.1 thorpej /*
991 1.19 thorpej * Initialize the receive descriptor ring.
992 1.1 thorpej */
993 1.19 thorpej for (i = 0; i < EPIC_NRXDESC; i++) {
994 1.19 thorpej ds = EPIC_DSRX(sc, i);
995 1.19 thorpej if (ds->ds_mbuf == NULL) {
996 1.19 thorpej if ((error = epic_add_rxbuf(sc, i)) != 0) {
997 1.76 tsutsui printf("%s: unable to allocate or map rx "
998 1.19 thorpej "buffer %d error = %d\n",
999 1.76 tsutsui device_xname(sc->sc_dev), i, error);
1000 1.19 thorpej /*
1001 1.19 thorpej * XXX Should attempt to run with fewer receive
1002 1.19 thorpej * XXX buffers instead of just failing.
1003 1.19 thorpej */
1004 1.19 thorpej epic_rxdrain(sc);
1005 1.19 thorpej goto out;
1006 1.19 thorpej }
1007 1.48 thorpej } else
1008 1.48 thorpej EPIC_INIT_RXDESC(sc, i);
1009 1.19 thorpej }
1010 1.10 thorpej sc->sc_rxptr = 0;
1011 1.1 thorpej
1012 1.1 thorpej /*
1013 1.1 thorpej * Initialize the interrupt mask and enable interrupts.
1014 1.1 thorpej */
1015 1.1 thorpej bus_space_write_4(st, sh, EPIC_INTMASK, INTMASK);
1016 1.1 thorpej bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_INTENA);
1017 1.1 thorpej
1018 1.1 thorpej /*
1019 1.1 thorpej * Give the transmit and receive rings to the EPIC.
1020 1.1 thorpej */
1021 1.1 thorpej bus_space_write_4(st, sh, EPIC_PTCDAR,
1022 1.10 thorpej EPIC_CDTXADDR(sc, EPIC_NEXTTX(sc->sc_txlast)));
1023 1.1 thorpej bus_space_write_4(st, sh, EPIC_PRCDAR,
1024 1.10 thorpej EPIC_CDRXADDR(sc, sc->sc_rxptr));
1025 1.1 thorpej
1026 1.1 thorpej /*
1027 1.1 thorpej * Set the EPIC in motion.
1028 1.1 thorpej */
1029 1.1 thorpej bus_space_write_4(st, sh, EPIC_COMMAND,
1030 1.1 thorpej COMMAND_RXQUEUED | COMMAND_START_RX);
1031 1.1 thorpej
1032 1.1 thorpej /*
1033 1.1 thorpej * ...all done!
1034 1.1 thorpej */
1035 1.1 thorpej ifp->if_flags |= IFF_RUNNING;
1036 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
1037 1.8 thorpej
1038 1.8 thorpej /*
1039 1.8 thorpej * Start the one second clock.
1040 1.8 thorpej */
1041 1.93 thorpej callout_schedule(&sc->sc_mii_callout, hz);
1042 1.9 thorpej
1043 1.9 thorpej /*
1044 1.9 thorpej * Attempt to start output on the interface.
1045 1.9 thorpej */
1046 1.9 thorpej epic_start(ifp);
1047 1.19 thorpej
1048 1.19 thorpej out:
1049 1.19 thorpej if (error)
1050 1.76 tsutsui printf("%s: interface not running\n", device_xname(sc->sc_dev));
1051 1.76 tsutsui return error;
1052 1.19 thorpej }
1053 1.19 thorpej
1054 1.19 thorpej /*
1055 1.19 thorpej * Drain the receive queue.
1056 1.19 thorpej */
1057 1.91 thorpej static void
1058 1.76 tsutsui epic_rxdrain(struct epic_softc *sc)
1059 1.19 thorpej {
1060 1.19 thorpej struct epic_descsoft *ds;
1061 1.19 thorpej int i;
1062 1.19 thorpej
1063 1.19 thorpej for (i = 0; i < EPIC_NRXDESC; i++) {
1064 1.19 thorpej ds = EPIC_DSRX(sc, i);
1065 1.19 thorpej if (ds->ds_mbuf != NULL) {
1066 1.19 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1067 1.19 thorpej m_freem(ds->ds_mbuf);
1068 1.19 thorpej ds->ds_mbuf = NULL;
1069 1.19 thorpej }
1070 1.19 thorpej }
1071 1.1 thorpej }
1072 1.1 thorpej
1073 1.1 thorpej /*
1074 1.1 thorpej * Stop transmission on the interface.
1075 1.1 thorpej */
1076 1.91 thorpej static void
1077 1.76 tsutsui epic_stop(struct ifnet *ifp, int disable)
1078 1.1 thorpej {
1079 1.34 thorpej struct epic_softc *sc = ifp->if_softc;
1080 1.1 thorpej bus_space_tag_t st = sc->sc_st;
1081 1.1 thorpej bus_space_handle_t sh = sc->sc_sh;
1082 1.1 thorpej struct epic_descsoft *ds;
1083 1.63 tsutsui uint32_t reg;
1084 1.1 thorpej int i;
1085 1.6 thorpej
1086 1.8 thorpej /*
1087 1.8 thorpej * Stop the one second clock.
1088 1.8 thorpej */
1089 1.29 thorpej callout_stop(&sc->sc_mii_callout);
1090 1.23 thorpej
1091 1.23 thorpej /* Down the MII. */
1092 1.23 thorpej mii_down(&sc->sc_mii);
1093 1.8 thorpej
1094 1.6 thorpej /* Paranoia... */
1095 1.6 thorpej epic_fixup_clock_source(sc);
1096 1.1 thorpej
1097 1.1 thorpej /*
1098 1.1 thorpej * Disable interrupts.
1099 1.1 thorpej */
1100 1.1 thorpej reg = bus_space_read_4(st, sh, EPIC_GENCTL);
1101 1.1 thorpej bus_space_write_4(st, sh, EPIC_GENCTL, reg & ~GENCTL_INTENA);
1102 1.1 thorpej bus_space_write_4(st, sh, EPIC_INTMASK, 0);
1103 1.1 thorpej
1104 1.1 thorpej /*
1105 1.1 thorpej * Stop the DMA engine and take the receiver off-line.
1106 1.1 thorpej */
1107 1.1 thorpej bus_space_write_4(st, sh, EPIC_COMMAND, COMMAND_STOP_RDMA |
1108 1.1 thorpej COMMAND_STOP_TDMA | COMMAND_STOP_RX);
1109 1.1 thorpej
1110 1.1 thorpej /*
1111 1.1 thorpej * Release any queued transmit buffers.
1112 1.1 thorpej */
1113 1.1 thorpej for (i = 0; i < EPIC_NTXDESC; i++) {
1114 1.10 thorpej ds = EPIC_DSTX(sc, i);
1115 1.1 thorpej if (ds->ds_mbuf != NULL) {
1116 1.1 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1117 1.1 thorpej m_freem(ds->ds_mbuf);
1118 1.1 thorpej ds->ds_mbuf = NULL;
1119 1.1 thorpej }
1120 1.19 thorpej }
1121 1.19 thorpej
1122 1.1 thorpej /*
1123 1.1 thorpej * Mark the interface down and cancel the watchdog timer.
1124 1.1 thorpej */
1125 1.1 thorpej ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1126 1.1 thorpej ifp->if_timer = 0;
1127 1.72 dyoung
1128 1.72 dyoung if (disable)
1129 1.72 dyoung epic_rxdrain(sc);
1130 1.1 thorpej }
1131 1.1 thorpej
1132 1.1 thorpej /*
1133 1.1 thorpej * Read the EPIC Serial EEPROM.
1134 1.1 thorpej */
1135 1.91 thorpej static void
1136 1.76 tsutsui epic_read_eeprom(struct epic_softc *sc, int word, int wordcnt, uint16_t *data)
1137 1.1 thorpej {
1138 1.1 thorpej bus_space_tag_t st = sc->sc_st;
1139 1.1 thorpej bus_space_handle_t sh = sc->sc_sh;
1140 1.63 tsutsui uint16_t reg;
1141 1.1 thorpej int i, x;
1142 1.1 thorpej
1143 1.1 thorpej #define EEPROM_WAIT_READY(st, sh) \
1144 1.1 thorpej while ((bus_space_read_4((st), (sh), EPIC_EECTL) & EECTL_EERDY) == 0) \
1145 1.1 thorpej /* nothing */
1146 1.1 thorpej
1147 1.1 thorpej /*
1148 1.1 thorpej * Enable the EEPROM.
1149 1.1 thorpej */
1150 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
1151 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1152 1.1 thorpej
1153 1.1 thorpej for (i = 0; i < wordcnt; i++) {
1154 1.1 thorpej /* Send CHIP SELECT for one clock tick. */
1155 1.88 msaitoh bus_space_write_4(st, sh, EPIC_EECTL,
1156 1.88 msaitoh EECTL_ENABLE | EECTL_EECS);
1157 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1158 1.1 thorpej
1159 1.1 thorpej /* Shift in the READ opcode. */
1160 1.1 thorpej for (x = 3; x > 0; x--) {
1161 1.88 msaitoh reg = EECTL_ENABLE | EECTL_EECS;
1162 1.1 thorpej if (EPIC_EEPROM_OPC_READ & (1 << (x - 1)))
1163 1.1 thorpej reg |= EECTL_EEDI;
1164 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, reg);
1165 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1166 1.88 msaitoh bus_space_write_4(st, sh, EPIC_EECTL, reg |EECTL_EESK);
1167 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1168 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, reg);
1169 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1170 1.1 thorpej }
1171 1.1 thorpej
1172 1.1 thorpej /* Shift in address. */
1173 1.1 thorpej for (x = 6; x > 0; x--) {
1174 1.88 msaitoh reg = EECTL_ENABLE | EECTL_EECS;
1175 1.1 thorpej if ((word + i) & (1 << (x - 1)))
1176 1.59 perry reg |= EECTL_EEDI;
1177 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, reg);
1178 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1179 1.88 msaitoh bus_space_write_4(st, sh, EPIC_EECTL, reg |EECTL_EESK);
1180 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1181 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, reg);
1182 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1183 1.1 thorpej }
1184 1.1 thorpej
1185 1.1 thorpej /* Shift out data. */
1186 1.88 msaitoh reg = EECTL_ENABLE | EECTL_EECS;
1187 1.1 thorpej data[i] = 0;
1188 1.1 thorpej for (x = 16; x > 0; x--) {
1189 1.88 msaitoh bus_space_write_4(st, sh, EPIC_EECTL, reg |EECTL_EESK);
1190 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1191 1.1 thorpej if (bus_space_read_4(st, sh, EPIC_EECTL) & EECTL_EEDO)
1192 1.1 thorpej data[i] |= (1 << (x - 1));
1193 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, reg);
1194 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1195 1.1 thorpej }
1196 1.1 thorpej
1197 1.1 thorpej /* Clear CHIP SELECT. */
1198 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
1199 1.1 thorpej EEPROM_WAIT_READY(st, sh);
1200 1.1 thorpej }
1201 1.1 thorpej
1202 1.1 thorpej /*
1203 1.1 thorpej * Disable the EEPROM.
1204 1.1 thorpej */
1205 1.1 thorpej bus_space_write_4(st, sh, EPIC_EECTL, 0);
1206 1.1 thorpej
1207 1.1 thorpej #undef EEPROM_WAIT_READY
1208 1.1 thorpej }
1209 1.1 thorpej
1210 1.1 thorpej /*
1211 1.1 thorpej * Add a receive buffer to the indicated descriptor.
1212 1.1 thorpej */
1213 1.91 thorpej static int
1214 1.76 tsutsui epic_add_rxbuf(struct epic_softc *sc, int idx)
1215 1.1 thorpej {
1216 1.10 thorpej struct epic_descsoft *ds = EPIC_DSRX(sc, idx);
1217 1.10 thorpej struct mbuf *m;
1218 1.10 thorpej int error;
1219 1.1 thorpej
1220 1.10 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
1221 1.10 thorpej if (m == NULL)
1222 1.76 tsutsui return ENOBUFS;
1223 1.1 thorpej
1224 1.10 thorpej MCLGET(m, M_DONTWAIT);
1225 1.10 thorpej if ((m->m_flags & M_EXT) == 0) {
1226 1.10 thorpej m_freem(m);
1227 1.76 tsutsui return ENOBUFS;
1228 1.1 thorpej }
1229 1.1 thorpej
1230 1.10 thorpej if (ds->ds_mbuf != NULL)
1231 1.10 thorpej bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1232 1.10 thorpej
1233 1.1 thorpej ds->ds_mbuf = m;
1234 1.1 thorpej
1235 1.10 thorpej error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1236 1.47 thorpej m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1237 1.88 msaitoh BUS_DMA_READ | BUS_DMA_NOWAIT);
1238 1.10 thorpej if (error) {
1239 1.76 tsutsui printf("%s: can't load rx DMA map %d, error = %d\n",
1240 1.76 tsutsui device_xname(sc->sc_dev), idx, error);
1241 1.76 tsutsui panic("%s", __func__); /* XXX */
1242 1.1 thorpej }
1243 1.1 thorpej
1244 1.1 thorpej bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1245 1.1 thorpej ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1246 1.1 thorpej
1247 1.10 thorpej EPIC_INIT_RXDESC(sc, idx);
1248 1.1 thorpej
1249 1.76 tsutsui return 0;
1250 1.1 thorpej }
1251 1.1 thorpej
1252 1.1 thorpej /*
1253 1.1 thorpej * Set the EPIC multicast hash table.
1254 1.13 thorpej *
1255 1.13 thorpej * NOTE: We rely on a recently-updated mii_media_active here!
1256 1.1 thorpej */
1257 1.91 thorpej static void
1258 1.76 tsutsui epic_set_mchash(struct epic_softc *sc)
1259 1.1 thorpej {
1260 1.1 thorpej struct ethercom *ec = &sc->sc_ethercom;
1261 1.1 thorpej struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1262 1.1 thorpej struct ether_multi *enm;
1263 1.1 thorpej struct ether_multistep step;
1264 1.63 tsutsui uint32_t hash, mchash[4];
1265 1.1 thorpej
1266 1.1 thorpej /*
1267 1.1 thorpej * Set up the multicast address filter by passing all multicast
1268 1.31 thorpej * addresses through a CRC generator, and then using the low-order
1269 1.1 thorpej * 6 bits as an index into the 64 bit multicast hash table (only
1270 1.1 thorpej * the lower 16 bits of each 32 bit multicast hash register are
1271 1.31 thorpej * valid). The high order bits select the register, while the
1272 1.1 thorpej * rest of the bits select the bit within the register.
1273 1.1 thorpej */
1274 1.1 thorpej
1275 1.1 thorpej if (ifp->if_flags & IFF_PROMISC)
1276 1.1 thorpej goto allmulti;
1277 1.1 thorpej
1278 1.13 thorpej if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
1279 1.13 thorpej /* XXX hardware bug in 10Mbps mode. */
1280 1.13 thorpej goto allmulti;
1281 1.13 thorpej }
1282 1.1 thorpej
1283 1.1 thorpej mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0;
1284 1.1 thorpej
1285 1.90 msaitoh ETHER_LOCK(ec);
1286 1.1 thorpej ETHER_FIRST_MULTI(step, ec, enm);
1287 1.1 thorpej while (enm != NULL) {
1288 1.46 thorpej if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1289 1.1 thorpej /*
1290 1.1 thorpej * We must listen to a range of multicast addresses.
1291 1.1 thorpej * For now, just accept all multicasts, rather than
1292 1.1 thorpej * trying to set only those filter bits needed to match
1293 1.1 thorpej * the range. (At this time, the only use of address
1294 1.1 thorpej * ranges is for IP multicast routing, for which the
1295 1.1 thorpej * range is big enough to require all bits set.)
1296 1.1 thorpej */
1297 1.90 msaitoh ETHER_UNLOCK(ec);
1298 1.1 thorpej goto allmulti;
1299 1.1 thorpej }
1300 1.1 thorpej
1301 1.37 thorpej hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1302 1.37 thorpej hash >>= 26;
1303 1.1 thorpej
1304 1.1 thorpej /* Set the corresponding bit in the hash table. */
1305 1.31 thorpej mchash[hash >> 4] |= 1 << (hash & 0xf);
1306 1.1 thorpej
1307 1.1 thorpej ETHER_NEXT_MULTI(step, enm);
1308 1.1 thorpej }
1309 1.90 msaitoh ETHER_UNLOCK(ec);
1310 1.1 thorpej
1311 1.1 thorpej ifp->if_flags &= ~IFF_ALLMULTI;
1312 1.1 thorpej goto sethash;
1313 1.1 thorpej
1314 1.1 thorpej allmulti:
1315 1.1 thorpej ifp->if_flags |= IFF_ALLMULTI;
1316 1.1 thorpej mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0xffff;
1317 1.1 thorpej
1318 1.1 thorpej sethash:
1319 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC0, mchash[0]);
1320 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC1, mchash[1]);
1321 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC2, mchash[2]);
1322 1.1 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC3, mchash[3]);
1323 1.8 thorpej }
1324 1.8 thorpej
1325 1.8 thorpej /*
1326 1.8 thorpej * Wait for the MII to become ready.
1327 1.8 thorpej */
1328 1.91 thorpej static int
1329 1.76 tsutsui epic_mii_wait(struct epic_softc *sc, uint32_t rw)
1330 1.8 thorpej {
1331 1.8 thorpej int i;
1332 1.8 thorpej
1333 1.8 thorpej for (i = 0; i < 50; i++) {
1334 1.8 thorpej if ((bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL) & rw)
1335 1.8 thorpej == 0)
1336 1.8 thorpej break;
1337 1.8 thorpej delay(2);
1338 1.8 thorpej }
1339 1.8 thorpej if (i == 50) {
1340 1.76 tsutsui printf("%s: MII timed out\n", device_xname(sc->sc_dev));
1341 1.87 msaitoh return ETIMEDOUT;
1342 1.8 thorpej }
1343 1.8 thorpej
1344 1.76 tsutsui return 0;
1345 1.8 thorpej }
1346 1.8 thorpej
1347 1.8 thorpej /*
1348 1.8 thorpej * Read from the MII.
1349 1.8 thorpej */
1350 1.91 thorpej static int
1351 1.87 msaitoh epic_mii_read(device_t self, int phy, int reg, uint16_t *val)
1352 1.8 thorpej {
1353 1.76 tsutsui struct epic_softc *sc = device_private(self);
1354 1.87 msaitoh int rv;
1355 1.8 thorpej
1356 1.87 msaitoh if ((rv = epic_mii_wait(sc, MMCTL_WRITE)) != 0)
1357 1.87 msaitoh return rv;
1358 1.8 thorpej
1359 1.8 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
1360 1.8 thorpej MMCTL_ARG(phy, reg, MMCTL_READ));
1361 1.8 thorpej
1362 1.87 msaitoh if ((rv = epic_mii_wait(sc, MMCTL_READ)) != 0)
1363 1.87 msaitoh return rv;
1364 1.8 thorpej
1365 1.87 msaitoh *val = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA)
1366 1.87 msaitoh & MMDATA_MASK;
1367 1.87 msaitoh return 0;
1368 1.8 thorpej }
1369 1.8 thorpej
1370 1.8 thorpej /*
1371 1.8 thorpej * Write to the MII.
1372 1.8 thorpej */
1373 1.91 thorpej static int
1374 1.87 msaitoh epic_mii_write(device_t self, int phy, int reg, uint16_t val)
1375 1.8 thorpej {
1376 1.76 tsutsui struct epic_softc *sc = device_private(self);
1377 1.87 msaitoh int rv;
1378 1.8 thorpej
1379 1.87 msaitoh if ((rv = epic_mii_wait(sc, MMCTL_WRITE)) != 0)
1380 1.87 msaitoh return rv;
1381 1.8 thorpej
1382 1.8 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA, val);
1383 1.8 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
1384 1.8 thorpej MMCTL_ARG(phy, reg, MMCTL_WRITE));
1385 1.87 msaitoh
1386 1.87 msaitoh return 0;
1387 1.8 thorpej }
1388 1.8 thorpej
1389 1.8 thorpej /*
1390 1.8 thorpej * Callback from PHY when media changes.
1391 1.8 thorpej */
1392 1.91 thorpej static void
1393 1.81 matt epic_statchg(struct ifnet *ifp)
1394 1.8 thorpej {
1395 1.81 matt struct epic_softc *sc = ifp->if_softc;
1396 1.63 tsutsui uint32_t txcon, miicfg;
1397 1.11 thorpej
1398 1.11 thorpej /*
1399 1.11 thorpej * Update loopback bits in TXCON to reflect duplex mode.
1400 1.11 thorpej */
1401 1.11 thorpej txcon = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_TXCON);
1402 1.11 thorpej if (sc->sc_mii.mii_media_active & IFM_FDX)
1403 1.88 msaitoh txcon |= (TXCON_LOOPBACK_D1 | TXCON_LOOPBACK_D2);
1404 1.11 thorpej else
1405 1.88 msaitoh txcon &= ~(TXCON_LOOPBACK_D1 | TXCON_LOOPBACK_D2);
1406 1.11 thorpej bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TXCON, txcon);
1407 1.13 thorpej
1408 1.43 drochner /* On some cards we need manualy set fullduplex led */
1409 1.43 drochner if (sc->sc_hwflags & EPIC_DUPLEXLED_ON_694) {
1410 1.43 drochner miicfg = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG);
1411 1.59 perry if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX)
1412 1.43 drochner miicfg |= MIICFG_ENABLE;
1413 1.43 drochner else
1414 1.43 drochner miicfg &= ~MIICFG_ENABLE;
1415 1.43 drochner bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG, miicfg);
1416 1.43 drochner }
1417 1.43 drochner
1418 1.13 thorpej /*
1419 1.13 thorpej * There is a multicast filter bug in 10Mbps mode. Kick the
1420 1.13 thorpej * multicast filter in case the speed changed.
1421 1.13 thorpej */
1422 1.13 thorpej epic_set_mchash(sc);
1423 1.8 thorpej }
1424 1.8 thorpej
1425 1.8 thorpej /*
1426 1.8 thorpej * Callback from ifmedia to request new media setting.
1427 1.70 dyoung *
1428 1.70 dyoung * XXX Looks to me like some of this complexity should move into
1429 1.70 dyoung * XXX one or two custom PHY drivers. --dyoung
1430 1.8 thorpej */
1431 1.91 thorpej static int
1432 1.76 tsutsui epic_mediachange(struct ifnet *ifp)
1433 1.8 thorpej {
1434 1.11 thorpej struct epic_softc *sc = ifp->if_softc;
1435 1.43 drochner struct mii_data *mii = &sc->sc_mii;
1436 1.43 drochner struct ifmedia *ifm = &mii->mii_media;
1437 1.43 drochner int media = ifm->ifm_cur->ifm_media;
1438 1.63 tsutsui uint32_t miicfg;
1439 1.43 drochner struct mii_softc *miisc;
1440 1.87 msaitoh int rc;
1441 1.87 msaitoh uint16_t cfg;
1442 1.43 drochner
1443 1.70 dyoung if ((ifp->if_flags & IFF_UP) == 0)
1444 1.76 tsutsui return 0;
1445 1.43 drochner
1446 1.43 drochner if (IFM_INST(media) != sc->sc_serinst) {
1447 1.43 drochner /* If we're not selecting serial interface, select MII mode */
1448 1.43 drochner #ifdef EPICMEDIADEBUG
1449 1.43 drochner printf("%s: parallel mode\n", ifp->if_xname);
1450 1.59 perry #endif
1451 1.43 drochner miicfg = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG);
1452 1.43 drochner miicfg &= ~MIICFG_SERMODEENA;
1453 1.43 drochner bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG, miicfg);
1454 1.43 drochner }
1455 1.43 drochner
1456 1.71 dyoung if ((rc = mii_mediachg(mii)) == ENXIO)
1457 1.71 dyoung rc = 0;
1458 1.43 drochner
1459 1.43 drochner if (IFM_INST(media) == sc->sc_serinst) {
1460 1.43 drochner /* select serial interface */
1461 1.43 drochner #ifdef EPICMEDIADEBUG
1462 1.43 drochner printf("%s: serial mode\n", ifp->if_xname);
1463 1.43 drochner #endif
1464 1.43 drochner miicfg = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG);
1465 1.43 drochner miicfg |= (MIICFG_SERMODEENA | MIICFG_ENABLE);
1466 1.43 drochner bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG, miicfg);
1467 1.43 drochner
1468 1.43 drochner /* There is no driver to fill this */
1469 1.43 drochner mii->mii_media_active = media;
1470 1.43 drochner mii->mii_media_status = 0;
1471 1.43 drochner
1472 1.81 matt epic_statchg(mii->mii_ifp);
1473 1.76 tsutsui return 0;
1474 1.43 drochner }
1475 1.43 drochner
1476 1.43 drochner /* Lookup selected PHY */
1477 1.69 dyoung LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1478 1.43 drochner if (IFM_INST(media) == miisc->mii_inst)
1479 1.43 drochner break;
1480 1.43 drochner }
1481 1.43 drochner if (!miisc) {
1482 1.76 tsutsui printf("%s: can't happen\n", __func__); /* ??? panic */
1483 1.76 tsutsui return 0;
1484 1.43 drochner }
1485 1.43 drochner #ifdef EPICMEDIADEBUG
1486 1.43 drochner printf("%s: using phy %s\n", ifp->if_xname,
1487 1.75 xtraeme device_xname(miisc->mii_dev));
1488 1.43 drochner #endif
1489 1.43 drochner
1490 1.43 drochner if (miisc->mii_flags & MIIF_HAVEFIBER) {
1491 1.43 drochner /* XXX XXX assume it's a Level1 - should check */
1492 1.43 drochner
1493 1.54 wiz /* We have to powerup fiber transceivers */
1494 1.87 msaitoh PHY_READ(miisc, MII_LXTPHY_CONFIG, &cfg);
1495 1.43 drochner if (IFM_SUBTYPE(media) == IFM_100_FX) {
1496 1.43 drochner #ifdef EPICMEDIADEBUG
1497 1.43 drochner printf("%s: power up fiber\n", ifp->if_xname);
1498 1.43 drochner #endif
1499 1.43 drochner cfg |= (CONFIG_LEDC1 | CONFIG_LEDC0);
1500 1.43 drochner } else {
1501 1.43 drochner #ifdef EPICMEDIADEBUG
1502 1.43 drochner printf("%s: power down fiber\n", ifp->if_xname);
1503 1.43 drochner #endif
1504 1.43 drochner cfg &= ~(CONFIG_LEDC1 | CONFIG_LEDC0);
1505 1.43 drochner }
1506 1.43 drochner PHY_WRITE(miisc, MII_LXTPHY_CONFIG, cfg);
1507 1.43 drochner }
1508 1.8 thorpej
1509 1.71 dyoung return rc;
1510 1.1 thorpej }
1511