if_admsw.c revision 1.1 1 /* $NetBSD: if_admsw.c,v 1.1 2007/03/20 08:52:01 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. The names of the authors may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 2001 Wasabi Systems, Inc.
35 * All rights reserved.
36 *
37 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed for the NetBSD Project by
50 * Wasabi Systems, Inc.
51 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
52 * or promote products derived from this software without specific prior
53 * written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
66 */
67
68 /*
69 * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media
70 * Access Controller.
71 *
72 * TODO:
73 *
74 * Better Rx buffer management; we want to get new Rx buffers
75 * to the chip more quickly than we currently do.
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: if_admsw.c,v 1.1 2007/03/20 08:52:01 dyoung Exp $");
80
81 #include "bpfilter.h"
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/callout.h>
86 #include <sys/mbuf.h>
87 #include <sys/malloc.h>
88 #include <sys/kernel.h>
89 #include <sys/socket.h>
90 #include <sys/ioctl.h>
91 #include <sys/errno.h>
92 #include <sys/device.h>
93 #include <sys/queue.h>
94
95 #include <prop/proplib.h>
96
97 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
98
99 #include <net/if.h>
100 #include <net/if_dl.h>
101 #include <net/if_media.h>
102 #include <net/if_ether.h>
103
104 #if NBPFILTER > 0
105 #include <net/bpf.h>
106 #endif
107
108 #include <machine/bus.h>
109 #include <machine/intr.h>
110 #include <machine/endian.h>
111
112 #include <dev/mii/mii.h>
113 #include <dev/mii/miivar.h>
114
115 #include <sys/gpio.h>
116 #include <dev/gpio/gpiovar.h>
117
118 #include <mips/adm5120/include/adm5120reg.h>
119 #include <mips/adm5120/include/adm5120var.h>
120 #include <mips/adm5120/include/adm5120_obiovar.h>
121 #include <mips/adm5120/dev/if_admswreg.h>
122 #include <mips/adm5120/dev/if_admswvar.h>
123
124 static uint8_t vlan_matrix[SW_DEVS] = {
125 (1 << 6) | (1 << 0), /* CPU + port0 */
126 (1 << 6) | (1 << 1), /* CPU + port1 */
127 (1 << 6) | (1 << 2), /* CPU + port2 */
128 (1 << 6) | (1 << 3), /* CPU + port3 */
129 (1 << 6) | (1 << 4), /* CPU + port4 */
130 (1 << 6) | (1 << 5), /* CPU + port5 */
131 };
132
133 #ifdef ADMSW_EVENT_COUNTERS
134 #define ADMSW_EVCNT_INCR(ev) (ev)->ev_count++
135 #else
136 #define ADMSW_EVCNT_INCR(ev) /* nothing */
137 #endif
138
139 static void admsw_start(struct ifnet *);
140 static void admsw_watchdog(struct ifnet *);
141 static int admsw_ioctl(struct ifnet *, u_long, void *);
142 static int admsw_init(struct ifnet *);
143 static void admsw_stop(struct ifnet *, int);
144
145 static void admsw_shutdown(void *);
146
147 static void admsw_reset(struct admsw_softc *);
148 static void admsw_set_filter(struct admsw_softc *);
149
150 static int admsw_intr(void *);
151 static void admsw_txintr(struct admsw_softc *, int);
152 static void admsw_rxintr(struct admsw_softc *, int);
153 static int admsw_add_rxbuf(struct admsw_softc *, int, int);
154 #define admsw_add_rxhbuf(sc, idx) admsw_add_rxbuf(sc, idx, 1)
155 #define admsw_add_rxlbuf(sc, idx) admsw_add_rxbuf(sc, idx, 0)
156
157 static int admsw_mediachange(struct ifnet *);
158 static void admsw_mediastatus(struct ifnet *, struct ifmediareq *);
159
160 static int admsw_match(struct device *, struct cfdata *, void *);
161 static void admsw_attach(struct device *, struct device *, void *);
162
163 CFATTACH_DECL(admsw, sizeof(struct admsw_softc),
164 admsw_match, admsw_attach, NULL, NULL);
165
166 static int
167 admsw_match(struct device *parent, struct cfdata *cf, void *aux)
168 {
169 struct obio_attach_args *aa = aux;
170
171 return strcmp(aa->oba_name, cf->cf_name) == 0;
172 }
173
174 #define REG_READ(o) bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
175 #define REG_WRITE(o,v) bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
176
177
178 static void
179 admsw_init_bufs(struct admsw_softc *sc)
180 {
181 int i;
182 struct admsw_desc *desc;
183
184 for (i = 0; i < ADMSW_NTXHDESC; i++) {
185 if (sc->sc_txhsoft[i].ds_mbuf != NULL) {
186 m_freem(sc->sc_txhsoft[i].ds_mbuf);
187 sc->sc_txhsoft[i].ds_mbuf = NULL;
188 }
189 desc = &sc->sc_txhdescs[i];
190 desc->data = 0;
191 desc->cntl = 0;
192 desc->len = MAC_BUFLEN;
193 desc->status = 0;
194 ADMSW_CDTXHSYNC(sc, i,
195 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
196 }
197 sc->sc_txhdescs[ADMSW_NTXHDESC - 1].data |= ADM5120_DMA_RINGEND;
198 ADMSW_CDTXHSYNC(sc, ADMSW_NTXHDESC - 1,
199 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
200
201 for (i = 0; i < ADMSW_NRXHDESC; i++) {
202 if (sc->sc_rxhsoft[i].ds_mbuf == NULL) {
203 if (admsw_add_rxhbuf(sc, i) != 0)
204 panic("admsw_init_bufs\n");
205 } else
206 ADMSW_INIT_RXHDESC(sc, i);
207 }
208
209 for (i = 0; i < ADMSW_NTXLDESC; i++) {
210 if (sc->sc_txlsoft[i].ds_mbuf != NULL) {
211 m_freem(sc->sc_txlsoft[i].ds_mbuf);
212 sc->sc_txlsoft[i].ds_mbuf = NULL;
213 }
214 desc = &sc->sc_txldescs[i];
215 desc->data = 0;
216 desc->cntl = 0;
217 desc->len = MAC_BUFLEN;
218 desc->status = 0;
219 ADMSW_CDTXLSYNC(sc, i,
220 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
221 }
222 sc->sc_txldescs[ADMSW_NTXLDESC - 1].data |= ADM5120_DMA_RINGEND;
223 ADMSW_CDTXLSYNC(sc, ADMSW_NTXLDESC - 1,
224 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
225
226 for (i = 0; i < ADMSW_NRXLDESC; i++) {
227 if (sc->sc_rxlsoft[i].ds_mbuf == NULL) {
228 if (admsw_add_rxlbuf(sc, i) != 0)
229 panic("admsw_init_bufs\n");
230 } else
231 ADMSW_INIT_RXLDESC(sc, i);
232 }
233
234 REG_WRITE(SEND_HBADDR_REG, ADMSW_CDTXHADDR(sc, 0));
235 REG_WRITE(SEND_LBADDR_REG, ADMSW_CDTXLADDR(sc, 0));
236 REG_WRITE(RECV_HBADDR_REG, ADMSW_CDRXHADDR(sc, 0));
237 REG_WRITE(RECV_LBADDR_REG, ADMSW_CDRXLADDR(sc, 0));
238
239 sc->sc_txfree = ADMSW_NTXLDESC;
240 sc->sc_txnext = 0;
241 sc->sc_txdirty = 0;
242 sc->sc_rxptr = 0;
243 }
244
245 static void
246 admsw_setvlan(struct admsw_softc *sc, char matrix[6])
247 {
248 uint32_t i;
249
250 i = matrix[0] + (matrix[1] << 8) + (matrix[2] << 16) + (matrix[3] << 24);
251 REG_WRITE(VLAN_G1_REG, i);
252 i = matrix[4] + (matrix[5] << 8);
253 REG_WRITE(VLAN_G2_REG, i);
254 }
255
256 static void
257 admsw_reset(struct admsw_softc *sc)
258 {
259 uint32_t wdog1;
260 int i;
261
262 REG_WRITE(PORT_CONF0_REG,
263 REG_READ(PORT_CONF0_REG) | PORT_CONF0_DP_MASK);
264 REG_WRITE(CPUP_CONF_REG,
265 REG_READ(CPUP_CONF_REG) | CPUP_CONF_DCPUP);
266
267 /* Wait for DMA to complete. Overkill. In 3ms, we can
268 * send at least two entire 1500-byte packets at 10 Mb/s.
269 */
270 DELAY(3000);
271
272 /* The datasheet recommends that we move all PHYs to reset
273 * state prior to software reset.
274 */
275 REG_WRITE(PHY_CNTL2_REG,
276 REG_READ(PHY_CNTL2_REG) & ~PHY_CNTL2_PHYR_MASK);
277
278 /* Reset the switch. */
279 REG_WRITE(ADMSW_SW_RES, 0x1);
280
281 DELAY(100 * 1000);
282
283 REG_WRITE(ADMSW_BOOT_DONE, ADMSW_BOOT_DONE_BO);
284
285 /* begin old code */
286 REG_WRITE(CPUP_CONF_REG,
287 CPUP_CONF_DCPUP | CPUP_CONF_CRCP | CPUP_CONF_DUNP_MASK |
288 CPUP_CONF_DMCP_MASK);
289
290 REG_WRITE(PORT_CONF0_REG, PORT_CONF0_EMCP_MASK | PORT_CONF0_EMBP_MASK);
291
292 REG_WRITE(PHY_CNTL2_REG,
293 REG_READ(PHY_CNTL2_REG) | PHY_CNTL2_ANE_MASK | PHY_CNTL2_PHYR_MASK |
294 PHY_CNTL2_AMDIX_MASK);
295
296 REG_WRITE(PHY_CNTL3_REG, REG_READ(PHY_CNTL3_REG) | PHY_CNTL3_RNT);
297
298 REG_WRITE(ADMSW_INT_MASK, INT_MASK);
299 REG_WRITE(ADMSW_INT_ST, INT_MASK);
300
301 /*
302 * While in DDB, we stop servicing interrupts, RX ring
303 * fills up and when free block counter falls behind FC
304 * threshold, the switch starts to emit 802.3x PAUSE
305 * frames. This can upset peer switches.
306 *
307 * Stop this from happening by disabling FC and D2
308 * thresholds.
309 */
310 REG_WRITE(FC_TH_REG,
311 REG_READ(FC_TH_REG) & ~(FC_TH_FCS_MASK | FC_TH_D2S_MASK));
312
313 admsw_setvlan(sc, vlan_matrix);
314
315 for (i = 0; i < SW_DEVS; i++) {
316 REG_WRITE(MAC_WT1_REG,
317 sc->sc_enaddr[2] |
318 (sc->sc_enaddr[3]<<8) |
319 (sc->sc_enaddr[4]<<16) |
320 ((sc->sc_enaddr[5]+i)<<24));
321 REG_WRITE(MAC_WT0_REG, (i<<MAC_WT0_VLANID_SHIFT) |
322 (sc->sc_enaddr[0]<<16) | (sc->sc_enaddr[1]<<24) |
323 MAC_WT0_WRITE | MAC_WT0_VLANID_EN);
324
325 while (!(REG_READ(MAC_WT0_REG) & MAC_WT0_WRITE_DONE));
326 }
327 wdog1 = REG_READ(ADM5120_WDOG1);
328 printf("%s: reg[ADM5120_WDOG1] = 0x%08" PRIx32 "\n", __func__, wdog1);
329 REG_WRITE(ADM5120_WDOG1, wdog1 & ~ADM5120_WDOG1_WDE);
330 }
331
332 static void
333 admsw_attach(struct device *parent, struct device *self, void *aux)
334 {
335 uint8_t enaddr[ETHER_ADDR_LEN];
336 struct admsw_softc *sc = (void *) self;
337 struct obio_attach_args *aa = aux;
338 struct ifnet *ifp;
339 bus_dma_segment_t seg;
340 int error, i, rseg;
341 prop_data_t pd;
342
343 printf(": ADM5120 Switch Engine, %d ports\n", SW_DEVS);
344
345 sc->sc_dmat = aa->oba_dt;
346 sc->sc_st = aa->oba_st;
347
348 pd = prop_dictionary_get(device_properties(&sc->sc_dev), "mac-addr");
349
350 if (pd == NULL) {
351 enaddr[0] = 0x02;
352 enaddr[1] = 0xaa;
353 enaddr[2] = 0xbb;
354 enaddr[3] = 0xcc;
355 enaddr[4] = 0xdd;
356 enaddr[5] = 0xee;
357 } else
358 memcpy(enaddr, prop_data_data_nocopy(pd), sizeof(enaddr));
359
360 memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
361
362 printf("%s: base Ethernet address %s\n", sc->sc_dev.dv_xname,
363 ether_sprintf(enaddr));
364
365 /* Map the device. */
366 if (bus_space_map(sc->sc_st, aa->oba_addr, 512, 0, &sc->sc_ioh) != 0) {
367 printf("%s: unable to map device\n", device_xname(&sc->sc_dev));
368 return;
369 }
370
371 /* Hook up the interrupt handler. */
372 sc->sc_ih = adm5120_intr_establish(aa->oba_irq, INTR_IRQ, admsw_intr, sc);
373
374 if (sc->sc_ih == NULL) {
375 printf("%s: unable to register interrupt handler\n",
376 sc->sc_dev.dv_xname);
377 return;
378 }
379
380 /*
381 * Allocate the control data structures, and create and load the
382 * DMA map for it.
383 */
384 if ((error = bus_dmamem_alloc(sc->sc_dmat,
385 sizeof(struct admsw_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
386 0)) != 0) {
387 printf("%s: unable to allocate control data, error = %d\n",
388 sc->sc_dev.dv_xname, error);
389 return;
390 }
391 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
392 sizeof(struct admsw_control_data), (void *)&sc->sc_control_data,
393 0)) != 0) {
394 printf("%s: unable to map control data, error = %d\n",
395 sc->sc_dev.dv_xname, error);
396 return;
397 }
398 if ((error = bus_dmamap_create(sc->sc_dmat,
399 sizeof(struct admsw_control_data), 1,
400 sizeof(struct admsw_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
401 printf("%s: unable to create control data DMA map, "
402 "error = %d\n", sc->sc_dev.dv_xname, error);
403 return;
404 }
405 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
406 sc->sc_control_data, sizeof(struct admsw_control_data), NULL,
407 0)) != 0) {
408 printf("%s: unable to load control data DMA map, error = %d\n",
409 sc->sc_dev.dv_xname, error);
410 return;
411 }
412
413 /*
414 * Create the transmit buffer DMA maps.
415 */
416 for (i = 0; i < ADMSW_NTXHDESC; i++) {
417 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
418 2, MCLBYTES, 0, 0,
419 &sc->sc_txhsoft[i].ds_dmamap)) != 0) {
420 printf("%s: unable to create txh DMA map %d, "
421 "error = %d\n", sc->sc_dev.dv_xname, i, error);
422 return;
423 }
424 sc->sc_txhsoft[i].ds_mbuf = NULL;
425 }
426 for (i = 0; i < ADMSW_NTXLDESC; i++) {
427 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
428 2, MCLBYTES, 0, 0,
429 &sc->sc_txlsoft[i].ds_dmamap)) != 0) {
430 printf("%s: unable to create txl DMA map %d, "
431 "error = %d\n", sc->sc_dev.dv_xname, i, error);
432 return;
433 }
434 sc->sc_txlsoft[i].ds_mbuf = NULL;
435 }
436
437 /*
438 * Create the receive buffer DMA maps.
439 */
440 for (i = 0; i < ADMSW_NRXHDESC; i++) {
441 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
442 MCLBYTES, 0, 0, &sc->sc_rxhsoft[i].ds_dmamap)) != 0) {
443 printf("%s: unable to create rxh DMA map %d, "
444 "error = %d\n", sc->sc_dev.dv_xname, i, error);
445 return;
446 }
447 sc->sc_rxhsoft[i].ds_mbuf = NULL;
448 }
449 for (i = 0; i < ADMSW_NRXLDESC; i++) {
450 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
451 MCLBYTES, 0, 0, &sc->sc_rxlsoft[i].ds_dmamap)) != 0) {
452 printf("%s: unable to create rxl DMA map %d, "
453 "error = %d\n", sc->sc_dev.dv_xname, i, error);
454 return;
455 }
456 sc->sc_rxlsoft[i].ds_mbuf = NULL;
457 }
458
459 admsw_init_bufs(sc);
460
461 admsw_reset(sc);
462
463 for (i = 0; i < SW_DEVS; i++) {
464 ifmedia_init(&sc->sc_ifmedia[i], 0, admsw_mediachange, admsw_mediastatus);
465 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_10_T, 0, NULL);
466 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
467 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_100_TX, 0, NULL);
468 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
469 ifmedia_add(&sc->sc_ifmedia[i], IFM_ETHER|IFM_AUTO, 0, NULL);
470 ifmedia_set(&sc->sc_ifmedia[i], IFM_ETHER|IFM_AUTO);
471
472 ifp = &sc->sc_ethercom[i].ec_if;
473 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
474 ifp->if_xname[5] += i;
475 ifp->if_softc = sc;
476 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
477 ifp->if_ioctl = admsw_ioctl;
478 ifp->if_start = admsw_start;
479 ifp->if_watchdog = admsw_watchdog;
480 ifp->if_init = admsw_init;
481 ifp->if_stop = admsw_stop;
482 ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
483 IFQ_SET_MAXLEN(&ifp->if_snd, max(ADMSW_NTXLDESC, IFQ_MAXLEN));
484 IFQ_SET_READY(&ifp->if_snd);
485
486 /* Attach the interface. */
487 if_attach(ifp);
488 ether_ifattach(ifp, enaddr);
489 enaddr[5]++;
490 }
491
492 #ifdef ADMSW_EVENT_COUNTERS
493 evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
494 NULL, sc->sc_dev.dv_xname, "txstall");
495 evcnt_attach_dynamic(&sc->sc_ev_rxstall, EVCNT_TYPE_MISC,
496 NULL, sc->sc_dev.dv_xname, "rxstall");
497 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_MISC,
498 NULL, sc->sc_dev.dv_xname, "txintr");
499 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_MISC,
500 NULL, sc->sc_dev.dv_xname, "rxintr");
501 #if 1
502 evcnt_attach_dynamic(&sc->sc_ev_rxsync, EVCNT_TYPE_MISC,
503 NULL, sc->sc_dev.dv_xname, "rxsync");
504 #endif
505 #endif
506
507 admwdog_attach(sc);
508
509 /* Make sure the interface is shutdown during reboot. */
510 sc->sc_sdhook = shutdownhook_establish(admsw_shutdown, sc);
511 if (sc->sc_sdhook == NULL)
512 printf("%s: WARNING: unable to establish shutdown hook\n",
513 sc->sc_dev.dv_xname);
514
515 /* leave interrupts and cpu port disabled */
516 return;
517 }
518
519
520 /*
521 * admsw_shutdown:
522 *
523 * Make sure the interface is stopped at reboot time.
524 */
525 static void
526 admsw_shutdown(void *arg)
527 {
528 struct admsw_softc *sc = arg;
529 int i;
530
531 for (i = 0; i < SW_DEVS; i++)
532 admsw_stop(&sc->sc_ethercom[i].ec_if, 1);
533 }
534
535 /*
536 * admsw_start: [ifnet interface function]
537 *
538 * Start packet transmission on the interface.
539 */
540 static void
541 admsw_start(struct ifnet *ifp)
542 {
543 struct admsw_softc *sc = ifp->if_softc;
544 struct mbuf *m0, *m;
545 struct admsw_descsoft *ds;
546 struct admsw_desc *desc;
547 bus_dmamap_t dmamap;
548 struct ether_header *eh;
549 int error, nexttx, len, i;
550 static int vlan = 0;
551
552 /*
553 * Loop through the send queues, setting up transmit descriptors
554 * unitl we drain the queues, or use up all available transmit
555 * descriptors.
556 */
557 for (;;) {
558 vlan++;
559 if (vlan == SW_DEVS)
560 vlan = 0;
561 i = vlan;
562 for (;;) {
563 ifp = &sc->sc_ethercom[i].ec_if;
564 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) ==
565 IFF_RUNNING) {
566 /* Grab a packet off the queue. */
567 IFQ_POLL(&ifp->if_snd, m0);
568 if (m0 != NULL)
569 break;
570 }
571 i++;
572 if (i == SW_DEVS)
573 i = 0;
574 if (i == vlan)
575 return;
576 }
577 vlan = i;
578 m = NULL;
579
580 /* Get a spare descriptor. */
581 if (sc->sc_txfree == 0) {
582 /* No more slots left; notify upper layer. */
583 ifp->if_flags |= IFF_OACTIVE;
584 ADMSW_EVCNT_INCR(&sc->sc_ev_txstall);
585 break;
586 }
587 nexttx = sc->sc_txnext;
588 desc = &sc->sc_txldescs[nexttx];
589 ds = &sc->sc_txlsoft[nexttx];
590 dmamap = ds->ds_dmamap;
591
592 /*
593 * Load the DMA map. If this fails, the packet either
594 * didn't fit in the alloted number of segments, or we
595 * were short on resources. In this case, we'll copy
596 * and try again.
597 */
598 if (m0->m_pkthdr.len < ETHER_MIN_LEN ||
599 bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
600 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
601 MGETHDR(m, M_DONTWAIT, MT_DATA);
602 if (m == NULL) {
603 printf("%s: unable to allocate Tx mbuf\n",
604 sc->sc_dev.dv_xname);
605 break;
606 }
607 if (m0->m_pkthdr.len > MHLEN) {
608 MCLGET(m, M_DONTWAIT);
609 if ((m->m_flags & M_EXT) == 0) {
610 printf("%s: unable to allocate Tx "
611 "cluster\n", sc->sc_dev.dv_xname);
612 m_freem(m);
613 break;
614 }
615 }
616 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
617 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
618 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
619 if (m->m_pkthdr.len < ETHER_MIN_LEN) {
620 if (M_TRAILINGSPACE(m) < ETHER_MIN_LEN - m->m_pkthdr.len)
621 panic("admsw_start: M_TRAILINGSPACE\n");
622 memset(mtod(m, uint8_t *) + m->m_pkthdr.len, 0,
623 ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len);
624 m->m_pkthdr.len = m->m_len = ETHER_MIN_LEN;
625 }
626 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
627 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
628 if (error) {
629 printf("%s: unable to load Tx buffer, "
630 "error = %d\n", sc->sc_dev.dv_xname, error);
631 break;
632 }
633 }
634
635 IFQ_DEQUEUE(&ifp->if_snd, m0);
636 if (m != NULL) {
637 m_freem(m0);
638 m0 = m;
639 }
640
641 /*
642 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
643 */
644
645 /* Sync the DMA map. */
646 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
647 BUS_DMASYNC_PREWRITE);
648
649 if (dmamap->dm_nsegs != 1 && dmamap->dm_nsegs != 2)
650 panic("admsw_start: dm_nsegs == %d\n", dmamap->dm_nsegs);
651 desc->data = dmamap->dm_segs[0].ds_addr;
652 desc->len = len = dmamap->dm_segs[0].ds_len;
653 if (dmamap->dm_nsegs > 1) {
654 len += dmamap->dm_segs[1].ds_len;
655 desc->cntl = dmamap->dm_segs[1].ds_addr | ADM5120_DMA_BUF2ENABLE;
656 } else
657 desc->cntl = 0;
658 desc->status = (len << ADM5120_DMA_LENSHIFT) | (1 << vlan);
659 eh = mtod(m0, struct ether_header *);
660 if (ntohs(eh->ether_type) == ETHERTYPE_IP &&
661 m0->m_pkthdr.csum_flags & M_CSUM_IPv4)
662 desc->status |= ADM5120_DMA_CSUM;
663 if (nexttx == ADMSW_NTXLDESC - 1)
664 desc->data |= ADM5120_DMA_RINGEND;
665 desc->data |= ADM5120_DMA_OWN;
666
667 /* Sync the descriptor. */
668 ADMSW_CDTXLSYNC(sc, nexttx,
669 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
670
671 REG_WRITE(SEND_TRIG_REG, 1);
672 /* printf("send slot %d\n",nexttx); */
673
674 /*
675 * Store a pointer to the packet so we can free it later.
676 */
677 ds->ds_mbuf = m0;
678
679 /* Advance the Tx pointer. */
680 sc->sc_txfree--;
681 sc->sc_txnext = ADMSW_NEXTTXL(nexttx);
682
683 #if NBPFILTER > 0
684 /* Pass the packet to any BPF listeners. */
685 if (ifp->if_bpf)
686 bpf_mtap(ifp->if_bpf, m0);
687 #endif /* NBPFILTER */
688
689 /* Set a watchdog timer in case the chip flakes out. */
690 sc->sc_ethercom[0].ec_if.if_timer = 5;
691 }
692 }
693
694 /*
695 * admsw_watchdog: [ifnet interface function]
696 *
697 * Watchdog timer handler.
698 */
699 static void
700 admsw_watchdog(struct ifnet *ifp)
701 {
702 struct admsw_softc *sc = ifp->if_softc;
703 int vlan;
704
705 #if 1
706 /* Check if an interrupt was lost. */
707 if (sc->sc_txfree == ADMSW_NTXLDESC) {
708 printf("%s: watchdog false alarm\n", sc->sc_dev.dv_xname);
709 return;
710 }
711 if (sc->sc_ethercom[0].ec_if.if_timer != 0)
712 printf("%s: watchdog timer is %d!\n", sc->sc_dev.dv_xname, sc->sc_ethercom[0].ec_if.if_timer);
713 admsw_txintr(sc, 0);
714 if (sc->sc_txfree == ADMSW_NTXLDESC) {
715 printf("%s: tx IRQ lost (queue empty)\n", sc->sc_dev.dv_xname);
716 return;
717 }
718 if (sc->sc_ethercom[0].ec_if.if_timer != 0) {
719 printf("%s: tx IRQ lost (timer recharged)\n", sc->sc_dev.dv_xname);
720 return;
721 }
722 #endif
723
724 printf("%s: device timeout, txfree = %d\n", sc->sc_dev.dv_xname, sc->sc_txfree);
725 for (vlan = 0; vlan < SW_DEVS; vlan++)
726 admsw_stop(&sc->sc_ethercom[vlan].ec_if, 0);
727 for (vlan = 0; vlan < SW_DEVS; vlan++)
728 (void) admsw_init(&sc->sc_ethercom[vlan].ec_if);
729
730 /* Try to get more packets going. */
731 admsw_start(ifp);
732 }
733
734 /*
735 * admsw_ioctl: [ifnet interface function]
736 *
737 * Handle control requests from the operator.
738 */
739 static int
740 admsw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
741 {
742 struct admsw_softc *sc = ifp->if_softc;
743 struct ifdrv *ifd;
744 int s, error, port;
745
746 s = splnet();
747
748 switch (cmd) {
749 case SIOCSIFMEDIA:
750 case SIOCGIFMEDIA:
751 port = (struct ethercom *)ifp - sc->sc_ethercom; /* XXX */
752 if (port >= SW_DEVS)
753 error = EOPNOTSUPP;
754 else
755 error = ifmedia_ioctl(ifp, (struct ifreq *)data,
756 &sc->sc_ifmedia[port], cmd);
757 break;
758
759 case SIOCGDRVSPEC:
760 case SIOCSDRVSPEC:
761 ifd = (struct ifdrv *) data;
762 if (ifd->ifd_cmd != 0 || ifd->ifd_len != sizeof(vlan_matrix)) {
763 error = EINVAL;
764 break;
765 }
766 if (cmd == SIOCGDRVSPEC) {
767 error = copyout(vlan_matrix, ifd->ifd_data,
768 sizeof(vlan_matrix));
769 } else {
770 error = copyin(ifd->ifd_data, vlan_matrix,
771 sizeof(vlan_matrix));
772 admsw_setvlan(sc, vlan_matrix);
773 }
774 break;
775
776 default:
777 error = ether_ioctl(ifp, cmd, data);
778 if (error == ENETRESET) {
779 /*
780 * Multicast list has changed; set the hardware filter
781 * accordingly.
782 */
783 admsw_set_filter(sc);
784 error = 0;
785 }
786 break;
787 }
788
789 /* Try to get more packets going. */
790 admsw_start(ifp);
791
792 splx(s);
793 return (error);
794 }
795
796
797 /*
798 * admsw_intr:
799 *
800 * Interrupt service routine.
801 */
802 static int
803 admsw_intr(void *arg)
804 {
805 struct admsw_softc *sc = arg;
806 uint32_t pending;
807 char buf[64];
808
809 pending = REG_READ(ADMSW_INT_ST);
810 /* printf("admsw_intr: %x\n",pending); */
811
812 if ((pending & ~(ADMSW_INTR_RHD|ADMSW_INTR_RLD|ADMSW_INTR_SHD|ADMSW_INTR_SLD)) != 0) {
813 printf("%s: pending=%s\n", __func__,
814 bitmask_snprintf(pending, ADMSW_INT_FMT, buf, sizeof(buf)));
815 }
816 REG_WRITE(ADMSW_INT_ST, pending);
817
818 if (sc->ndevs == 0)
819 return (0);
820
821 if ((pending & ADMSW_INTR_RHD) != 0)
822 admsw_rxintr(sc, 1);
823
824 if ((pending & ADMSW_INTR_RLD) != 0)
825 admsw_rxintr(sc, 0);
826
827 if ((pending & ADMSW_INTR_SHD) != 0)
828 admsw_txintr(sc, 1);
829
830 if ((pending & ADMSW_INTR_SLD) != 0)
831 admsw_txintr(sc, 0);
832
833 return (1);
834 }
835
836 /*
837 * admsw_txintr:
838 *
839 * Helper; handle transmit interrupts.
840 */
841 static void
842 admsw_txintr(struct admsw_softc *sc, int prio)
843 {
844 struct ifnet *ifp;
845 struct admsw_desc *desc;
846 struct admsw_descsoft *ds;
847 int i, vlan;
848 int gotone = 0;
849
850 /* printf("txintr: txdirty: %d, txfree: %d\n",sc->sc_txdirty, sc->sc_txfree); */
851 for (i = sc->sc_txdirty; sc->sc_txfree != ADMSW_NTXLDESC;
852 i = ADMSW_NEXTTXL(i)) {
853
854 ADMSW_CDTXLSYNC(sc, i,
855 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
856
857 desc = &sc->sc_txldescs[i];
858 ds = &sc->sc_txlsoft[i];
859 if (desc->data & ADM5120_DMA_OWN) {
860 ADMSW_CDTXLSYNC(sc, i,
861 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
862 break;
863 }
864
865 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
866 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
867 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
868 m_freem(ds->ds_mbuf);
869 ds->ds_mbuf = NULL;
870
871 vlan = ffs(desc->status & 0x3f) - 1;
872 if (vlan < 0 || vlan >= SW_DEVS)
873 panic("admsw_txintr: bad vlan\n");
874 ifp = &sc->sc_ethercom[vlan].ec_if;
875 gotone = 1;
876 /* printf("clear tx slot %d\n",i); */
877
878 ifp->if_opackets++;
879
880 sc->sc_txfree++;
881 }
882
883 if (gotone) {
884 sc->sc_txdirty = i;
885 #ifdef ADMSW_EVENT_COUNTERS
886 ADMSW_EVCNT_INCR(&sc->sc_ev_txintr);
887 #endif
888 for (vlan = 0; vlan < SW_DEVS; vlan++)
889 sc->sc_ethercom[vlan].ec_if.if_flags &= ~IFF_OACTIVE;
890
891 ifp = &sc->sc_ethercom[0].ec_if;
892
893 /* Try to queue more packets. */
894 admsw_start(ifp);
895
896 /*
897 * If there are no more pending transmissions,
898 * cancel the watchdog timer.
899 */
900 if (sc->sc_txfree == ADMSW_NTXLDESC)
901 ifp->if_timer = 0;
902
903 }
904
905 /* printf("txintr end: txdirty: %d, txfree: %d\n",sc->sc_txdirty, sc->sc_txfree); */
906 }
907
908 /*
909 * admsw_rxintr:
910 *
911 * Helper; handle receive interrupts.
912 */
913 static void
914 admsw_rxintr(struct admsw_softc *sc, int high)
915 {
916 struct ifnet *ifp;
917 struct admsw_descsoft *ds;
918 struct mbuf *m;
919 uint32_t stat;
920 int i, len, port, vlan;
921
922 /* printf("rxintr\n"); */
923 if (high)
924 panic("admsw_rxintr: high priority packet\n");
925
926 #ifdef ADMSW_EVENT_COUNTERS
927 int pkts = 0;
928 #endif
929
930 #if 1
931 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
932 if ((sc->sc_rxldescs[sc->sc_rxptr].data & ADM5120_DMA_OWN) == 0)
933 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
934 else {
935 i = sc->sc_rxptr;
936 do {
937 ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
938 i = ADMSW_NEXTRXL(i);
939 /* the ring is empty, just return. */
940 if (i == sc->sc_rxptr)
941 return;
942 ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
943 } while (sc->sc_rxldescs[i].data & ADM5120_DMA_OWN);
944 ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
945
946 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
947 if ((sc->sc_rxldescs[sc->sc_rxptr].data & ADM5120_DMA_OWN) == 0)
948 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
949 else {
950 ADMSW_CDRXLSYNC(sc, sc->sc_rxptr, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
951 /* We've fallen behind the chip: catch it. */
952 printf("%s: RX ring resync, base=%x, work=%x, %d -> %d\n",
953 sc->sc_dev.dv_xname, REG_READ(RECV_LBADDR_REG),
954 REG_READ(RECV_LWADDR_REG), sc->sc_rxptr, i);
955 sc->sc_rxptr = i;
956 ADMSW_EVCNT_INCR(&sc->sc_ev_rxsync);
957 }
958 }
959 #endif
960 for (i = sc->sc_rxptr;; i = ADMSW_NEXTRXL(i)) {
961 ds = &sc->sc_rxlsoft[i];
962
963 ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
964
965 if (sc->sc_rxldescs[i].data & ADM5120_DMA_OWN) {
966 ADMSW_CDRXLSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
967 break;
968 }
969
970 /* printf("process slot %d\n",i); */
971
972 #ifdef ADMSW_EVENT_COUNTERS
973 pkts++;
974 #endif
975
976 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
977 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
978
979 stat = sc->sc_rxldescs[i].status;
980 len = (stat & ADM5120_DMA_LEN) >> ADM5120_DMA_LENSHIFT;
981 len -= ETHER_CRC_LEN;
982 port = (stat & ADM5120_DMA_PORTID) >> ADM5120_DMA_PORTSHIFT;
983 for (vlan = 0; vlan < SW_DEVS; vlan++)
984 if ((1 << port) & vlan_matrix[vlan])
985 break;
986 if (vlan == SW_DEVS)
987 vlan = 0;
988 ifp = &sc->sc_ethercom[vlan].ec_if;
989
990 m = ds->ds_mbuf;
991 if (admsw_add_rxlbuf(sc, i) != 0) {
992 ifp->if_ierrors++;
993 ADMSW_INIT_RXLDESC(sc, i);
994 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
995 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
996 continue;
997 }
998
999 m->m_pkthdr.rcvif = ifp;
1000 m->m_pkthdr.len = m->m_len = len;
1001 if ((stat & ADM5120_DMA_TYPE) == ADM5120_DMA_TYPE_IP) {
1002 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1003 if (stat & ADM5120_DMA_CSUMFAIL)
1004 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1005 }
1006 #if NBPFILTER > 0
1007 /* Pass this up to any BPF listeners. */
1008 if (ifp->if_bpf)
1009 bpf_mtap(ifp->if_bpf, m);
1010 #endif /* NBPFILTER > 0 */
1011
1012 /* Pass it on. */
1013 (*ifp->if_input)(ifp, m);
1014 ifp->if_ipackets++;
1015 }
1016 #ifdef ADMSW_EVENT_COUNTERS
1017 if (pkts)
1018 ADMSW_EVCNT_INCR(&sc->sc_ev_rxintr);
1019
1020 if (pkts == ADMSW_NRXLDESC)
1021 ADMSW_EVCNT_INCR(&sc->sc_ev_rxstall);
1022 #endif
1023
1024 /* Update the receive pointer. */
1025 sc->sc_rxptr = i;
1026 }
1027
1028 /*
1029 * admsw_init: [ifnet interface function]
1030 *
1031 * Initialize the interface. Must be called at splnet().
1032 */
1033 static int
1034 admsw_init(struct ifnet *ifp)
1035 {
1036 struct admsw_softc *sc = ifp->if_softc;
1037
1038 /* printf("admsw_init called\n"); */
1039
1040 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1041 if (sc->ndevs == 0) {
1042 admsw_init_bufs(sc);
1043 admsw_reset(sc);
1044 REG_WRITE(CPUP_CONF_REG,
1045 CPUP_CONF_CRCP | CPUP_CONF_DUNP_MASK |
1046 CPUP_CONF_DMCP_MASK);
1047 /* clear all pending interrupts */
1048 REG_WRITE(ADMSW_INT_ST, INT_MASK);
1049
1050 /* enable needed interrupts */
1051 REG_WRITE(ADMSW_INT_MASK, REG_READ(ADMSW_INT_MASK) &
1052 ~(ADMSW_INTR_SHD | ADMSW_INTR_SLD | ADMSW_INTR_RHD |
1053 ADMSW_INTR_RLD | ADMSW_INTR_HDF | ADMSW_INTR_LDF));
1054 }
1055 sc->ndevs++;
1056 }
1057
1058 /* Set the receive filter. */
1059 admsw_set_filter(sc);
1060
1061 /* mark iface as running */
1062 ifp->if_flags |= IFF_RUNNING;
1063 ifp->if_flags &= ~IFF_OACTIVE;
1064
1065 return 0;
1066 }
1067
1068 /*
1069 * admsw_stop: [ifnet interface function]
1070 *
1071 * Stop transmission on the interface.
1072 */
1073 static void
1074 admsw_stop(struct ifnet *ifp, int disable)
1075 {
1076 struct admsw_softc *sc = ifp->if_softc;
1077
1078 /* printf("admsw_stop: %d\n",disable); */
1079
1080 if (!(ifp->if_flags & IFF_RUNNING))
1081 return;
1082
1083 if (--sc->ndevs == 0) {
1084 /* printf("debug: de-initializing hardware\n"); */
1085
1086 /* disable cpu port */
1087 REG_WRITE(CPUP_CONF_REG,
1088 CPUP_CONF_DCPUP | CPUP_CONF_CRCP |
1089 CPUP_CONF_DUNP_MASK | CPUP_CONF_DMCP_MASK);
1090
1091 /* XXX We should disable, then clear? --dyoung */
1092 /* clear all pending interrupts */
1093 REG_WRITE(ADMSW_INT_ST, INT_MASK);
1094
1095 /* disable interrupts */
1096 REG_WRITE(ADMSW_INT_MASK, INT_MASK);
1097 }
1098
1099 /* Mark the interface as down and cancel the watchdog timer. */
1100 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1101 ifp->if_timer = 0;
1102
1103 return;
1104 }
1105
1106 /*
1107 * admsw_set_filter:
1108 *
1109 * Set up the receive filter.
1110 */
1111 static void
1112 admsw_set_filter(struct admsw_softc *sc)
1113 {
1114 int i;
1115 uint32_t allmc, anymc, conf, promisc;
1116 struct ether_multi *enm;
1117 struct ethercom *ec;
1118 struct ifnet *ifp;
1119 struct ether_multistep step;
1120
1121 /* Find which ports should be operated in promisc mode. */
1122 allmc = anymc = promisc = 0;
1123 for (i = 0; i < SW_DEVS; i++) {
1124 ec = &sc->sc_ethercom[i];
1125 ifp = &ec->ec_if;
1126 if (ifp->if_flags & IFF_PROMISC)
1127 promisc |= vlan_matrix[i];
1128
1129 ifp->if_flags &= ~IFF_ALLMULTI;
1130
1131 ETHER_FIRST_MULTI(step, ec, enm);
1132 while (enm != NULL) {
1133 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1134 ETHER_ADDR_LEN) != 0) {
1135 printf("%s: punting on mcast range\n",
1136 __func__);
1137 ifp->if_flags |= IFF_ALLMULTI;
1138 allmc |= vlan_matrix[i];
1139 break;
1140 }
1141
1142 anymc |= vlan_matrix[i];
1143
1144 #if 0
1145 /* XXX extract subroutine --dyoung */
1146 REG_WRITE(MAC_WT1_REG,
1147 enm->enm_addrlo[2] |
1148 (enm->enm_addrlo[3] << 8) |
1149 (enm->enm_addrlo[4] << 16) |
1150 (enm->enm_addrlo[5] << 24));
1151 REG_WRITE(MAC_WT0_REG,
1152 (i << MAC_WT0_VLANID_SHIFT) |
1153 (enm->enm_addrlo[0] << 16) |
1154 (enm->enm_addrlo[1] << 24) |
1155 MAC_WT0_WRITE | MAC_WT0_VLANID_EN);
1156 /* timeout? */
1157 while (!(REG_READ(MAC_WT0_REG) & MAC_WT0_WRITE_DONE));
1158 #endif
1159
1160 /* load h/w with mcast address, port = CPU */
1161 ETHER_NEXT_MULTI(step, enm);
1162 }
1163 }
1164
1165 conf = REG_READ(CPUP_CONF_REG);
1166 /* 1 Disable forwarding of unknown & multicast packets to
1167 * CPU on all ports.
1168 * 2 Enable forwarding of unknown & multicast packets to
1169 * CPU on ports where IFF_PROMISC or IFF_ALLMULTI is set.
1170 */
1171 conf |= CPUP_CONF_DUNP_MASK | CPUP_CONF_DMCP_MASK;
1172 /* Enable forwarding of unknown packets to CPU on selected ports. */
1173 conf ^= ((promisc << CPUP_CONF_DUNP_SHIFT) & CPUP_CONF_DUNP_MASK);
1174 conf ^= ((allmc << CPUP_CONF_DMCP_SHIFT) & CPUP_CONF_DMCP_MASK);
1175 conf ^= ((anymc << CPUP_CONF_DMCP_SHIFT) & CPUP_CONF_DMCP_MASK);
1176 REG_WRITE(CPUP_CONF_REG, conf);
1177 }
1178
1179 /*
1180 * admsw_add_rxbuf:
1181 *
1182 * Add a receive buffer to the indicated descriptor.
1183 */
1184 int
1185 admsw_add_rxbuf(struct admsw_softc *sc, int idx, int high)
1186 {
1187 struct admsw_descsoft *ds;
1188 struct mbuf *m;
1189 int error;
1190
1191 if (high)
1192 ds = &sc->sc_rxhsoft[idx];
1193 else
1194 ds = &sc->sc_rxlsoft[idx];
1195
1196 MGETHDR(m, M_DONTWAIT, MT_DATA);
1197 if (m == NULL)
1198 return (ENOBUFS);
1199
1200 MCLGET(m, M_DONTWAIT);
1201 if ((m->m_flags & M_EXT) == 0) {
1202 m_freem(m);
1203 return (ENOBUFS);
1204 }
1205
1206 if (ds->ds_mbuf != NULL)
1207 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1208
1209 ds->ds_mbuf = m;
1210
1211 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1212 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1213 BUS_DMA_READ | BUS_DMA_NOWAIT);
1214 if (error) {
1215 printf("%s: can't load rx DMA map %d, error = %d\n",
1216 sc->sc_dev.dv_xname, idx, error);
1217 panic("admsw_add_rxbuf"); /* XXX */
1218 }
1219
1220 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1221 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1222
1223 if (high)
1224 ADMSW_INIT_RXHDESC(sc, idx);
1225 else
1226 ADMSW_INIT_RXLDESC(sc, idx);
1227
1228 return (0);
1229 }
1230
1231 int
1232 admsw_mediachange(struct ifnet *ifp)
1233 {
1234 struct admsw_softc *sc = ifp->if_softc;
1235 int port = (struct ethercom *)ifp - sc->sc_ethercom; /* XXX */
1236 struct ifmedia *ifm = &sc->sc_ifmedia[port];
1237 int old, new, val;
1238
1239 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1240 return (EINVAL);
1241
1242 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
1243 val = PHY_CNTL2_AUTONEG|PHY_CNTL2_100M|PHY_CNTL2_FDX;
1244 } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
1245 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1246 val = PHY_CNTL2_100M|PHY_CNTL2_FDX;
1247 else
1248 val = PHY_CNTL2_100M;
1249 } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) {
1250 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1251 val = PHY_CNTL2_FDX;
1252 else
1253 val = 0;
1254 } else
1255 return (EINVAL);
1256
1257 old = REG_READ(PHY_CNTL2_REG);
1258 new = old & ~((PHY_CNTL2_AUTONEG|PHY_CNTL2_100M|PHY_CNTL2_FDX) << port);
1259 new |= (val << port);
1260
1261 if (new != old)
1262 REG_WRITE(PHY_CNTL2_REG, new);
1263
1264 return (0);
1265 }
1266
1267 void
1268 admsw_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1269 {
1270 struct admsw_softc *sc = ifp->if_softc;
1271 int port = (struct ethercom *)ifp - sc->sc_ethercom; /* XXX */
1272 int status;
1273
1274 ifmr->ifm_status = IFM_AVALID;
1275 ifmr->ifm_active = IFM_ETHER;
1276
1277 status = REG_READ(PHY_ST_REG) >> port;
1278
1279 if ((status & PHY_ST_LINKUP) == 0) {
1280 ifmr->ifm_active |= IFM_NONE;
1281 return;
1282 }
1283
1284 ifmr->ifm_status |= IFM_ACTIVE;
1285 ifmr->ifm_active |= (status & PHY_ST_100M) ? IFM_100_TX : IFM_10_T;
1286 if (status & PHY_ST_FDX)
1287 ifmr->ifm_active |= IFM_FDX;
1288 }
1289